diff --git a/www/src/components/Table/ColumnHeader.tsx b/www/src/components/Table/ColumnHeader.tsx index 49741bc9..f3c03093 100644 --- a/www/src/components/Table/ColumnHeader.tsx +++ b/www/src/components/Table/ColumnHeader.tsx @@ -259,19 +259,23 @@ export default function DraggableHeaderRenderer({ )} - {userClaims?.roles?.includes("ADMIN") && ( - - - - - - )} + {userClaims?.roles?.includes("ADMIN") || + (userClaims?.roles?.includes("OPS") && + [FieldType.multiSelect, FieldType.singleSelect].includes( + (column as any).type + ) && ( + + + + + + ))} ); // return ( diff --git a/www/src/components/Wizards/ImportCsvWizard/Step3Preview.tsx b/www/src/components/Wizards/ImportCsvWizard/Step3Preview.tsx index c5888c98..beffd178 100644 --- a/www/src/components/Wizards/ImportCsvWizard/Step3Preview.tsx +++ b/www/src/components/Wizards/ImportCsvWizard/Step3Preview.tsx @@ -1,7 +1,6 @@ import React from "react"; import { ScrollSync, ScrollSyncPane } from "react-scroll-sync"; import _find from "lodash/find"; -import { parseJSON } from "date-fns"; import { makeStyles, createStyles, Grid } from "@material-ui/core"; @@ -83,6 +82,7 @@ export default function Step4Preview({ csvData, config }: IStepProps) { const columns = config.pairs.map(({ csvKey, columnKey }) => ({ csvKey, + columnKey, ...(tableState!.columns[columnKey] ?? _find(config.newColumns, { key: columnKey }) ?? {}), @@ -105,17 +105,13 @@ export default function Step4Preview({ csvData, config }: IStepProps) { - {columns.map(({ csvKey, name, type }) => ( + {columns.map(({ csvKey, name, columnKey, type }) => ( {csvData.rows.map((row, i) => ( diff --git a/www/src/components/Wizards/ImportCsvWizard/index.tsx b/www/src/components/Wizards/ImportCsvWizard/index.tsx index 85cd13c9..995c2e92 100644 --- a/www/src/components/Wizards/ImportCsvWizard/index.tsx +++ b/www/src/components/Wizards/ImportCsvWizard/index.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from "react"; +import React, { useState, useMemo } from "react"; import _mergeWith from "lodash/mergeWith"; import _find from "lodash/find"; import { parseJSON } from "date-fns"; @@ -21,6 +21,7 @@ import { ColumnConfig } from "hooks/useFiretable/useTableConfig"; import { useFiretableContext } from "contexts/FiretableContext"; import { FieldType } from "constants/fields"; import { useSnackContext } from "contexts/SnackContext"; +import { getFieldProp } from "components/fields"; export type CsvConfig = { pairs: { csvKey: string; columnKey: string }[]; @@ -67,24 +68,32 @@ export default function ImportCsvWizard({ })); }; - const handleFinish = () => { - if (!tableState || !tableActions || !csvData) return; - openSnackbar({ message: "Importing data…" }); - // Add all new rows — synchronous - for (const row of csvData.rows) { - const newRow = config.pairs.reduce((a, pair) => { + const parsedRows: any[] = useMemo(() => { + if (!tableState || !tableActions || !csvData) return []; + return csvData.rows.map((row) => + config.pairs.reduce((a, pair) => { const matchingColumn = tableState.columns[pair.columnKey] ?? _find(config.newColumns, { key: pair.columnKey }); - const value = - matchingColumn.type === FieldType.date || - matchingColumn.type === FieldType.dateTime - ? parseJSON(row[pair.csvKey]) - : row[pair.csvKey]; + console.log({ type: matchingColumn.type }); + const csvFieldParser = getFieldProp( + "csvImportParser", + matchingColumn.type + ); + const value = csvFieldParser + ? csvFieldParser(row[pair.csvKey]) + : row[pair.csvKey]; return { ...a, [pair.columnKey]: value }; - }, {}); - tableActions.row.add(newRow); - } + }, {}) + ); + }, [csvData, tableState, tableActions, config]); + + const handleFinish = () => { + if (!tableState || !tableActions || !parsedRows) return; + openSnackbar({ message: "Importing data…" }); + // Add all new rows — synchronous + parsedRows?.forEach((newRow) => tableActions.row.add(newRow)); + // Add any new columns to the end for (const col of config.newColumns) { tableActions.column.add(col.name, col.type, col); @@ -172,7 +181,7 @@ export default function ImportCsvWizard({ "Preview your data with your configured columns. You can change column types by clicking “Edit Type” from the column menu at any time.", content: ( { + if (["YES", "TRUE", "1"].includes(value.toUpperCase())) return true; + else if (["NO", "FALSE", "0"].includes(value.toUpperCase())) return false; + else return null; + }, SideDrawerField, }; export default config;