Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

custom error

Manav Rathi9b5dcc9a954d
diff --git a/web/packages/base/components/SingleInputDialog.tsx b/web/packages/base/components/SingleInputDialog.tsx
index 11f23575ad..7282b14a81 100644
--- a/web/packages/base/components/SingleInputDialog.tsx
+++ b/web/packages/base/components/SingleInputDialog.tsx
@@ -1,7 +1,11 @@
import { Dialog, DialogContent, DialogTitle } from "@mui/material";
import type { ModalVisibilityProps } from "ente-base/components/utils/modal";
import React from "react";
-import { SingleInputForm, type SingleInputFormProps } from "./SingleInputForm";
+import {
+ SingleInputForm,
+ type SingleInputFormProps,
+ type SingleInputFormShowError,
+} from "./SingleInputForm";
type SingleInputDialogProps = ModalVisibilityProps &
Omit<SingleInputFormProps, "onCancel"> & {
@@ -25,8 +29,11 @@ export const SingleInputDialog: React.FC<SingleInputDialogProps> = ({
title,
...rest
}) => {
- const handleSubmit = async (value: string) => {
- await onSubmit(value);
+ const handleSubmit = async (
+ value: string,
+ showError: SingleInputFormShowError,
+ ) => {
+ await onSubmit(value, showError);
onClose();
};
diff --git a/web/packages/base/components/SingleInputForm.tsx b/web/packages/base/components/SingleInputForm.tsx
index 7483d8d75b..00c04f4935 100644
--- a/web/packages/base/components/SingleInputForm.tsx
+++ b/web/packages/base/components/SingleInputForm.tsx
@@ -6,6 +6,8 @@ import { useFormik } from "formik";
import { t } from "i18next";
import React from "react";
+export type SingleInputFormShowError = (message: string) => void;
+
export type SingleInputFormProps = Pick<
TextFieldProps,
"label" | "placeholder" | "autoComplete" | "autoFocus" | "slotProps"
@@ -37,9 +39,25 @@ export type SingleInputFormProps = Pick<
* If this function rejects then a generic error helper text is shown below
* the text input, and the input (/ buttons) reenabled.
*
+ * This function is also passed an function that can be used to explicitly
+ * set the error message that as shown below the text input when a specific
+ * problem occurs during submission.
+ *
* @param name The current value of the text input.
+ *
+ * @param showError A function that can be called to set the error message
+ * shown below the text input if submission fails.
+ *
+ * Note that if {@link showError} is called, then the {@link onSubmit}
+ * function should not throw, otherwise the error message shown by
+ * {@link showError} will get overwritten by the generic error message.
*/
- onSubmit: ((name: string) => void) | ((name: string) => Promise<void>);
+ onSubmit:
+ | ((name: string, showError: SingleInputFormShowError) => void)
+ | ((
+ name: string,
+ showError: SingleInputFormShowError,
+ ) => Promise<void>);
};
/**
@@ -64,15 +82,18 @@ export const SingleInputForm: React.FC<SingleInputFormProps> = ({
initialValues: { value: initialValue ?? "" },
onSubmit: async (values, { setFieldError }) => {
const value = values.value;
+ const showError = (message: string) =>
+ setFieldError("value", message);
+
if (!value) {
- setFieldError("value", t("required"));
+ showError(t("required"));
return;
}
try {
- await onSubmit(value);
+ await onSubmit(value, showError);
} catch (e) {
log.error(`Failed to submit input ${value}`, e);
- setFieldError("value", t("generic_error"));
+ showError(t("generic_error"));
}
},
});