Skip to content

Commit 2c8faaa

Browse files
authored
Merge pull request #168 from UgnisSoftware/UGN-368
feat UGN-368 - original file in onSubmit
2 parents 238a959 + d53c06d commit 2c8faaa

7 files changed

Lines changed: 31 additions & 24 deletions

File tree

src/steps/UploadFlow.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ interface Props {
4747
export const UploadFlow = ({ nextStep }: Props) => {
4848
const { initialStepState } = useRsi()
4949
const [state, setState] = useState<StepState>(initialStepState || { type: StepType.upload })
50+
const [uploadedFile, setUploadedFile] = useState<File | null>(null)
5051
const { maxRecords, translations, uploadStepHook, selectHeaderStepHook, matchColumnsStepHook } = useRsi()
5152
const toast = useToast()
5253
const errorToast = useCallback(
@@ -67,7 +68,8 @@ export const UploadFlow = ({ nextStep }: Props) => {
6768
case StepType.upload:
6869
return (
6970
<UploadStep
70-
onContinue={async (workbook) => {
71+
onContinue={async (workbook, file) => {
72+
setUploadedFile(file)
7173
const isSingleSheet = workbook.SheetNames.length === 1
7274
if (isSingleSheet) {
7375
if (maxRecords && exceedsMaxRecords(workbook.Sheets[workbook.SheetNames[0]], maxRecords)) {
@@ -151,7 +153,7 @@ export const UploadFlow = ({ nextStep }: Props) => {
151153
/>
152154
)
153155
case StepType.validateData:
154-
return <ValidationStep initialData={state.data} />
156+
return <ValidationStep initialData={state.data} file={uploadedFile!} />
155157
default:
156158
return <Progress isIndeterminate />
157159
}

src/steps/UploadStep/UploadStep.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import { FadingOverlay } from "./components/FadingOverlay"
88
import type { themeOverrides } from "../../theme"
99

1010
type UploadProps = {
11-
onContinue: (data: XLSX.WorkBook) => Promise<void>
11+
onContinue: (data: XLSX.WorkBook, file: File) => Promise<void>
1212
}
1313

1414
export const UploadStep = ({ onContinue }: UploadProps) => {
1515
const [isLoading, setIsLoading] = useState(false)
1616
const styles = useStyleConfig("UploadStep") as typeof themeOverrides["components"]["UploadStep"]["baseStyle"]
1717
const { translations, fields } = useRsi()
1818
const handleOnContinue = useCallback(
19-
async (data) => {
19+
async (data, file) => {
2020
setIsLoading(true)
21-
await onContinue(data)
21+
await onContinue(data, file)
2222
setIsLoading(false)
2323
},
2424
[onContinue],

src/steps/UploadStep/components/DropZone.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { readFileAsync } from "../utils/readFilesAsync"
88
import type { themeOverrides } from "../../../theme"
99

1010
type DropZoneProps = {
11-
onContinue: (data: XLSX.WorkBook) => void
11+
onContinue: (data: XLSX.WorkBook, file: File) => void
1212
isLoading: boolean
1313
}
1414

@@ -46,7 +46,7 @@ export const DropZone = ({ onContinue, isLoading }: DropZoneProps) => {
4646
dense: true,
4747
})
4848
setLoading(false)
49-
onContinue(workbook)
49+
onContinue(workbook, file)
5050
},
5151
})
5252

src/steps/ValidationStep/ValidationStep.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import type { RowsChangeData } from "react-data-grid"
1313

1414
type Props<T extends string> = {
1515
initialData: Data<T>[]
16+
file: File
1617
}
1718

18-
export const ValidationStep = <T extends string>({ initialData }: Props<T>) => {
19+
export const ValidationStep = <T extends string>({ initialData, file }: Props<T>) => {
1920
const { translations, fields, onClose, onSubmit, rowHook, tableHook } = useRsi<T>()
2021
const styles = useStyleConfig("ValidationStep") as typeof themeOverrides["components"]["ValidationStep"]["baseStyle"]
2122

@@ -92,7 +93,7 @@ export const ValidationStep = <T extends string>({ initialData }: Props<T>) => {
9293
},
9394
{ validData: [] as Data<T>[], invalidData: [] as Data<T>[], all: data },
9495
)
95-
onSubmit(calculatedData)
96+
onSubmit(calculatedData, file)
9697
setShowSubmitAlert(false)
9798
onClose()
9899
}

src/steps/ValidationStep/stories/Validation.stories.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ export default {
1111
},
1212
}
1313

14+
const file = new File([""], "file.csv")
15+
1416
export const Basic = () => (
1517
<Providers theme={defaultTheme} rsiValues={mockRsiValues}>
1618
<ModalWrapper isOpen={true} onClose={() => {}}>
17-
<ValidationStep initialData={editableTableInitialData} />
19+
<ValidationStep initialData={editableTableInitialData} file={file} />
1820
</ModalWrapper>
1921
</Providers>
2022
)

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ const getFilterSwitch = () =>
1919
name: "Show only rows with errors",
2020
})
2121

22+
const file = new File([""], "file.csv")
23+
2224
describe("Validation step tests", () => {
2325
test("Submit data", async () => {
2426
const onSubmit = jest.fn()
2527
render(
2628
<Providers theme={defaultTheme} rsiValues={{ ...mockValues, onSubmit: onSubmit }}>
2729
<ModalWrapper isOpen={true} onClose={() => {}}>
28-
<ValidationStep initialData={[]} />
30+
<ValidationStep initialData={[]} file={file} />
2931
</ModalWrapper>
3032
</Providers>,
3133
)
@@ -37,7 +39,7 @@ describe("Validation step tests", () => {
3739
userEvent.click(finishButton)
3840

3941
await waitFor(() => {
40-
expect(onSubmit).toBeCalled()
42+
expect(onSubmit).toBeCalledWith({ all: [], invalidData: [], validData: [] }, file)
4143
})
4244
})
4345

@@ -69,7 +71,7 @@ describe("Validation step tests", () => {
6971
render(
7072
<Providers theme={defaultTheme} rsiValues={{ ...mockValues, fields }}>
7173
<ModalWrapper isOpen={true} onClose={() => {}}>
72-
<ValidationStep initialData={initialData} />
74+
<ValidationStep initialData={initialData} file={file} />
7375
</ModalWrapper>
7476
</Providers>,
7577
)
@@ -123,7 +125,7 @@ describe("Validation step tests", () => {
123125
render(
124126
<Providers theme={defaultTheme} rsiValues={{ ...mockValues, fields, onSubmit }}>
125127
<ModalWrapper isOpen={true} onClose={() => {}}>
126-
<ValidationStep initialData={initialData} />
128+
<ValidationStep initialData={initialData} file={file} />
127129
</ModalWrapper>
128130
</Providers>,
129131
)
@@ -197,7 +199,7 @@ describe("Validation step tests", () => {
197199
render(
198200
<Providers theme={defaultTheme} rsiValues={{ ...mockValues, fields }}>
199201
<ModalWrapper isOpen={true} onClose={() => {}}>
200-
<ValidationStep initialData={initialData} />
202+
<ValidationStep initialData={initialData} file={file} />
201203
</ModalWrapper>
202204
</Providers>,
203205
)
@@ -244,7 +246,7 @@ describe("Validation step tests", () => {
244246
render(
245247
<Providers theme={defaultTheme} rsiValues={{ ...mockValues, fields }}>
246248
<ModalWrapper isOpen={true} onClose={() => {}}>
247-
<ValidationStep initialData={initialData} />
249+
<ValidationStep initialData={initialData} file={file} />
248250
</ModalWrapper>
249251
</Providers>,
250252
)
@@ -288,7 +290,7 @@ describe("Validation step tests", () => {
288290
render(
289291
<Providers theme={defaultTheme} rsiValues={{ ...mockValues, fields }}>
290292
<ModalWrapper isOpen={true} onClose={() => {}}>
291-
<ValidationStep initialData={initialData} />
293+
<ValidationStep initialData={initialData} file={file} />
292294
</ModalWrapper>
293295
</Providers>,
294296
)
@@ -345,7 +347,7 @@ describe("Validation step tests", () => {
345347
render(
346348
<Providers theme={defaultTheme} rsiValues={{ ...mockValues, fields }}>
347349
<ModalWrapper isOpen={true} onClose={() => {}}>
348-
<ValidationStep initialData={initialData} />
350+
<ValidationStep initialData={initialData} file={file} />
349351
</ModalWrapper>
350352
</Providers>,
351353
)
@@ -430,7 +432,7 @@ describe("Validation step tests", () => {
430432
}}
431433
>
432434
<ModalWrapper isOpen={true} onClose={() => {}}>
433-
<ValidationStep initialData={initialData} />
435+
<ValidationStep initialData={initialData} file={file} />
434436
</ModalWrapper>
435437
</Providers>,
436438
)
@@ -589,7 +591,7 @@ describe("Validation step tests", () => {
589591
}}
590592
>
591593
<ModalWrapper isOpen={true} onClose={() => {}}>
592-
<ValidationStep initialData={initialData} />
594+
<ValidationStep initialData={initialData} file={file} />
593595
</ModalWrapper>
594596
</Providers>,
595597
)
@@ -650,7 +652,7 @@ describe("Validation step tests", () => {
650652
}}
651653
>
652654
<ModalWrapper isOpen={true} onClose={() => {}}>
653-
<ValidationStep initialData={initialData} />
655+
<ValidationStep initialData={initialData} file={file} />
654656
</ModalWrapper>
655657
</Providers>,
656658
)
@@ -712,7 +714,7 @@ describe("Validation step tests", () => {
712714
}}
713715
>
714716
<ModalWrapper isOpen={true} onClose={() => {}}>
715-
<ValidationStep initialData={initialData} />
717+
<ValidationStep initialData={initialData} file={file} />
716718
</ModalWrapper>
717719
</Providers>,
718720
)
@@ -775,7 +777,7 @@ describe("Validation step tests", () => {
775777
}}
776778
>
777779
<ModalWrapper isOpen={true} onClose={() => {}}>
778-
<ValidationStep initialData={initialData} />
780+
<ValidationStep initialData={initialData} file={file} />
779781
</ModalWrapper>
780782
</Providers>,
781783
)

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type RsiProps<T extends string> = {
2222
// Runs after column matching and on entry change
2323
tableHook?: TableHook<T>
2424
// Function called after user finishes the flow
25-
onSubmit: (data: Result<T>) => void
25+
onSubmit: (data: Result<T>, file: File) => void
2626
// Allows submitting with errors. Default: true
2727
allowInvalidSubmit?: boolean
2828
// Translations for each text

0 commit comments

Comments
 (0)