From 04cfa9a4a18f425f1fbce6f5d656489639cab852 Mon Sep 17 00:00:00 2001 From: Ivan Procaccini Date: Tue, 8 Oct 2024 12:01:27 +0100 Subject: [PATCH] refactor: improve naming and use Boolean function --- .../MarkingPage/HorizontalMarkingPane.tsx | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/pages/MarkingPage/HorizontalMarkingPane.tsx b/src/pages/MarkingPage/HorizontalMarkingPane.tsx index 35c3847..ea8a9b8 100644 --- a/src/pages/MarkingPage/HorizontalMarkingPane.tsx +++ b/src/pages/MarkingPage/HorizontalMarkingPane.tsx @@ -31,23 +31,23 @@ const HorizontalMarkingPane: FC = ({ fromPairs(sectionIDs.map((id) => [id, true])) ) useEffect(() => { - onActiveSectionsUpdate(keys(pickBy(horizontalMarkingState, (v, k) => v))) + onActiveSectionsUpdate(keys(pickBy(horizontalMarkingState, Boolean))) }, [horizontalMarkingState, onActiveSectionsUpdate]) function handleUpdateAll(value: boolean) { setHorizontalMarkingState((current) => mapValues(current, () => value)) } - function handleBulkUpdateByPrefix(prefix: string) { + function handleBulkToggleByPrefix(prefix: string) { setHorizontalMarkingState((current) => { - let newValue = !hasPrefix(keys(pickBy(current, (v, _) => v)), prefix) - return mapValues(current, (v, k) => (k.startsWith(`${prefix}-`) ? newValue : v)) + let flippedState = !hasPrefix(keys(pickBy(current, (v, _) => v)), prefix) + return mapValues(current, (v, k) => (k.startsWith(`${prefix}-`) ? flippedState : v)) }) } - function handleSectionSelectionUpdate(partID: string, sectionIDs: string[]) { + function handleSectionSelectionToggle(partID: string, selectedIDs: string[]) { setHorizontalMarkingState((current) => - mapValues(current, (v, k) => (k.startsWith(`${partID}-`) ? sectionIDs.includes(k) : v)) + mapValues(current, (v, k) => (k.startsWith(`${partID}-`) ? selectedIDs.includes(k) : v)) ) } @@ -63,19 +63,19 @@ const HorizontalMarkingPane: FC = ({ {Object.entries(questions).map(([q, question]) => ( - handleBulkUpdateByPrefix(q)}>Question {q} + handleBulkToggleByPrefix(q)}>Question {q} {Object.entries(question.parts).map(([p, part]) => { const partID = `${q}-${p}` return ( - handleBulkUpdateByPrefix(partID)}> + handleBulkToggleByPrefix(partID)}> Part {numberToLetter(Number(p))} handleSectionSelectionUpdate(partID, vs)} + onValueChange={(vs) => handleSectionSelectionToggle(partID, vs)} > {Object.keys(part.sections).map((s) => { const sectionID = `${q}-${p}-${s}`