From 3e046a44e47a91f4ac5e58f5b7aac6e70fd23bbb Mon Sep 17 00:00:00 2001
From: Jan-Erik S <6697396+Bhavatu@users.noreply.github.com>
Date: Fri, 18 Oct 2024 16:55:54 +0300
Subject: [PATCH] Effectively Empty form validation
---
components/common/FeedbackForm.js | 38 +++-
locales/en/common.json | 355 +++++++++++++++---------------
2 files changed, 215 insertions(+), 178 deletions(-)
diff --git a/components/common/FeedbackForm.js b/components/common/FeedbackForm.js
index ad1c52fd..a0a4185d 100644
--- a/components/common/FeedbackForm.js
+++ b/components/common/FeedbackForm.js
@@ -58,7 +58,12 @@ const FeedbackForm = ({
const t = useTranslations();
const [sent, setSent] = useState(false);
- const onDismiss = () => setSent(false);
+ const [formEmptyError, setFormEmptyError] = useState(false);
+ const onDismiss = () => {
+ setSent(false);
+ setFormEmptyError(false);
+ };
+
const pathname = usePathname();
const searchParams = useSearchParams();
@@ -70,6 +75,26 @@ const FeedbackForm = ({
const onSubmit = (formData) => {
const { name, email, comment, ...additionalResponse } = formData;
+ const isCommentEmpty = !comment || comment.trim() === '';
+ const areAllAdditionalFieldsEmpty = Object.values(additionalResponse).every(
+ (value) => {
+ if (Array.isArray(value)) {
+ return value.length === 0;
+ }
+ return value === '' || value === null || value === undefined;
+ }
+ );
+
+ const isFormEffectivelyEmpty =
+ feedbackSetting !== 'required' &&
+ isCommentEmpty &&
+ areAllAdditionalFieldsEmpty;
+
+ if (isFormEffectivelyEmpty) {
+ setFormEmptyError(true);
+ return;
+ }
+
const data = {
name,
email: emailSetting !== 'excluded' ? email : undefined,
@@ -85,6 +110,7 @@ const FeedbackForm = ({
),
};
setSent(true);
+ setFormEmptyError(false);
createUserFeedback({ variables: { input: data } });
};
const renderAdditionalField = (field) => {
@@ -292,6 +318,16 @@ const FeedbackForm = ({
{t('feedback-error-content')}
)}
+ {formEmptyError && (
+
+ {t('form-effectively-empty-error')}
+
+ )}