From 2e5c678690d7e0c406da3a67bf1633a013858127 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 7 Oct 2024 18:56:05 +0530 Subject: [PATCH] Success --- .../photos/components/SingleInputFormV2.tsx | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/web/packages/new/photos/components/SingleInputFormV2.tsx b/web/packages/new/photos/components/SingleInputFormV2.tsx index 9c40c9b38d..44e533eea0 100644 --- a/web/packages/new/photos/components/SingleInputFormV2.tsx +++ b/web/packages/new/photos/components/SingleInputFormV2.tsx @@ -1,7 +1,9 @@ import log from "@/base/log"; import { wait } from "@/utils/promise"; +import Done from "@mui/icons-material/Done"; import { Box, + CircularProgress, Dialog, DialogContent, DialogTitle, @@ -10,7 +12,7 @@ import { } from "@mui/material"; import { useFormik } from "formik"; import { t } from "i18next"; -import React from "react"; +import React, { useState } from "react"; import { FocusVisibleButton } from "./FocusVisibleButton"; import type { DialogVisibilityProps } from "./mui/Dialog"; @@ -40,6 +42,14 @@ type SingleInputFormProps = Pick< * an indeterminate progress indicator is shown. */ onSubmit: ((name: string) => void) | ((name: string) => Promise); + /** + * Completion handler. + * + * After a successful completion, the form waits a bit to show a brief + * "success indicator" to the user. Post that, it calls this completion + * handler to allow the caller to proceed to the next step. + */ + onComplete: () => void; }; /** @@ -59,8 +69,11 @@ export const SingleInputFormV2: React.FC = ({ submitButtonTitle, onCancel, onSubmit, + onComplete, ...rest }) => { + const [isDone, setIsDone] = useState(false); + const formik = useFormik({ initialValues: { value: initialValue ?? "", @@ -73,6 +86,9 @@ export const SingleInputFormV2: React.FC = ({ } try { await onSubmit(value); + setIsDone(true); + await wait(1000); + onComplete(); } catch (e) { log.error(`Failed to submit input ${value}`, e); setFieldError("value", t("UNKNOWN_ERROR")); @@ -113,8 +129,21 @@ export const SingleInputFormV2: React.FC = ({ color="accent" type="submit" disabled={formik.isSubmitting} + sx={{ + "&.Mui-disabled": { + backgroundColor: (theme) => + theme.colors.accent.A500, + color: (theme) => theme.colors.text.base, + }, + }} > - {submitButtonTitle} + {isDone ? ( + + ) : formik.isSubmitting ? ( + + ) : ( + submitButtonTitle + )} @@ -139,7 +168,7 @@ export const SingleInputDialogTest: React.FC = ({ // }; const handleSubmit = async (value: string) => { - await wait(1000); + await wait(3000); if (value == "t") throw new Error("test"); }; @@ -161,6 +190,7 @@ export const SingleInputDialogTest: React.FC = ({ submitButtonTitle="Add person" onCancel={onClose} onSubmit={handleSubmit} + onComplete={onClose} />