import CSV: parse dates (#219)

This commit is contained in:
Sidney Alcantara
2020-10-31 21:55:31 +11:00
parent a2e4d5e008
commit ec51b6907d
3 changed files with 22 additions and 7 deletions

View File

@@ -17,7 +17,6 @@ import Column from "../Column";
import Cell from "../Cell";
import FieldsDropdown from "components/Table/ColumnMenu/FieldsDropdown";
import { useFiretableContext } from "contexts/firetableContext";
import { FieldType } from "constants/fields";
import { SELECTABLE_TYPES } from "../ImportWizard/utils";

View File

@@ -1,6 +1,7 @@
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";
@@ -9,6 +10,7 @@ import Column from "../Column";
import Cell from "../Cell";
import { useFiretableContext } from "contexts/firetableContext";
import { FieldType } from "constants/fields";
const useStyles = makeStyles((theme) =>
createStyles({
@@ -103,7 +105,11 @@ export default function Step4Preview({ csvData, config }: IStepProps) {
<Cell
key={csvKey + i}
field={csvKey}
value={row[csvKey]}
value={
type === FieldType.date || type === FieldType.dateTime
? parseJSON(row[csvKey]).getTime()
: row[csvKey]
}
type={type}
name={name}
/>

View File

@@ -1,5 +1,7 @@
import React, { useState, useEffect } from "react";
import _mergeWith from "lodash/mergeWith";
import _find from "lodash/find";
import { parseJSON } from "date-fns";
import { useTheme, useMediaQuery } from "@material-ui/core";
@@ -10,6 +12,7 @@ import Step3Preview from "./Step3Preview";
import { ColumnConfig } from "hooks/useFiretable/useTableConfig";
import { useFiretableContext } from "contexts/firetableContext";
import { FieldType } from "constants/fields";
export type CsvConfig = {
pairs: { csvKey: string; columnKey: string }[];
@@ -68,17 +71,24 @@ export default function ImportCsvWizard({
if (tableState?.rows.length === 0) return null;
const handleFinish = () => {
if (!tableActions || !csvData) return;
if (!tableState || !tableActions || !csvData) return;
// Add any new columns to the end
config.newColumns.forEach((col) =>
tableActions.column.add(col.name, col.type, col)
);
// Add all new rows
csvData.rows.forEach((row) => {
const newRow = config.pairs.reduce(
(a, c) => ({ ...a, [c.columnKey]: row[c.csvKey] }),
{}
);
const newRow = 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];
return { ...a, [pair.columnKey]: value };
}, {});
tableActions.row.add(newRow);
});
// Close wizard