Skip to content

Commit

Permalink
fix dirty state; disallow route navigation when dirty state
Browse files Browse the repository at this point in the history
  • Loading branch information
fiskus committed Sep 20, 2024
1 parent aaa1102 commit 27db082
Showing 1 changed file with 47 additions and 17 deletions.
64 changes: 47 additions & 17 deletions catalog/app/containers/Admin/Buckets/Buckets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,15 @@ interface InlineActionsProps<T> {
form: FF.FormApi<T>
onCancel: () => void
disabled: boolean
onFormSpy: (state: FF.FormState<T>) => void
}

function InlineActions<T>({ form, disabled, onCancel }: InlineActionsProps<T>) {
function InlineActions<T>({
form,
disabled,
onCancel,
onFormSpy,
}: InlineActionsProps<T>) {

Check warning on line 652 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L648-L652

Added lines #L648 - L652 were not covered by tests
const classes = useInlineActionsStyles()
const state = form.getState()
const { reset, submit } = form

Check warning on line 655 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L655

Added line #L655 was not covered by tests
Expand All @@ -659,6 +665,7 @@ function InlineActions<T>({ form, disabled, onCancel }: InlineActionsProps<T>) {
}, [state])
return (
<>
<RF.FormSpy subscription={{ modified: true, dirty: true }} onChange={onFormSpy} />
{state.submitFailed && (
<Form.FormError
className={classes.helper}
Expand Down Expand Up @@ -817,6 +824,7 @@ interface PrimaryCardProps {
disabled: boolean
editing: boolean
onEdit: (ex: boolean) => void
onFormSpy: (state: FF.FormState<PrimaryFormValues>) => void
onSubmit: FF.Config<PrimaryFormValues>['onSubmit']
}

Expand All @@ -826,6 +834,7 @@ function PrimaryCard({
disabled,
editing,
onEdit,
onFormSpy,
onSubmit,
}: PrimaryCardProps) {

Check warning on line 839 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L832-L839

Added lines #L832 - L839 were not covered by tests
return (
Expand All @@ -840,6 +849,7 @@ function PrimaryCard({
disabled={disabled}
form={form}
onCancel={() => onEdit(false)}

Check warning on line 851 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L851

Added line #L851 was not covered by tests
onFormSpy={onFormSpy}
/>
}
form={
Expand Down Expand Up @@ -956,6 +966,7 @@ interface MetadataCardProps {
disabled: boolean
editing: boolean
onEdit: (ex: boolean) => void
onFormSpy: (state: FF.FormState<MetadataFormValues>) => void
onSubmit: FF.Config<MetadataFormValues>['onSubmit']
}

Expand All @@ -965,6 +976,7 @@ function MetadataCard({
disabled,
editing,
onEdit,
onFormSpy,
onSubmit,
}: MetadataCardProps) {

Check warning on line 981 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L974-L981

Added lines #L974 - L981 were not covered by tests
const classes = useMetadataCardStyles()
Expand All @@ -980,6 +992,7 @@ function MetadataCard({
disabled={disabled}
form={form}
onCancel={() => onEdit(false)}

Check warning on line 994 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L994

Added line #L994 was not covered by tests
onFormSpy={onFormSpy}
/>
}
hasError={submitFailed}
Expand Down Expand Up @@ -1237,11 +1250,12 @@ type IndexingAndNotificationsFormValues = ReturnType<
>

interface IndexingAndNotificationsCardProps {
editing: boolean
onEdit: (ex: boolean) => void
disabled: boolean
bucket: BucketConfig
className: string
disabled: boolean
editing: boolean
onEdit: (ex: boolean) => void
onFormSpy: (state: FF.FormState<IndexingAndNotificationsFormValues>) => void
onSubmit: FF.Config<IndexingAndNotificationsFormValues>['onSubmit']
reindex?: () => void
}
Expand All @@ -1250,9 +1264,10 @@ function IndexingAndNotificationsCard({
bucket,
className,
disabled,
onSubmit,
onEdit,
editing,
onEdit,
onFormSpy,
onSubmit,

Check warning on line 1270 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L1266-L1270

Added lines #L1266 - L1270 were not covered by tests
reindex,
}: IndexingAndNotificationsCardProps) {
const data = GQL.useQueryS(CONTENT_INDEXING_SETTINGS_QUERY)
Expand All @@ -1273,6 +1288,7 @@ function IndexingAndNotificationsCard({
disabled={disabled}
form={form}
onCancel={() => onEdit(false)}

Check warning on line 1290 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L1290

Added line #L1290 was not covered by tests
onFormSpy={onFormSpy}
/>
}
hasError={submitFailed}
Expand Down Expand Up @@ -1376,6 +1392,7 @@ interface PreviewCardProps {
disabled: boolean
editing: boolean
onEdit: (ex: boolean) => void
onFormSpy: (state: FF.FormState<PreviewFormValues>) => void
onSubmit: FF.Config<PreviewFormValues>['onSubmit']
}

Expand All @@ -1385,6 +1402,7 @@ function PreviewCard({
disabled,
editing,
onEdit,
onFormSpy,
onSubmit,
}: PreviewCardProps) {
return (

Check warning on line 1408 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L1400-L1408

Added lines #L1400 - L1408 were not covered by tests
Expand All @@ -1399,6 +1417,7 @@ function PreviewCard({
disabled={disabled}
form={form}
onCancel={() => onEdit(false)}

Check warning on line 1419 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L1419

Added line #L1419 was not covered by tests
onFormSpy={onFormSpy}
/>
}
hasError={submitFailed}
Expand Down Expand Up @@ -1436,6 +1455,7 @@ interface LongQueryConfigCardProps {
editing: boolean
onEdit: (ex: boolean) => void
tabulatorTables: Model.GQLTypes.BucketConfig['tabulatorTables']
onFormSpy: (state: { modified: { tabulator: boolean }; dirty: boolean }) => void
}

function LongQueryConfigCard({
Expand All @@ -1444,15 +1464,18 @@ function LongQueryConfigCard({
disabled,
editing,
onEdit,
onFormSpy,
tabulatorTables,
}: LongQueryConfigCardProps) {
const [dirtyCounter, setDirtyCounter] = React.useState(0)
const onFormSpy = React.useCallback(({ dirty, modified }) => {
setDirtyCounter((c) => {
if (Object.values(modified).every((v) => !v)) return c
return dirty ? c + 1 : Math.max(c - 1, 0)
})
}, [])
const handleFormSpy = React.useCallback(
({ dirty, modified }) => {

Check warning on line 1472 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L1462-L1472

Added lines #L1462 - L1472 were not covered by tests
if (Object.values(modified).every((v) => !v)) return
setDirtyCounter((c) => (dirty ? c + 1 : Math.max(c - 1, 0)))
onFormSpy({ modified: { tabulator: true }, dirty })

Check warning on line 1475 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L1475

Added line #L1475 was not covered by tests
},
[onFormSpy],
)
const handleEdit = React.useCallback(
(e) => {

Check warning on line 1480 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L1479-L1480

Added lines #L1479 - L1480 were not covered by tests
if (!e && !!dirtyCounter) {
Expand All @@ -1470,7 +1493,7 @@ function LongQueryConfigCard({
bucket={bucket}
onClose={() => handleEdit(false)}

Check warning on line 1494 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L1494

Added line #L1494 was not covered by tests
tabulatorTables={tabulatorTables}
onFormSpy={onFormSpy}
onFormSpy={handleFormSpy}
/>
}
editing={editing}
Expand Down Expand Up @@ -1964,15 +1987,17 @@ function Edit({ bucket, back, submit, tabulatorTables }: EditProps) {
const handleEditing = (cardName: keyof typeof editing) => (expanded: boolean) => {
setEditing((x) => ({ ...x, [cardName]: expanded }))

Check warning on line 1988 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L1987-L1988

Added lines #L1987 - L1988 were not covered by tests
}
const [dirtyCounter, setDirtyCounter] = React.useState(0)
const onFormSpy = React.useCallback(({ dirty, modified }) => {

Check warning on line 1991 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L1990-L1991

Added lines #L1990 - L1991 were not covered by tests
if (Object.values(modified).every((v) => !v)) return
setDirtyCounter((c) => (dirty ? c + 1 : Math.max(c - 1, 0)))
}, [])
const guardNavigation = () =>
'Some forms are opened and ready for modification. Leave the page anyway?'

Check warning on line 1996 in catalog/app/containers/Admin/Buckets/Buckets.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Buckets/Buckets.tsx#L1995-L1996

Added lines #L1995 - L1996 were not covered by tests

return (
<>
<RRDom.Prompt
when={!!Object.values(editing).some((n) => n)}
message={guardNavigation}
/>
<RRDom.Prompt when={!!dirtyCounter} message={guardNavigation} />
<Reindex bucket={bucket.name} open={reindexOpen} close={closeReindex} />
<SubPageHeader back={back} disabled={disabled} />
<React.Suspense fallback={<CardsPlaceholder className={classes.fields} />}>
Expand All @@ -1984,6 +2009,7 @@ function Edit({ bucket, back, submit, tabulatorTables }: EditProps) {
disabled={disabled}
editing={editing.primary}
onEdit={handleEditing('primary')}
onFormSpy={onFormSpy}
onSubmit={onSubmit}
/>
<MetadataCard
Expand All @@ -1992,6 +2018,7 @@ function Edit({ bucket, back, submit, tabulatorTables }: EditProps) {
disabled={disabled}
editing={editing.metadata}
onEdit={handleEditing('metadata')}
onFormSpy={onFormSpy}
onSubmit={onSubmit}
/>
<IndexingAndNotificationsCard
Expand All @@ -2000,6 +2027,7 @@ function Edit({ bucket, back, submit, tabulatorTables }: EditProps) {
disabled={disabled}
editing={editing.indexing}
onEdit={handleEditing('indexing')}
onFormSpy={onFormSpy}
onSubmit={onSubmit}
reindex={openReindex}
/>
Expand All @@ -2009,6 +2037,7 @@ function Edit({ bucket, back, submit, tabulatorTables }: EditProps) {
disabled={disabled}
editing={editing.preview}
onEdit={handleEditing('preview')}
onFormSpy={onFormSpy}
onSubmit={onSubmit}
/>
</div>
Expand All @@ -2019,6 +2048,7 @@ function Edit({ bucket, back, submit, tabulatorTables }: EditProps) {
editing={editing.tabulator}
onEdit={handleEditing('tabulator')}
tabulatorTables={tabulatorTables}
onFormSpy={onFormSpy}
/>
</div>
</React.Suspense>
Expand Down

0 comments on commit 27db082

Please sign in to comment.