From 9cd9f1e9dfb140fa103fc2da54321715c5fc705b Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 4 Jun 2021 16:16:28 +0800 Subject: fix: Fix change post property time init value bug. --- FrontEnd/src/app/views/common/OperationDialog.tsx | 68 +++++++++++++++-------- 1 file changed, 45 insertions(+), 23 deletions(-) (limited to 'FrontEnd/src') diff --git a/FrontEnd/src/app/views/common/OperationDialog.tsx b/FrontEnd/src/app/views/common/OperationDialog.tsx index 0ede42e5..ac4c51b9 100644 --- a/FrontEnd/src/app/views/common/OperationDialog.tsx +++ b/FrontEnd/src/app/views/common/OperationDialog.tsx @@ -2,6 +2,7 @@ import React, { useState } from "react"; import { useTranslation } from "react-i18next"; import { Form, Button, Modal } from "react-bootstrap"; import { TwitterPicker } from "react-color"; +import moment from "moment"; import { convertI18nText, I18nText, UiLogicError } from "@/common"; @@ -79,26 +80,39 @@ export type OperationDialogInput = | OperationDialogColorInput | OperationDialogDateTimeInput; -type MapOperationInputInfoValueType = T extends OperationDialogTextInput - ? string - : T extends OperationDialogBoolInput - ? boolean - : T extends OperationDialogSelectInput - ? string - : T extends OperationDialogColorInput - ? string | null - : T extends OperationDialogDateTimeInput - ? string - : never; - -const defaultValueMap: { - [T in OperationDialogInput as T["type"]]: MapOperationInputInfoValueType; +interface OperationInputTypeStringToValueTypeMap { + text: string; + bool: boolean; + select: string; + color: string | null; + datetime: string; +} + +type MapOperationInputTypeStringToValueType = + Type extends keyof OperationInputTypeStringToValueTypeMap + ? OperationInputTypeStringToValueTypeMap[Type] + : never; + +type MapOperationInputInfoValueType = T extends OperationDialogInput + ? MapOperationInputTypeStringToValueType + : T; + +const initValueMapperMap: { + [T in OperationDialogInput as T["type"]]: ( + item: T + ) => MapOperationInputInfoValueType; } = { - bool: false, - color: null, - datetime: "", - select: "", - text: "", + bool: (item) => item.initValue ?? false, + color: (item) => item.initValue ?? null, + datetime: (item) => { + if (item.initValue != null) { + return moment(item.initValue).format("YYYY-MM-DDTHH:mm:ss"); + } else { + return ""; + } + }, + select: (item) => item.initValue ?? item.options[0].value, + text: (item) => item.initValue ?? "", }; type MapOperationInputInfoValueTypeList< @@ -171,9 +185,13 @@ const OperationDialog = < type ValueType = boolean | string | null | undefined; const [values, setValues] = useState( - inputScheme.map((i) => { - if (i.type in defaultValueMap) { - return i.initValue ?? defaultValueMap[i.type]; + inputScheme.map((item) => { + if (item.type in initValueMapperMap) { + return ( + initValueMapperMap[item.type] as ( + i: OperationDialogInput + ) => ValueType + )(item); } else { throw new UiLogicError("Unknown input scheme."); } @@ -199,7 +217,11 @@ const OperationDialog = < setStep({ type: "process" }); props .onProcess( - values as unknown as MapOperationInputInfoValueTypeList + values.map((v, index) => { + if (inputScheme[index].type === "datetime" && v !== "") + return new Date(v as string).toISOString(); + else return v; + }) as unknown as MapOperationInputInfoValueTypeList ) .then( (d) => { -- cgit v1.2.3