From a9dc6b16d6730d8d1dc1ea2fab8ab3830fe56ce4 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 28 Jul 2023 00:40:58 +0800 Subject: ... --- .../src/views/common/dialog/OperationDialog.tsx | 108 +++++++++------------ 1 file changed, 48 insertions(+), 60 deletions(-) (limited to 'FrontEnd/src/views/common/dialog/OperationDialog.tsx') diff --git a/FrontEnd/src/views/common/dialog/OperationDialog.tsx b/FrontEnd/src/views/common/dialog/OperationDialog.tsx index ad9bf5c1..97d135e9 100644 --- a/FrontEnd/src/views/common/dialog/OperationDialog.tsx +++ b/FrontEnd/src/views/common/dialog/OperationDialog.tsx @@ -5,12 +5,11 @@ import { useC, Text, ThemeColor } from "../common"; import Button from "../button/Button"; import { - default as InputGroup, - InputErrors, - InputList, - Validator, - Values, - useDirties, + useInputs, + InputGroup, + InitializeInfo as InputInitializer, + InputValueDict, + InputScheme, } from "../input/InputGroup"; import LoadingButton from "../button/LoadingButton"; import Dialog from "./Dialog"; @@ -36,42 +35,47 @@ function OperationDialogPrompt(props: OperationDialogPromptProps) { ); } -export interface OperationDialogProps { +export interface OperationDialogProps { open: boolean; - onClose: () => void; + close: () => void; color?: ThemeColor; title: Text; inputPrompt?: Text; - processPrompt?: Text; successPrompt?: (data: TData) => ReactNode; failurePrompt?: (error: unknown) => ReactNode; - inputs: Inputs; - validator?: Validator; + inputInit?: InputInitializer; + inputScheme?: InputScheme; - onProcess: (inputs: Values) => Promise; + onProcess: (inputs: InputValueDict) => Promise; onSuccessAndClose?: (data: TData) => void; } -function OperationDialog( - props: OperationDialogProps, -) { +function OperationDialog(props: OperationDialogProps) { const { open, - onClose, + close, color, title, inputPrompt, - processPrompt, successPrompt, failurePrompt, - inputs, - validator, + inputInit, + inputScheme, onProcess, onSuccessAndClose, } = props; + if (process.env.NODE_ENV === "development") { + if (inputScheme == null && inputInit == null) { + throw Error("Scheme or Init? Choose one and create one."); + } + if (inputScheme != null && inputInit != null) { + throw Error("Scheme or Init? Choose one and drop one"); + } + } + const c = useC(); type Step = @@ -87,15 +91,17 @@ function OperationDialog( }; const [step, setStep] = useState({ type: "input" }); - const [values, setValues] = useState>(); - const [errors, setErrors] = useState(); - const [dirties, setDirties, dirtyAll] = useDirties(); - function close() { + const { inputGroupProps, hasError, setAllDisabled, confirm } = useInputs({ + /* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */ + init: inputInit ?? { scheme: inputScheme! }, + }); + + function onClose() { if (step.type !== "process") { - props.onClose(); + close(); if (step.type === "success" && props.onSuccessAndClose) { - props.onSuccessAndClose(step.data); + onSuccessAndClose?.(step.data); } } else { console.log("Attempt to close modal dialog when processing."); @@ -103,14 +109,11 @@ function OperationDialog( } function onConfirm() { - setStep({ type: "process" }); - props - .onProcess( - values.map((value, index) => - finalValueMapperMap[inputScheme[index].type](value as never), - ) as Values, - ) - .then( + const result = confirm(); + if (result.type === "ok") { + setStep({ type: "process" }); + setAllDisabled(true); + onProcess(result.values).then( (d) => { setStep({ type: "success", @@ -124,31 +127,21 @@ function OperationDialog( }); }, ); + } } let body: ReactNode; if (step.type === "input" || step.type === "process") { const isProcessing = step.type === "process"; - const hasError = errors.length > 0; body = (
- + { - setValues(values); - setErrors(errors); - }} - dirties={dirties} - onDirty={setDirties} + {...inputGroupProps} />

@@ -157,19 +150,14 @@ function OperationDialog( text="operationDialog.cancel" color="secondary" outline - onClick={close} + onClick={onClose} disabled={isProcessing} /> { - dirtyAll(); - if (validate(values)) { - onConfirm(); - } - }} + onClick={onConfirm} > {c("operationDialog.confirm")} @@ -183,32 +171,32 @@ function OperationDialog( result.type === "success" ? { message: "operationDialog.success", - customMessage: props.successPrompt?.(result.data), + customMessage: successPrompt?.(result.data), } : { message: "operationDialog.error", - customMessage: props.failurePrompt?.(result.data), + customMessage: failurePrompt?.(result.data), }; body = (

-
); } return ( - +
-
{c(props.title)}
+
{c(title)}

{body}
-- cgit v1.2.3