Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow row and table hooks to be async #212

Merged
merged 16 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/steps/UploadFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SelectHeaderStep } from "./SelectHeaderStep/SelectHeaderStep"
import { SelectSheetStep } from "./SelectSheetStep/SelectSheetStep"
import { mapWorkbook } from "../utils/mapWorkbook"
import { ValidationStep } from "./ValidationStep/ValidationStep"
import { addErrorsAndRunHooks } from "./ValidationStep/utils/dataMutations"
import { MatchColumnsStep } from "./MatchColumnsStep/MatchColumnsStep"
import { exceedsMaxRecords } from "../utils/exceedsMaxRecords"
import { useRsi } from "../hooks/useRsi"
Expand Down Expand Up @@ -45,10 +46,19 @@ interface Props {
}

export const UploadFlow = ({ nextStep }: Props) => {
const { initialStepState } = useRsi()
const {
initialStepState,
maxRecords,
translations,
uploadStepHook,
selectHeaderStepHook,
matchColumnsStepHook,
fields,
rowHook,
tableHook,
} = useRsi()
const [state, setState] = useState<StepState>(initialStepState || { type: StepType.upload })
const [uploadedFile, setUploadedFile] = useState<File | null>(null)
const { maxRecords, translations, uploadStepHook, selectHeaderStepHook, matchColumnsStepHook } = useRsi()
const toast = useToast()
const errorToast = useCallback(
(description: string) => {
Expand Down Expand Up @@ -141,9 +151,10 @@ export const UploadFlow = ({ nextStep }: Props) => {
onContinue={async (values, rawData, columns) => {
try {
const data = await matchColumnsStepHook(values, rawData, columns)
const dataWithMeta = await addErrorsAndRunHooks(data, fields, rowHook, tableHook)
setState({
type: StepType.validateData,
data,
data: dataWithMeta,
})
nextStep()
} catch (e) {
Expand Down
28 changes: 14 additions & 14 deletions src/steps/ValidationStep/ValidationStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { themeOverrides } from "../../theme"
import type { RowsChangeData } from "react-data-grid"

type Props<T extends string> = {
initialData: Data<T>[]
initialData: (Data<T> & Meta)[]
file: File
}

Expand All @@ -22,22 +22,21 @@ export const ValidationStep = <T extends string>({ initialData, file }: Props<T>
"ValidationStep",
) as (typeof themeOverrides)["components"]["ValidationStep"]["baseStyle"]

const [data, setData] = useState<(Data<T> & Meta)[]>(
useMemo(
() => addErrorsAndRunHooks<T>(initialData, fields, rowHook, tableHook),
// eslint-disable-next-line react-hooks/exhaustive-deps
[],
),
)
const [data, setData] = useState<(Data<T> & Meta)[]>(initialData)

const [selectedRows, setSelectedRows] = useState<ReadonlySet<number | string>>(new Set())
const [filterByErrors, setFilterByErrors] = useState(false)
const [showSubmitAlert, setShowSubmitAlert] = useState(false)

const updateData = useCallback(
(rows: typeof data) => {
setData(addErrorsAndRunHooks<T>(rows, fields, rowHook, tableHook))
async (rows: typeof data, indexes?: number[]) => {
// Check if hooks are async - if they are we want to apply changes optimistically for better UX
if (rowHook?.constructor.name === "AsyncFunction" || tableHook?.constructor.name === "AsyncFunction") {
setData(rows)
}
addErrorsAndRunHooks<T>(rows, fields, rowHook, tableHook, indexes).then((data) => setData(data))
},
[setData, rowHook, tableHook, fields],
[rowHook, tableHook, fields],
)

const deleteSelectedRows = () => {
Expand All @@ -48,16 +47,17 @@ export const ValidationStep = <T extends string>({ initialData, file }: Props<T>
}
}

const updateRow = useCallback(
const updateRows = useCallback(
(rows: typeof data, changedData?: RowsChangeData<(typeof data)[number]>) => {
const changes = changedData?.indexes.reduce((acc, index) => {
// when data is filtered val !== actual index in data
const realIndex = data.findIndex((value) => value.__index === rows[index].__index)
acc[realIndex] = rows[index]
return acc
}, {} as Record<number, (typeof data)[number]>)
const realIndexes = changes == null ? undefined : Object.keys(changes).map((index) => Number(index))
masiulis marked this conversation as resolved.
Show resolved Hide resolved
const newData = Object.assign([], data, changes)
updateData(newData)
updateData(newData, realIndexes)
},
[data, updateData],
)
Expand Down Expand Up @@ -136,7 +136,7 @@ export const ValidationStep = <T extends string>({ initialData, file }: Props<T>
<Table
rowKeyGetter={rowKeyGetter}
rows={tableData}
onRowsChange={updateRow}
onRowsChange={updateRows}
columns={columns}
selectedRows={selectedRows}
onSelectedRowsChange={setSelectedRows}
Expand Down
18 changes: 11 additions & 7 deletions src/steps/ValidationStep/stories/Validation.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ValidationStep } from "../ValidationStep"
import { Providers } from "../../../components/Providers"
import { defaultTheme } from "../../../ReactSpreadsheetImport"
import { ModalWrapper } from "../../../components/ModalWrapper"
import { addErrorsAndRunHooks } from "../utils/dataMutations"

export default {
title: "Validation Step",
Expand All @@ -12,11 +13,14 @@ export default {
}

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

export const Basic = () => (
<Providers theme={defaultTheme} rsiValues={mockRsiValues}>
<ModalWrapper isOpen={true} onClose={() => {}}>
<ValidationStep initialData={editableTableInitialData} file={file} />
</ModalWrapper>
</Providers>
)
export const Basic = () => {
return (
<Providers theme={defaultTheme} rsiValues={mockRsiValues}>
<ModalWrapper isOpen={true} onClose={() => {}}>
<ValidationStep initialData={data} file={file} />
</ModalWrapper>
</Providers>
)
}
Loading
Loading