Skip to content

Commit 4b71537

Browse files
committed
Add a toast message on unsuccessful submit
1 parent f48c3d3 commit 4b71537

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import { ReactSpreadsheetImport } from "react-spreadsheet-import";
5555
isOpen: Boolean
5656
// Called when flow is closed without reaching submit.
5757
onClose: () => void
58-
// Called after user completes the flow. Provides data array, where data keys matches your field keys. You can return a promise that will be awaited.
58+
// Called after user completes the flow. Provides data array, where data keys matches your field keys.
5959
onSubmit: (data, file) => void | Promise<any>
6060
```
6161

src/steps/ValidationStep/ValidationStep.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useCallback, useMemo, useState } from "react"
2-
import { Box, Button, Heading, ModalBody, Switch, useStyleConfig } from "@chakra-ui/react"
2+
import { Box, Button, Heading, ModalBody, Switch, useStyleConfig, useToast } from "@chakra-ui/react"
33
import { ContinueButton } from "../../components/ContinueButton"
44
import { useRsi } from "../../hooks/useRsi"
55
import type { Meta } from "./types"
@@ -22,6 +22,7 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
2222
const styles = useStyleConfig(
2323
"ValidationStep",
2424
) as (typeof themeOverrides)["components"]["ValidationStep"]["baseStyle"]
25+
const toast = useToast()
2526

2627
const [data, setData] = useState<(Data<T> & Meta)[]>(initialData)
2728

@@ -103,8 +104,17 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
103104
?.then(() => {
104105
onClose()
105106
})
106-
?.catch(() => {})
107-
?.finally(() => {
107+
.catch((err: Error) => {
108+
toast({
109+
status: "error",
110+
variant: "left-accent",
111+
position: "bottom-left",
112+
title: `${translations.alerts.submitError.title}`,
113+
description: err?.message || `${translations.alerts.submitError.defaultMessage}`,
114+
isClosable: true,
115+
})
116+
})
117+
.finally(() => {
108118
setSubmitting(false)
109119
})
110120
}

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,35 +79,37 @@ describe("Validation step tests", () => {
7979
})
8080

8181
test("Submit data with a unsuccessful async return", async () => {
82+
const ERROR_MESSAGE = "ERROR has occurred"
8283
const onReject = jest.fn()
8384
const onSubmit = jest.fn(async (): Promise<void> => {
8485
onReject()
85-
return Promise.reject()
86+
throw new Error(ERROR_MESSAGE)
8687
})
8788
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-
)
9689

97-
const finishButton = screen.getByRole("button", {
98-
name: "Confirm",
99-
})
90+
render(
91+
<Providers theme={defaultTheme} rsiValues={{ ...mockValues, onSubmit, onClose }}>
92+
<ModalWrapper isOpen={true} onClose={() => {}}>
93+
<ValidationStep initialData={[]} file={file} />
94+
</ModalWrapper>
95+
</Providers>,
96+
)
10097

101-
await userEvent.click(finishButton)
98+
const finishButton = screen.getByRole("button", {
99+
name: "Confirm",
100+
})
101+
102+
await userEvent.click(finishButton)
102103

103-
await waitFor(() => {
104-
expect(onSubmit).toBeCalledWith({ all: [], invalidData: [], validData: [] }, file)
105-
})
106-
} catch (e) {}
107104
await waitFor(() => {
108-
expect(onReject).toBeCalled()
109-
expect(onClose).not.toBeCalled()
105+
expect(onSubmit).toBeCalledWith({ all: [], invalidData: [], validData: [] }, file)
110106
})
107+
108+
const errorToast = await screen.findAllByText(ERROR_MESSAGE, undefined, { timeout: 5000 })
109+
110+
expect(onReject).toBeCalled()
111+
expect(errorToast?.[0]).toBeInTheDocument()
112+
expect(onClose).not.toBeCalled()
111113
})
112114

113115
test("Filters rows with required errors", async () => {

src/translationsRSIProps.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ export const translations = {
6161
cancelButtonTitle: "Cancel",
6262
finishButtonTitle: "Submit",
6363
},
64+
submitError: {
65+
title: "Error",
66+
defaultMessage: "An error occurred while submitting data",
67+
},
6468
unmatchedRequiredFields: {
6569
headerTitle: "Not all columns matched",
6670
bodyText: "There are required columns that are not matched or ignored. Do you want to continue?",

0 commit comments

Comments
 (0)