Skip to content

Commit 4b4a2f5

Browse files
committed
merge hiveteams:react-spreadsheet-import:feat/navigation <- UgnisSoftware:react-spreadsheet-import:master
2 parents 116383f + a192098 commit 4b4a2f5

9 files changed

Lines changed: 423 additions & 298 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-spreadsheet-import",
3-
"version": "4.2.1",
3+
"version": "4.3.0",
44
"description": "React spreadsheet import for xlsx and csv files with column matching and validation",
55
"main": "./dist-commonjs/index.js",
66
"module": "./dist/index.js",

src/steps/UploadFlow.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { SelectHeaderStep } from "./SelectHeaderStep/SelectHeaderStep"
66
import { SelectSheetStep } from "./SelectSheetStep/SelectSheetStep"
77
import { mapWorkbook } from "../utils/mapWorkbook"
88
import { ValidationStep } from "./ValidationStep/ValidationStep"
9+
import { addErrorsAndRunHooks } from "./ValidationStep/utils/dataMutations"
910
import { MatchColumnsStep } from "./MatchColumnsStep/MatchColumnsStep"
1011
import { exceedsMaxRecords } from "../utils/exceedsMaxRecords"
1112
import { useRsi } from "../hooks/useRsi"
@@ -47,8 +48,17 @@ interface Props {
4748
}
4849

4950
export const UploadFlow = ({ state, onNext, onBack }: Props) => {
51+
const {
52+
maxRecords,
53+
translations,
54+
uploadStepHook,
55+
selectHeaderStepHook,
56+
matchColumnsStepHook,
57+
fields,
58+
rowHook,
59+
tableHook,
60+
} = useRsi()
5061
const [uploadedFile, setUploadedFile] = useState<File | null>(null)
51-
const { maxRecords, translations, uploadStepHook, selectHeaderStepHook, matchColumnsStepHook } = useRsi()
5262
const toast = useToast()
5363
const errorToast = useCallback(
5464
(description: string) => {
@@ -140,9 +150,10 @@ export const UploadFlow = ({ state, onNext, onBack }: Props) => {
140150
onContinue={async (values, rawData, columns) => {
141151
try {
142152
const data = await matchColumnsStepHook(values, rawData, columns)
153+
const dataWithMeta = await addErrorsAndRunHooks(data, fields, rowHook, tableHook)
143154
onNext({
144155
type: StepType.validateData,
145-
data,
156+
data: dataWithMeta,
146157
})
147158
} catch (e) {
148159
errorToast((e as Error).message)

src/steps/ValidationStep/ValidationStep.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { themeOverrides } from "../../theme"
1212
import type { RowsChangeData } from "react-data-grid"
1313

1414
type Props<T extends string> = {
15-
initialData: Data<T>[]
15+
initialData: (Data<T> & Meta)[]
1616
file: File
1717
onBack?: () => void
1818
}
@@ -23,22 +23,21 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
2323
"ValidationStep",
2424
) as (typeof themeOverrides)["components"]["ValidationStep"]["baseStyle"]
2525

26-
const [data, setData] = useState<(Data<T> & Meta)[]>(
27-
useMemo(
28-
() => addErrorsAndRunHooks<T>(initialData, fields, rowHook, tableHook),
29-
// eslint-disable-next-line react-hooks/exhaustive-deps
30-
[],
31-
),
32-
)
26+
const [data, setData] = useState<(Data<T> & Meta)[]>(initialData)
27+
3328
const [selectedRows, setSelectedRows] = useState<ReadonlySet<number | string>>(new Set())
3429
const [filterByErrors, setFilterByErrors] = useState(false)
3530
const [showSubmitAlert, setShowSubmitAlert] = useState(false)
3631

3732
const updateData = useCallback(
38-
(rows: typeof data) => {
39-
setData(addErrorsAndRunHooks<T>(rows, fields, rowHook, tableHook))
33+
async (rows: typeof data, indexes?: number[]) => {
34+
// Check if hooks are async - if they are we want to apply changes optimistically for better UX
35+
if (rowHook?.constructor.name === "AsyncFunction" || tableHook?.constructor.name === "AsyncFunction") {
36+
setData(rows)
37+
}
38+
addErrorsAndRunHooks<T>(rows, fields, rowHook, tableHook, indexes).then((data) => setData(data))
4039
},
41-
[setData, rowHook, tableHook, fields],
40+
[rowHook, tableHook, fields],
4241
)
4342

4443
const deleteSelectedRows = () => {
@@ -49,16 +48,17 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
4948
}
5049
}
5150

52-
const updateRow = useCallback(
51+
const updateRows = useCallback(
5352
(rows: typeof data, changedData?: RowsChangeData<(typeof data)[number]>) => {
5453
const changes = changedData?.indexes.reduce((acc, index) => {
5554
// when data is filtered val !== actual index in data
5655
const realIndex = data.findIndex((value) => value.__index === rows[index].__index)
5756
acc[realIndex] = rows[index]
5857
return acc
5958
}, {} as Record<number, (typeof data)[number]>)
59+
const realIndexes = changes && Object.keys(changes).map((index) => Number(index))
6060
const newData = Object.assign([], data, changes)
61-
updateData(newData)
61+
updateData(newData, realIndexes)
6262
},
6363
[data, updateData],
6464
)
@@ -137,7 +137,7 @@ export const ValidationStep = <T extends string>({ initialData, file, onBack }:
137137
<Table
138138
rowKeyGetter={rowKeyGetter}
139139
rows={tableData}
140-
onRowsChange={updateRow}
140+
onRowsChange={updateRows}
141141
columns={columns}
142142
selectedRows={selectedRows}
143143
onSelectedRowsChange={setSelectedRows}

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ValidationStep } from "../ValidationStep"
33
import { Providers } from "../../../components/Providers"
44
import { defaultTheme } from "../../../ReactSpreadsheetImport"
55
import { ModalWrapper } from "../../../components/ModalWrapper"
6+
import { addErrorsAndRunHooks } from "../utils/dataMutations"
67

78
export default {
89
title: "Validation Step",
@@ -12,11 +13,14 @@ export default {
1213
}
1314

1415
const file = new File([""], "file.csv")
16+
const data = await addErrorsAndRunHooks(editableTableInitialData, mockRsiValues.fields)
1517

16-
export const Basic = () => (
17-
<Providers theme={defaultTheme} rsiValues={mockRsiValues}>
18-
<ModalWrapper isOpen={true} onClose={() => {}}>
19-
<ValidationStep initialData={editableTableInitialData} file={file} />
20-
</ModalWrapper>
21-
</Providers>
22-
)
18+
export const Basic = () => {
19+
return (
20+
<Providers theme={defaultTheme} rsiValues={mockRsiValues}>
21+
<ModalWrapper isOpen={true} onClose={() => {}}>
22+
<ValidationStep initialData={data} file={file} />
23+
</ModalWrapper>
24+
</Providers>
25+
)
26+
}

0 commit comments

Comments
 (0)