mirror of
https://github.com/rowyio/rowy.git
synced 2025-12-29 00:16:39 +01:00
made one function for converter
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 [];
|
||||
};
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user