mirror of
https://github.com/rowyio/rowy.git
synced 2026-07-13 05:48:53 +02:00
import CSV: parse dates (#219)
This commit is contained in:
@@ -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";
|
||||
|
||||
|
||||
@@ -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}
|
||||
/>
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user