Skip to content

Commit

Permalink
feat: add warning in inactive step block for mismatched options
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin9foong committed Nov 19, 2024
1 parent 48f11b3 commit cd83b7f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '~shared/types'

import { textStyles } from '~theme/textStyles'
import { checkIsOptionsMismatched } from '~utils/optionsToEmailValidation'
import { parseCsvFileToCsvString } from '~utils/parseCsvFileToCsvString'
import { SingleSelect } from '~components/Dropdown'
import Attachment from '~components/Field/Attachment'
Expand Down Expand Up @@ -430,25 +431,14 @@ const ConditionalRoutingOption = ({
)
}

const checkMissingElement = (actual: string[], expected: Set<string>) => {
return actual.some((option) => !expected.has(option))
}

const validateOptions = (
optionsToRecipientsMapOptions: string[] | undefined,
selectedConditionalFieldOptions: string[] | undefined,
optionsToRecipientsMapOptions: string[],
selectedConditionalFieldOptions: string[],
) => {
if (!optionsToRecipientsMapOptions || !selectedConditionalFieldOptions)
return 'Your CSV or selected field does not contain any options'

if (
checkMissingElement(
checkIsOptionsMismatched(
optionsToRecipientsMapOptions,
new Set<string>(selectedConditionalFieldOptions),
) ||
checkMissingElement(
selectedConditionalFieldOptions,
new Set<string>(optionsToRecipientsMapOptions),
)
) {
return MISMATCHED_OPTIONS_ERROR_MESSAGE
Expand All @@ -457,7 +447,7 @@ const ConditionalRoutingOption = ({

const validateOptionsToRecipientsMapErrorMessage = validateOptions(
[...Object.keys(selectedConditionalFieldOptionsToRecipientsMap || {})],
selectedConditionalField?.fieldOptions,
selectedConditionalField?.fieldOptions || [],
)

const noEmailToOptionsMappingErrorMessage =
Expand Down Expand Up @@ -511,7 +501,10 @@ const ConditionalRoutingOption = ({
return 'There are duplicate options in your CSV.'
}

return validateOptions([...optionsSet], selectedConditionalFieldOptions)
return validateOptions(
[...optionsSet],
selectedConditionalFieldOptions || [],
)
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Dictionary } from 'lodash'
import { BasicField, FormField } from '~shared/types'
import { FormWorkflowStepDto, WorkflowType } from '~shared/types/form'

import { checkIsOptionsMismatched } from '~utils/optionsToEmailValidation'
import IconButton from '~components/IconButton'

import { FieldLogicBadge } from '~features/admin-form/create/logic/components/LogicContent/InactiveLogicBlock/FieldLogicBadge'
Expand Down Expand Up @@ -59,16 +60,14 @@ const SubsequentStepRespondentBadges = ({
}
const selectedConditionalFieldOptions =
selectedConditionalField.fieldOptions
const optionsToRecipientsMapOptions = Object.keys(
selectedConditionalField.optionsToRecipientsMap || {},
)

const optionsToRecipientsMap =
selectedConditionalField.optionsToRecipientsMap

const isMissingOptions =
selectedConditionalFieldOptions &&
optionsToRecipientsMap &&
selectedConditionalFieldOptions.some(
(option) => !optionsToRecipientsMap[option],
)
const isOptionsMismatched = checkIsOptionsMismatched(
optionsToRecipientsMapOptions,
selectedConditionalFieldOptions,
)
return (
<Stack direction="column" spacing="0.5rem">
<FieldLogicBadge
Expand All @@ -78,7 +77,7 @@ const SubsequentStepRespondentBadges = ({
: undefined
}
/>
{isMissingOptions ? (
{isOptionsMismatched ? (
<FieldLogicBadge
defaults={{
variant: 'error',
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/utils/optionsToEmailValidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const checkMissingElement = (actual: string[], expected: Set<string>) => {
return actual.some((option) => !expected.has(option))
}

export const checkIsOptionsMismatched = (
optionsToRecipientsMapOptions: string[],
selectedConditionalFieldOptions: string[],
) => {
return (
checkMissingElement(
optionsToRecipientsMapOptions,
new Set<string>(selectedConditionalFieldOptions),
) ||
checkMissingElement(
selectedConditionalFieldOptions,
new Set<string>(optionsToRecipientsMapOptions),
)
)
}

0 comments on commit cd83b7f

Please sign in to comment.