Skip to content

Commit

Permalink
fix submitting and redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
fiskus committed Sep 18, 2024
1 parent 496073c commit 1b40eae
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions catalog/app/containers/Admin/Buckets/Buckets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function Card({
<M.AccordionSummary
className={classes.summary}
expandIcon={<M.Icon>{expanded ? 'close' : 'edit'}</M.Icon>}
disabled={hasError}
disabled={disabled}
>
{icon && <CardAvatar className={classes.icon} src={icon} />}
<div className={classes.summaryInner}>
Expand Down Expand Up @@ -625,17 +625,23 @@ interface InlineActionsProps<T> {
form: FF.FormApi<T>
onCancel: () => void
onDirty: (dirty: boolean) => void
disabled: boolean
}

// TODO: disabled (when another form is submitting)
function InlineActions<T>({ form, onDirty, onCancel }: InlineActionsProps<T>) {
function InlineActions<T>({ form, disabled, onDirty, onCancel }: InlineActionsProps<T>) {

Check warning on line 631 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#L631

Added line #L631 was not covered by tests
const classes = useInlineActionsStyles()
const state = form.getState()
const { reset, submit } = form

Check warning on line 634 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#L634

Added line #L634 was not covered by tests
const handleCancel = React.useCallback(() => {
reset()

Check warning on line 636 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#L636

Added line #L636 was not covered by tests
onCancel()
}, [reset, onCancel])
const error = React.useMemo(() => {

Check warning on line 639 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#L639

Added line #L639 was not covered by tests
if (!state.submitFailed) return
if (state.error || state.submitError) return state.error || state.submitError
// This could happen only if we forgot to handle an error in fields
return `Unhandled error: ${JSON.stringify(state.submitErrors)}`

Check warning on line 643 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#L643

Added line #L643 was not covered by tests
}, [state])
return (
<>
<RF.FormSpy
Expand All @@ -645,7 +651,7 @@ function InlineActions<T>({ form, onDirty, onCancel }: InlineActionsProps<T>) {
{state.submitFailed && (
<Form.FormError
className={classes.helper}
error={state.error || state.submitError}
error={error}
errors={{
unexpected: 'Something went wrong',
notificationConfigurationError: 'Notification configuration error',
Expand All @@ -666,11 +672,15 @@ function InlineActions<T>({ form, onDirty, onCancel }: InlineActionsProps<T>) {
<M.Button
onClick={() => reset()}

Check warning on line 673 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#L673

Added line #L673 was not covered by tests
color="primary"
disabled={state.pristine || state.submitting}
disabled={state.pristine || state.submitting || disabled}
>
Reset
</M.Button>
<M.Button onClick={handleCancel} color="primary" disabled={state.submitting}>
<M.Button
onClick={handleCancel}
color="primary"
disabled={state.submitting || disabled}
>
Cancel
</M.Button>
<M.Button
Expand All @@ -679,7 +689,8 @@ function InlineActions<T>({ form, onDirty, onCancel }: InlineActionsProps<T>) {
disabled={
state.pristine ||
state.submitting ||
(state.submitFailed && state.hasValidationErrors)
(state.submitFailed && state.hasValidationErrors) ||
disabled

Check warning on line 693 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#L692-L693

Added lines #L692 - L693 were not covered by tests
}
variant="contained"
>
Expand Down Expand Up @@ -814,6 +825,7 @@ function PrimaryCard({
<Card
actions={
<InlineActions<PrimaryFormValues>
disabled={disabled}
form={form}
onCancel={() => setEditing(false)}

Check warning on line 830 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#L830

Added line #L830 was not covered by tests
onDirty={onDirty}
Expand Down Expand Up @@ -953,6 +965,7 @@ function MetadataCard({
<Card
actions={
<InlineActions
disabled={disabled}
form={form}
onCancel={() => setEditing(false)}

Check warning on line 970 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#L970

Added line #L970 was not covered by tests
onDirty={onDirty}
Expand Down Expand Up @@ -1247,6 +1260,7 @@ function IndexingAndNotificationsCard({
<Card
actions={
<InlineActions<IndexingAndNotificationsFormValues>
disabled={disabled}
form={form}
onCancel={() => setEditing(false)}

Check warning on line 1265 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#L1265

Added line #L1265 was not covered by tests
onDirty={onDirty}
Expand Down Expand Up @@ -1372,6 +1386,7 @@ function PreviewCard({
<Card
actions={
<InlineActions<PreviewFormValues>
disabled={disabled}
form={form}
onCancel={() => setEditing(false)}

Check warning on line 1391 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#L1391

Added line #L1391 was not covered by tests
onDirty={onDirty}
Expand Down Expand Up @@ -1568,6 +1583,7 @@ function Add({ back, settings, submit }: AddProps) {
const error = await submit(input)
if (!error) {
form.reset(values)
back()
return

Check warning on line 1587 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#L1585-L1587

Added lines #L1585 - L1587 were not covered by tests
}
if (error instanceof Error) throw error
Expand All @@ -1580,7 +1596,7 @@ function Add({ back, settings, submit }: AddProps) {
return { [FF.FORM_ERROR]: 'unexpected' }
}
},
[submit],
[back, submit],
)
const scrollingRef = React.useRef<HTMLFormElement>(null)
const guardNavigation = React.useCallback(
Expand Down Expand Up @@ -1877,24 +1893,10 @@ function Edit({ bucket, back, submit, tabulatorTables }: EditProps) {
[],
)

interface OnSubmit {
(
values: PrimaryFormValues,
form: FF.FormApi<PrimaryFormValues>,
): Promise<FF.SubmissionErrors | undefined>
(
values: MetadataFormValues,
form: FF.FormApi<MetadataFormValues>,
): Promise<FF.SubmissionErrors | undefined>
(
values: IndexingAndNotificationsFormValues,
form: FF.FormApi<IndexingAndNotificationsFormValues>,
): Promise<FF.SubmissionErrors | undefined>
(
values: PreviewFormValues,
form: FF.FormApi<PreviewFormValues>,
): Promise<FF.SubmissionErrors | undefined>
}
type OnSubmit = FF.Config<PrimaryFormValues>['onSubmit'] &
FF.Config<MetadataFormValues>['onSubmit'] &
FF.Config<IndexingAndNotificationsFormValues>['onSubmit'] &
FF.Config<PreviewFormValues>['onSubmit']

const onSubmit: OnSubmit = React.useCallback(
async (values, form) => {

Check warning on line 1902 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#L1901-L1902

Added lines #L1901 - L1902 were not covered by tests
Expand Down Expand Up @@ -2015,12 +2017,11 @@ function EditPage({ back }: EditPageProps) {
Model.GQLTypes.BucketUpdateSuccess
>
}
back()
} catch (e) {
return e instanceof Error ? e : new Error('Error updating bucket')
}
},
[back, bucket, update],
[bucket, update],
)
if (!bucket) return <RRDom.Redirect to={urls.adminBuckets()} />
return (

Check warning on line 2027 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#L2027

Added line #L2027 was not covered by tests
Expand All @@ -2042,24 +2043,24 @@ function AddPage({ back }: AddPageProps) {
async (input: Model.GQLTypes.BucketAddInput) => {
try {
const { bucketAdd: r } = await add({ input })
if (r.__typename !== 'BucketAddSuccess')
if (r.__typename !== 'BucketAddSuccess') {
// TS infered shape but not the actual type
return r as Exclude<
Model.GQLTypes.BucketAddResult,
Model.GQLTypes.BucketAddSuccess
>
}
push(`Bucket "${r.bucketConfig.name}" added`)
track('WEB', {
type: 'admin',
action: 'bucket add',
bucket: r.bucketConfig.name,
})
back()
} catch (e) {
return e instanceof Error ? e : new Error('Error adding bucket')
}
},
[add, back, push, track],
[add, push, track],
)
return <Add settings={settings} back={back} submit={submit} />
}
Expand Down

0 comments on commit 1b40eae

Please sign in to comment.