Skip to content

Commit

Permalink
the email and feedback visibility/requirement settings as separate bo…
Browse files Browse the repository at this point in the history
…oleans instead
  • Loading branch information
Bhavatu committed Oct 18, 2024
1 parent 3e046a4 commit 82a63ea
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 51 deletions.
28 changes: 16 additions & 12 deletions common/__generated__/graphql.ts

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions components/common/CategoryPageStreamField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ export const CategoryPageStreamField = ({
<ExpandableFeedbackFormBlock
heading={block.heading || undefined}
description={block.description || undefined}
emailSetting={block.emailSetting}
feedbackSetting={block.feedbackSetting}
emailVisible={block.emailVisible}
emailRequired={block.emailRequired}
feedbackVisible={block.feedbackVisible}
feedbackRequired={block.feedbackRequired}
categoryId={page.category?.id || undefined}
fields={block.fields || undefined}
id={block.id}
Expand Down
45 changes: 20 additions & 25 deletions components/common/FeedbackForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ const FeedbackForm = ({
categoryId = null,
heading,
description,
emailSetting = 'required',
feedbackSetting = 'required',
emailVisible = true,
emailRequired = true,
feedbackVisible = true,
feedbackRequired = true,
prompt,
formContext = null,
additionalFields = [],
Expand Down Expand Up @@ -86,9 +88,7 @@ const FeedbackForm = ({
);

const isFormEffectivelyEmpty =
feedbackSetting !== 'required' &&
isCommentEmpty &&
areAllAdditionalFieldsEmpty;
!feedbackRequired && isCommentEmpty && areAllAdditionalFieldsEmpty;

if (isFormEffectivelyEmpty) {
setFormEmptyError(true);
Expand All @@ -97,8 +97,8 @@ const FeedbackForm = ({

const data = {
name,
email: emailSetting !== 'excluded' ? email : undefined,
comment: feedbackSetting !== 'excluded' ? comment : undefined,
email: emailVisible ? email : undefined,
comment: feedbackVisible ? comment : undefined,
additionalFields: JSON.stringify(additionalResponse),
pageId,
type: formContext,
Expand All @@ -113,6 +113,7 @@ const FeedbackForm = ({
setFormEmptyError(false);
createUserFeedback({ variables: { input: data } });
};

const renderAdditionalField = (field) => {
const requiredMessage = ` (${t('required-field')})`;

Expand Down Expand Up @@ -242,22 +243,17 @@ const FeedbackForm = ({
control={control}
defaultValue=""
/>
{emailSetting !== 'excluded' && (
{emailVisible && (
<Controller
render={({ field }) => (
<TextInput
{...field}
id="email-field"
autoComplete="email"
label={`${t('email')}${
emailSetting === 'required'
? ` (${t('required-field')})`
: ''
emailRequired ? ` (${t('required-field')})` : ''
}`}
invalid={
emailSetting === 'required' &&
errors.email?.type === 'required'
}
invalid={emailRequired && errors.email?.type === 'required'}
formFeedback={errors.email && t('error-email-format')}
/>
)}
Expand All @@ -266,7 +262,7 @@ const FeedbackForm = ({
control={control}
defaultValue=""
rules={{
required: emailSetting === 'required',
required: emailRequired,
pattern: {
value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
},
Expand All @@ -276,20 +272,17 @@ const FeedbackForm = ({

{additionalFields.map(renderAdditionalField)}

{feedbackSetting !== 'excluded' && (
{feedbackVisible && (
<Controller
render={({ field }) => (
<TextInput
{...field}
id="comment-field"
label={`${t('feedback')}${
feedbackSetting === 'required'
? ` (${t('required-field')})`
: ''
feedbackRequired ? ` (${t('required-field')})` : ''
}`}
invalid={
feedbackSetting === 'required' &&
errors.comment?.type === 'required'
feedbackRequired && errors.comment?.type === 'required'
}
type="textarea"
rows="3"
Expand All @@ -302,7 +295,7 @@ const FeedbackForm = ({
name="comment"
id="comment-field"
control={control}
rules={{ required: feedbackSetting === 'required' }}
rules={{ required: feedbackRequired }}
defaultValue=""
/>
)}
Expand Down Expand Up @@ -351,8 +344,10 @@ FeedbackForm.propTypes = {
categoryId: PropTypes.string,
heading: PropTypes.string,
description: PropTypes.string,
emailSetting: PropTypes.oneOf(['required', 'optional', 'excluded']),
feedbackSetting: PropTypes.oneOf(['required', 'optional', 'excluded']),
emailVisible: PropTypes.bool,
emailRequired: PropTypes.bool,
feedbackVisible: PropTypes.bool,
feedbackRequired: PropTypes.bool,
prompt: PropTypes.string,
formContext: PropTypes.string,
additionalFields: PropTypes.arrayOf(PropTypes.object),
Expand Down
18 changes: 12 additions & 6 deletions components/contentblocks/ExpandableFeedbackFormBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ interface Props {
context?: 'sidebar' | 'default';
heading?: string;
description?: string;
feedbackSetting?: 'required' | 'optional' | 'excluded';
emailSetting?: 'required' | 'optional' | 'excluded';
feedbackVisible?: boolean;
feedbackRequired?: boolean;
emailVisible?: boolean;
emailRequired?: boolean;
fields?: [];
id: string;
pageId: number;
Expand All @@ -56,8 +58,10 @@ const ExpandableFeedbackFormBlock = ({
context = 'default',
heading,
description,
feedbackSetting = 'required',
emailSetting = 'required',
feedbackVisible = true,
feedbackRequired = true,
emailVisible = true,
emailRequired = true,
fields,
id,
pageId,
Expand Down Expand Up @@ -100,8 +104,10 @@ const ExpandableFeedbackFormBlock = ({
categoryId={isCategory ? categoryId : undefined}
heading=""
description=""
feedbackSetting={feedbackSetting}
emailSetting={emailSetting}
feedbackVisible={feedbackVisible}
feedbackRequired={feedbackRequired}
emailVisible={emailVisible}
emailRequired={emailRequired}
prompt=""
formContext={isAction ? 'action' : 'category'}
additionalFields={fields}
Expand Down
12 changes: 8 additions & 4 deletions queries/get-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,10 @@ const GET_ACTION_DETAILS = gql`
... on ActionContactFormBlock {
heading
description
emailSetting
feedbackSetting
emailVisible
emailRequired
feedbackVisible
feedbackRequired
fields {
... on FormFieldBlock {
id
Expand Down Expand Up @@ -429,8 +431,10 @@ const GET_ACTION_DETAILS = gql`
... on ActionContactFormBlock {
heading
description
emailSetting
feedbackSetting
emailVisible
emailRequired
feedbackVisible
feedbackRequired
fields {
... on FormFieldBlock {
id
Expand Down
6 changes: 4 additions & 2 deletions queries/get-content-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ const TEMPLATED_CATEGORY_PAGE_FRAGMENT = gql`
id
heading
description
emailSetting
feedbackSetting
emailVisible
emailRequired
feedbackVisible
feedbackRequired
fields {
... on FormFieldBlock {
id
Expand Down

0 comments on commit 82a63ea

Please sign in to comment.