Skip to content

Commit

Permalink
Merge pull request rowyio#1118 from iamanishroy/import-export-csv-fixes
Browse files Browse the repository at this point in the history
ROWY-202: Change Export CSV date format so it can be parsed by Import CSV
  • Loading branch information
shamsmosowi authored Feb 17, 2023
2 parents 7a99cb9 + 98b316e commit 0bde7dc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/components/TableModals/ImportExistingWizard/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,24 @@ export const REGEX_URL =
export const REGEX_HTML = /<\/?[a-z][\s\S]*>/;

const inferTypeFromValue = (value: any) => {
// by default the type of value is string, so trying to convert it to JSON/Object.
try {
value = JSON.parse(value);
} catch (e) {}
if (!value || typeof value === "function") return;

if (Array.isArray(value) && typeof value[0] === "string")
return FieldType.multiSelect;
if (typeof value === "boolean") return FieldType.checkbox;
if (isDate(value)) return FieldType.dateTime;
// trying to convert the value to date
if (+new Date(value)) {
// date and time are separated by a blank space, checking if time present.
if (value.split(" ").length > 1) {
return FieldType.dateTime;
}
return FieldType.date;
}

if (typeof value === "object") {
if ("hex" in value && "rgb" in value) return FieldType.color;
Expand Down Expand Up @@ -71,6 +83,7 @@ const inferTypeFromValue = (value: any) => {
export const suggestType = (data: { [key: string]: any }[], field: string) => {
const results: Record<string, number> = {};

// console.log(data)
data.forEach((row) => {
const result = inferTypeFromValue(row[field]);
if (!result) return;
Expand Down
5 changes: 2 additions & 3 deletions src/components/fields/Date/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ export const config: IFieldConfig = {
SideDrawerField,
filter: { operators: filterOperators, valueFormatter },
settings: Settings,
csvImportParser: (value, config) =>
parse(value, config?.format ?? DATE_FORMAT, new Date()),
csvImportParser: (value, config) => parse(value, DATE_FORMAT, new Date()),
csvExportFormatter: (value: any, config?: any) =>
format(value.toDate(), config?.format ?? DATE_FORMAT),
format(value.toDate(), DATE_FORMAT),
};
export default config;

Expand Down
6 changes: 3 additions & 3 deletions src/components/fields/DateTime/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { lazy } from "react";
import { IFieldConfig, FieldType } from "@src/components/fields/types";
import withRenderTableCell from "@src/components/Table/TableCell/withRenderTableCell";
import { parseJSON, format } from "date-fns";
import { format } from "date-fns";
import { DATE_TIME_FORMAT } from "@src/constants/dates";

import DateTimeIcon from "@mui/icons-material/AccessTime";
Expand Down Expand Up @@ -46,9 +46,9 @@ export const config: IFieldConfig = {
customInput: FilterCustomInput,
},
settings: Settings,
csvImportParser: (value) => parseJSON(value).getTime(),
csvImportParser: (value) => new Date(value),
csvExportFormatter: (value: any, config?: any) =>
format(value.toDate(), config?.format ?? DATE_TIME_FORMAT),
format(value.toDate(), DATE_TIME_FORMAT),
};
export default config;

Expand Down

0 comments on commit 0bde7dc

Please sign in to comment.