Skip to content

Commit

Permalink
fix(CriteriaForm): correct validating data
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Aug 3, 2023
1 parent 00a1a39 commit 756d4c4
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/components/GoalCriteria/GoalCriteria.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,11 @@ export const GoalCriteria: React.FC<GoalCriteriaProps> = ({
onConvertToGoal,
onUpdateCriteria,
}) => {
const [{ mode, index }, setViewItemMode] = useState<{ mode: 'view'; index: -1 } | { mode: 'edit'; index: number }>({
const [{ mode, criteriaId }, setViewItemMode] = useState<
{ mode: 'view'; criteriaId: null } | { mode: 'edit'; criteriaId: string }
>({
mode: 'view',
index: -1,
criteriaId: null,
});
const onAddHandler = useCallback(
(val: AddCriteriaScheme) => {
Expand All @@ -319,25 +321,6 @@ export const GoalCriteria: React.FC<GoalCriteriaProps> = ({
[onAddCriteria, goalId],
);

const dataForValidateCriteria = useMemo(
() =>
criteriaList.reduce<{ sum: number; title: string[] }>(
(acc, { weight, title }, criteriaIdx) => {
if (mode === 'edit' && index === criteriaIdx) {
return acc;
}
acc.sum += weight;
acc.title.push(title);
return acc;
},
{
sum: 0,
title: [],
},
),
[criteriaList, mode, index],
);

const sortedCriteriaItems = useMemo(() => {
const list = criteriaList.reduce<Record<'done' | 'undone', GoalAchiveCriteria[]>>(
(acc, criteria) => {
Expand All @@ -358,6 +341,25 @@ export const GoalCriteria: React.FC<GoalCriteriaProps> = ({
return list.done.concat(list.undone);
}, [criteriaList]);

const dataForValidateCriteria = useMemo(
() =>
sortedCriteriaItems.reduce<{ sum: number; title: string[] }>(
(acc, { weight, title, id }) => {
if (mode === 'edit' && id === criteriaId) {
return acc;
}
acc.sum += weight;
acc.title.push(title);
return acc;
},
{
sum: 0,
title: [],
},
),
[sortedCriteriaItems, mode, criteriaId],
);

return (
<ActivityFeedItem>
<Circle size={32}>
Expand All @@ -372,8 +374,8 @@ export const GoalCriteria: React.FC<GoalCriteriaProps> = ({
</StyledHeadingWrapper>
))}
<StyledTable gap={5}>
{sortedCriteriaItems.map((item, i) => {
if (mode === 'view' || i !== index) {
{sortedCriteriaItems.map((item) => {
if (mode === 'view' || item.id !== criteriaId) {
return (
<GoalCriteriaItem
key={item.id}
Expand All @@ -385,15 +387,15 @@ export const GoalCriteria: React.FC<GoalCriteriaProps> = ({
onUpdateClick={() =>
setViewItemMode({
mode: 'edit',
index: i,
criteriaId: item.id,
})
}
canEdit={canEdit}
item={item}
/>
);
}
if (mode === 'edit' && index === i) {
if (mode === 'edit' && criteriaId === item.id) {
return (
<EditCriteriaForm
key={item.id}
Expand All @@ -411,7 +413,7 @@ export const GoalCriteria: React.FC<GoalCriteriaProps> = ({
onReset={() =>
setViewItemMode({
mode: 'view',
index: -1,
criteriaId: null,
})
}
/>
Expand Down

0 comments on commit 756d4c4

Please sign in to comment.