Add Sonner-based notifications and UX polish
This commit is contained in:
@@ -40,9 +40,15 @@ const contactErrorMessages = {
|
||||
},
|
||||
} as const;
|
||||
|
||||
function withError(pathname: string, message: string) {
|
||||
const contactSuccessMessages = {
|
||||
ar: "شكراً على رسالتك.",
|
||||
en: "Thank you for your message.",
|
||||
de: "Vielen Dank fuer deine Nachricht.",
|
||||
} as const;
|
||||
|
||||
function withMessage(pathname: string, type: "success" | "error", message: string) {
|
||||
const params = new URLSearchParams();
|
||||
params.set("error", message);
|
||||
params.set(type, message);
|
||||
|
||||
return `${pathname}?${params.toString()}`;
|
||||
}
|
||||
@@ -80,18 +86,18 @@ export async function submitContactFormAction(formData: FormData) {
|
||||
message: values.message,
|
||||
});
|
||||
|
||||
redirect(getLocalizedPath(locale, "/success"));
|
||||
redirect(withMessage(getLocalizedPath(locale, "/success"), "success", contactSuccessMessages[locale]));
|
||||
} catch (error) {
|
||||
if (isRedirectError(error)) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (error instanceof z.ZodError) {
|
||||
redirect(withError(contactPath, contactErrorMessages[locale].invalid));
|
||||
redirect(withMessage(contactPath, "error", contactErrorMessages[locale].invalid));
|
||||
}
|
||||
|
||||
if (error instanceof Error && error.message === "Too many contact requests. Please try again later.") {
|
||||
redirect(withError(contactPath, contactErrorMessages[locale].blocked));
|
||||
redirect(withMessage(contactPath, "error", contactErrorMessages[locale].blocked));
|
||||
}
|
||||
|
||||
if (
|
||||
@@ -100,10 +106,10 @@ export async function submitContactFormAction(formData: FormData) {
|
||||
error.message === "Turnstile verification failed." ||
|
||||
error.message === "Turnstile verification request failed.")
|
||||
) {
|
||||
redirect(withError(contactPath, contactErrorMessages[locale].verification));
|
||||
redirect(withMessage(contactPath, "error", contactErrorMessages[locale].verification));
|
||||
}
|
||||
|
||||
console.error("Contact form delivery failed.", error);
|
||||
redirect(withError(contactPath, contactErrorMessages[locale].failed));
|
||||
redirect(withMessage(contactPath, "error", contactErrorMessages[locale].failed));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user