Skip to content

Commit a3442f1

Browse files
committed
Add async onSubmit
1 parent ef15f1c commit a3442f1

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

src/steps/ValidationStep/ValidationStep.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
2828
const [selectedRows, setSelectedRows] = useState<ReadonlySet<number | string>>(new Set())
2929
const [filterByErrors, setFilterByErrors] = useState(false)
3030
const [showSubmitAlert, setShowSubmitAlert] = useState(false)
31+
const [isSubmitting, setSubmitting] = useState(false)
3132

3233
const updateData = useCallback(
3334
async (rows: typeof data, indexes?: number[]) => {
@@ -96,9 +97,16 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
9697
},
9798
{ validData: [] as Data<T>[], invalidData: [] as Data<T>[], all: data },
9899
)
99-
onSubmit(calculatedData, file)
100100
setShowSubmitAlert(false)
101-
onClose()
101+
setSubmitting(true)
102+
onSubmit(calculatedData, file)
103+
?.then(() => {
104+
onClose()
105+
})
106+
?.catch(() => {})
107+
?.finally(() => {
108+
setSubmitting(false)
109+
})
102110
}
103111
const onContinue = () => {
104112
const invalidData = data.find((value) => {
@@ -153,6 +161,7 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
153161
/>
154162
</ModalBody>
155163
<ContinueButton
164+
isLoading={isSubmitting}
156165
onContinue={onContinue}
157166
onBack={onBack}
158167
title={translations.validationStep.nextButtonTitle}

src/steps/ValidationStep/tests/ValidationStep.test.tsx

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,68 @@ describe("Validation step tests", () => {
4848
})
4949
})
5050

51+
test("Submit data with a successful async return", async () => {
52+
const onSuccess = jest.fn()
53+
const onSubmit = jest.fn(async (): Promise<void> => {
54+
onSuccess()
55+
return Promise.resolve()
56+
})
57+
const onClose = jest.fn()
58+
render(
59+
<Providers theme={defaultTheme} rsiValues={{ ...mockValues, onSubmit, onClose }}>
60+
<ModalWrapper isOpen={true} onClose={() => {}}>
61+
<ValidationStep initialData={[]} file={file} />
62+
</ModalWrapper>
63+
</Providers>,
64+
)
65+
66+
const finishButton = screen.getByRole("button", {
67+
name: "Confirm",
68+
})
69+
70+
await userEvent.click(finishButton)
71+
72+
await waitFor(() => {
73+
expect(onSubmit).toBeCalledWith({ all: [], invalidData: [], validData: [] }, file)
74+
})
75+
await waitFor(() => {
76+
expect(onSuccess).toBeCalled()
77+
expect(onClose).toBeCalled()
78+
})
79+
})
80+
81+
test("Submit data with a unsuccessful async return", async () => {
82+
const onReject = jest.fn()
83+
const onSubmit = jest.fn(async (): Promise<void> => {
84+
onReject()
85+
return Promise.reject()
86+
})
87+
const onClose = jest.fn()
88+
try {
89+
render(
90+
<Providers theme={defaultTheme} rsiValues={{ ...mockValues, onSubmit, onClose }}>
91+
<ModalWrapper isOpen={true} onClose={() => {}}>
92+
<ValidationStep initialData={[]} file={file} />
93+
</ModalWrapper>
94+
</Providers>,
95+
)
96+
97+
const finishButton = screen.getByRole("button", {
98+
name: "Confirm",
99+
})
100+
101+
await userEvent.click(finishButton)
102+
103+
await waitFor(() => {
104+
expect(onSubmit).toBeCalledWith({ all: [], invalidData: [], validData: [] }, file)
105+
})
106+
} catch (e) {}
107+
await waitFor(() => {
108+
expect(onReject).toBeCalled()
109+
expect(onClose).not.toBeCalled()
110+
})
111+
})
112+
51113
test("Filters rows with required errors", async () => {
52114
const UNIQUE_NAME = "very unique name"
53115
const fields = [

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export type RsiProps<T extends string> = {
2121
rowHook?: RowHook<T>
2222
// Runs after column matching and on entry change
2323
tableHook?: TableHook<T>
24-
// Function called after user finishes the flow
25-
onSubmit: (data: Result<T>, file: File) => void
24+
// Function called after user finishes the flow. You can return a promise that will be awaited.
25+
onSubmit: (data: Result<T>, file: File) => void | Promise<any>
2626
// Allows submitting with errors. Default: true
2727
allowInvalidSubmit?: boolean
2828
// Enable navigation in stepper component and show back button. Default: false

0 commit comments

Comments
 (0)