Files
rowy/src/components/TableModals/ImportExistingWizard/ImportExistingWizard.tsx

144 lines
4.3 KiB
TypeScript
Raw Normal View History

2022-06-07 12:00:58 +10:00
import { useState, useEffect, useCallback } from "react";
import { useAtom, useSetAtom } from "jotai";
2022-06-08 13:10:50 +10:00
import { RESET } from "jotai/utils";
2022-06-07 12:00:58 +10:00
import { merge } from "lodash-es";
import { ITableModalProps } from "@src/components/TableModals";
2022-06-07 12:00:58 +10:00
import { useTheme, useMediaQuery, Typography } from "@mui/material";
import WizardDialog from "@src/components/TableModals/WizardDialog";
2022-06-07 12:00:58 +10:00
import Step1Columns from "./Step1Columns";
import Step2Rename from "./Step2Rename";
import Step3Types from "./Step3Types";
import Step4Preview from "./Step4Preview";
import {
tableScope,
2022-06-08 13:10:50 +10:00
updateTableSchemaAtom,
2022-06-07 12:00:58 +10:00
tableFiltersAtom,
tableSortsAtom,
2022-06-07 12:00:58 +10:00
tableRowsAtom,
2022-06-08 13:10:50 +10:00
tableModalAtom,
2022-06-07 12:00:58 +10:00
} from "@src/atoms/tableScope";
import { TableSchema, ColumnConfig } from "@src/types/table";
export type TableColumnsConfig = NonNullable<TableSchema["columns"]>;
2022-06-08 13:10:50 +10:00
export type ImportExistingWizardRef = {
2022-06-07 12:00:58 +10:00
open: boolean;
setOpen: React.Dispatch<React.SetStateAction<boolean>>;
};
export interface IStepProps {
config: TableColumnsConfig;
setConfig: React.Dispatch<React.SetStateAction<TableColumnsConfig>>;
updateConfig: (value: Partial<ColumnConfig>) => void;
isXs: boolean;
}
2022-06-08 13:10:50 +10:00
export default function ImportExistingWizard({ onClose }: ITableModalProps) {
const [updateTableSchema] = useAtom(updateTableSchemaAtom, tableScope);
2022-06-07 12:00:58 +10:00
const setTableFilters = useSetAtom(tableFiltersAtom, tableScope);
const setTableSorts = useSetAtom(tableSortsAtom, tableScope);
2022-06-07 12:00:58 +10:00
const [tableRows] = useAtom(tableRowsAtom, tableScope);
2022-06-08 13:10:50 +10:00
const setTableModal = useSetAtom(tableModalAtom, tableScope);
2022-06-07 12:00:58 +10:00
const theme = useTheme();
const isXs = useMediaQuery(theme.breakpoints.down("sm"));
const [config, setConfig] = useState<TableColumnsConfig>({});
const updateConfig: IStepProps["updateConfig"] = useCallback((value) => {
setConfig((prev) => ({ ...merge(prev, value) }));
}, []);
// Reset table filters and sorts on open
2022-06-07 12:00:58 +10:00
useEffect(() => {
setTableFilters([]);
setTableSorts([]);
}, [setTableFilters, setTableSorts]);
2022-06-07 12:00:58 +10:00
2022-06-08 13:10:50 +10:00
if (tableRows.length === 0 || !updateTableSchema) {
setTableModal(RESET);
return null;
}
2022-06-07 12:00:58 +10:00
const handleFinish = () => {
2022-06-08 13:10:50 +10:00
updateTableSchema!({ columns: config });
onClose();
2022-06-07 12:00:58 +10:00
};
return (
<WizardDialog
open
onClose={onClose}
2022-06-08 13:10:50 +10:00
title="Import existing data"
2022-06-07 12:00:58 +10:00
steps={[
{
title: "Choose columns",
description: (
<>
<Typography gutterBottom>
It looks like you already have data in this table. You can
import and view the data by setting up columns for this table.
</Typography>
<Typography gutterBottom>
Start by choosing which columns you want to display, then sort
your columns.
</Typography>
</>
),
content: (
<Step1Columns
config={config}
setConfig={setConfig}
updateConfig={updateConfig}
isXs={isXs}
/>
),
disableNext: Object.keys(config).length === 0,
},
{
title: "Rename columns",
description:
"Rename your table columns with user-friendly names. These changes will not update the field names in your database.",
content: (
<Step2Rename
config={config}
setConfig={setConfig}
updateConfig={updateConfig}
isXs={isXs}
/>
),
},
{
title: "Set column types",
description:
"Set the type of each column to display your data correctly. Some column types have been suggested based on your data.",
content: (
<Step3Types
config={config}
setConfig={setConfig}
updateConfig={updateConfig}
isXs={isXs}
/>
),
},
{
title: "Preview",
description:
"Preview your data with your configured columns. You can change column types by clicking “Edit type” from the column menu at any time.",
content: (
<Step4Preview
config={config}
setConfig={setConfig}
updateConfig={updateConfig}
isXs={isXs}
/>
),
},
]}
onFinish={handleFinish}
/>
);
}