Skip to content

Commit

Permalink
Merge branch 'long-term/remove-tp' into adjust-pp-model
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlo-mk authored Oct 31, 2024
2 parents 39f3a99 + 4905ab5 commit a80e25a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 49 deletions.
18 changes: 14 additions & 4 deletions src/frontend/src/components/targeting/SubField.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-shadow */
import CalendarTodayRoundedIcon from '@mui/icons-material/CalendarTodayRounded';
import { Field, useFormikContext } from 'formik';
import { FC, useEffect } from 'react';
Expand Down Expand Up @@ -51,6 +52,11 @@ interface Values {
isNull?: boolean;
}[];
}[];
collectorsFiltersBlocks?: {
collectorBlockFilters?: {
isNull?: boolean;
}[];
}[];
}

interface SubFieldProps {
Expand Down Expand Up @@ -78,11 +84,15 @@ export const SubField: FC<SubFieldProps> = ({
}
}

const checkIsNullInBlocks = (blocks, blockIndex, index) => {
return blockIndex !== undefined && index !== undefined
? blocks?.[blockIndex]?.blockFilters?.[index]?.isNull ?? false
: false;
};

const isNullSelected =
(blockIndex !== undefined && index !== undefined
? values?.individualsFiltersBlocks?.[blockIndex]
?.individualBlockFilters?.[index]?.isNull ?? false
: false) ||
checkIsNullInBlocks(values?.individualsFiltersBlocks, blockIndex, index) ||
checkIsNullInBlocks(values?.collectorsFiltersBlocks, blockIndex, index) ||
(values.filters?.some((filter) => filter.isNull) ?? false);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ const CriteriaField = ({ field, choicesDict }): ReactElement => {
interface CriteriaProps {
rules: [TargetingCriteriaRuleObjectType];
individualsFiltersBlocks;
collectorsFiltersBlocks;
removeFunction?;
editFunction?;
isEdit: boolean;
Expand All @@ -235,6 +236,7 @@ export function Criteria({
choicesDict,
alternative = null,
individualsFiltersBlocks,
collectorsFiltersBlocks,
}: CriteriaProps): ReactElement {
return (
<CriteriaElement alternative={alternative} data-cy="criteria-container">
Expand All @@ -252,6 +254,17 @@ export function Criteria({
))}
</CriteriaSetBox>
))}
{collectorsFiltersBlocks.map((item, index) => (
<CriteriaSetBox key={index}>
{item.collectorsFiltersBlocks.map((filter, filterIndex) => (
<CriteriaField
choicesDict={choicesDict}
key={filterIndex}
field={filter}
/>
))}
</CriteriaSetBox>
))}
{isEdit && (
<ButtonsContainer>
<IconButton data-cy="button-edit" onClick={editFunction}>
Expand Down
95 changes: 50 additions & 45 deletions src/frontend/src/containers/forms/TargetingCriteriaForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const validationSchema = Yup.object().shape({
.nullable()
.when(
['fieldName', 'fieldAttribute'],
(fieldName, fieldAttribute, schema) => {
(_fieldName, _fieldAttribute, schema) => {
const parent = schema.parent;
if (
parent &&
Expand Down Expand Up @@ -99,7 +99,7 @@ const validationSchema = Yup.object().shape({
.nullable()
.when(
['fieldName', 'fieldAttribute'],
(fieldName, fieldAttribute, schema) => {
(_fieldName, _fieldAttribute, schema) => {
const parent = schema.parent;
if (
parent &&
Expand Down Expand Up @@ -199,65 +199,70 @@ export const TargetingCriteriaForm = ({

if (!data) return null;

const validate = ({
filters,
individualsFiltersBlocks,
}): { nonFieldErrors?: string[] } => {
const filterNullOrNoSelections = (filter): boolean =>
!filter.isNull &&
(filter.value === null ||
filter.value === '' ||
(filter?.fieldAttribute?.type === 'SELECT_MANY' &&
filter.value &&
filter.value.length === 0));
const filterNullOrNoSelections = (filter): boolean =>
!filter.isNull &&
(filter.value === null ||
filter.value === '' ||
(filter?.fieldAttribute?.type === 'SELECT_MANY' &&
filter.value &&
filter.value.length === 0));

const filterEmptyFromTo = (filter): boolean =>
!filter.isNull &&
typeof filter.value === 'object' &&
filter.value !== null &&
Object.prototype.hasOwnProperty.call(filter.value, 'from') &&
Object.prototype.hasOwnProperty.call(filter.value, 'to') &&
!filter.value.from &&
!filter.value.to;

const hasFiltersNullValues = Boolean(
filters.filter(filterNullOrNoSelections).length,
);

const hasFiltersEmptyFromToValues = Boolean(
filters.filter(filterEmptyFromTo).length,
);
const filterEmptyFromTo = (filter): boolean =>
!filter.isNull &&
typeof filter.value === 'object' &&
filter.value !== null &&
Object.prototype.hasOwnProperty.call(filter.value, 'from') &&
Object.prototype.hasOwnProperty.call(filter.value, 'to') &&
!filter.value.from &&
!filter.value.to;

const validate = (values) => {
const hasFiltersErrors =
hasFiltersNullValues || hasFiltersEmptyFromToValues;

const hasIndividualsFiltersBlocksErrors = individualsFiltersBlocks.some(
(block) => {
const hasNulls = block.individualBlockFilters.some(
filterNullOrNoSelections,
);
const hasFromToError =
block.individualBlockFilters.some(filterEmptyFromTo);
values.filters.some(filterNullOrNoSelections) ||
values.filters.some(filterEmptyFromTo);

const hasBlockFiltersErrors = (blocks) => {
return blocks.some((block) => {
const hasNulls = block.blockFilters.some(filterNullOrNoSelections);
const hasFromToError = block.blockFilters.some(filterEmptyFromTo);
return hasNulls || hasFromToError;
},
});
};

const hasIndividualsFiltersBlocksErrors = hasBlockFiltersErrors(
values.individualsFiltersBlocks,
);
const hasCollectorsFiltersBlocksErrors = hasBlockFiltersErrors(
values.collectorsFiltersBlocks,
);

const errors: { nonFieldErrors?: string[] } = {};
if (hasFiltersErrors || hasIndividualsFiltersBlocksErrors) {
if (
hasFiltersErrors ||
hasIndividualsFiltersBlocksErrors ||
hasCollectorsFiltersBlocksErrors
) {
errors.nonFieldErrors = ['You need to fill out missing values.'];
}
if (filters.length + individualsFiltersBlocks.length === 0) {
if (
values.filters.length +
values.individualsFiltersBlocks.length +
values.collectorsFiltersBlocks.length ===
0
) {
errors.nonFieldErrors = [
'You need to add at least one household filter or an individual block filter.',
'You need to add at least one household filter, an individual block filter, or a collector block filter.',
];
} else if (
individualsFiltersBlocks.filter(
(block) => block.individualBlockFilters.length === 0,
values.individualsFiltersBlocks.filter(
(block) => block.blockFilters.length === 0,
).length > 0 ||
values.collectorsFiltersBlocks.filter(
(block) => block.blockFilters.length === 0,
).length > 0
) {
errors.nonFieldErrors = [
'You need to add at least one household filter or an individual block filter.',
'You need to add at least one household filter, an individual block filter, or a collector block filter.',
];
}
return errors;
Expand Down
4 changes: 4 additions & 0 deletions src/frontend/src/utils/targetingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ export function getTargetingCriteriaVariables(values) {
block.individualBlockFilters.map(mapFilterToVariable),
}),
),
collectorsFiltersBlocks: rule.collectorsFiltersBlocks.map((block) => ({
collectorsFiltersBlocks:
block.collectorsFiltersBlocks.map(mapFilterToVariable),
})),
})),
},
};
Expand Down

0 comments on commit a80e25a

Please sign in to comment.