made one function for converter

This commit is contained in:
Anish Roy
2023-03-10 14:38:26 +05:30
parent 2a04ee1bd2
commit 4e92dd39b0
5 changed files with 13 additions and 56 deletions

View File

@@ -7,7 +7,7 @@ import EmptyState from "@src/components/EmptyState";
import { FieldType } from "@src/constants/fields";
import { getFieldProp } from "@src/components/fields";
import { DEFAULT_ROW_HEIGHT } from "@src/components/Table";
import mockValue from "./mockValue";
import useConverter from "@src/components/TableModals/ImportCsvWizard/useConverter";
export interface ICellProps
extends Partial<
@@ -26,14 +26,14 @@ export interface ICellProps
export default function Cell({
field,
type,
value,
value: value_,
name,
rowHeight = DEFAULT_ROW_HEIGHT,
...props
}: ICellProps) {
const tableCell = type ? getFieldProp("TableCell", type) : null;
value = mockValue(value, type);
const { checkAndConvert } = useConverter();
const value = checkAndConvert(value_, type);
return (
<StyledTable>
<StyledCell

View File

@@ -1,20 +0,0 @@
export const fileValueConverter = (value: any) => {
if (!value) return [];
if (Array.isArray(value)) return value;
if (typeof value === "string") {
return value
.split(",")
.map((url) => {
url = url.trim();
if (url !== "") {
return {
downloadURL: url,
name: +new Date() + "-" + Math.round(Math.random() * 1000),
};
}
return null;
})
.filter((mockValue) => mockValue !== null);
}
return [];
};

View File

@@ -1,20 +0,0 @@
import { FieldType } from "@src/constants/fields";
import { fileValueConverter } from "./file";
import { referenceValueConverter } from "./reference";
export const VALUE_CONVERTERS: Partial<{
[key in FieldType]: (value: any) => any;
}> = {
[FieldType.image]: fileValueConverter,
[FieldType.reference]: referenceValueConverter,
[FieldType.file]: fileValueConverter,
};
export default function convert(value: any, type: FieldType) {
const converter = VALUE_CONVERTERS[type];
if (converter) {
return converter(value);
}
return value;
}

View File

@@ -1,12 +0,0 @@
export const referenceValueConverter = (value: any) => {
if (typeof value === "string") {
if (
value !== "" &&
value.split("/").length > 0 &&
value.split("/").length % 2 === 0
) {
return { path: value };
}
}
return value;
};

View File

@@ -51,10 +51,19 @@ export default function useConverter() {
}
};
const checkAndConvert = (value: any, type: FieldType) => {
if (needsConverter(type)) {
const converter = getConverter(type);
if (converter) return converter(value);
}
return value;
};
return {
needsConverter,
referenceConverter,
imageOrFileConverter,
getConverter,
checkAndConvert,
};
}