-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement horizontal marking (#36)
* feat: implement basic horizontal marking pane visual concept * feat: implement correct handling of horizontal marking section checkboxes * feat: implement All/None buttons * feat: disable all/none button conditionally * feat: implement filtering of question/part/section by visibility * refactor: merge update-by-question and update-by-part * fix: use unique keys * feat: reflect horizontal marking in scrollspy * refactor: use keys to extract only section number * refactor: reduce code duplications * refactor: improve naming and use Boolean function * style: use pointer cursor for headers and change colours of all/none buttons
- Loading branch information
1 parent
9561800
commit 1c6e590
Showing
7 changed files
with
270 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { MixerHorizontalIcon } from '@radix-ui/react-icons' | ||
import { | ||
Box, | ||
Button, | ||
CheckboxGroup, | ||
Flex, | ||
Grid, | ||
Heading, | ||
Popover, | ||
Separator, | ||
} from '@radix-ui/themes' | ||
import { fromPairs, keys, mapValues, pickBy, size } from 'lodash' | ||
import { FC, useEffect, useState } from 'react' | ||
|
||
import '../../index.css' | ||
import { Question } from '../../types/exam' | ||
import { hasPrefix, numberToLetter, numberToRoman } from '../../utils/common' | ||
|
||
interface HorizontalMarkingPaneProps { | ||
questions: Record<number, Question> | ||
sectionIDs: string[] | ||
onActiveSectionsUpdate: (sectionIDs: string[]) => void | ||
} | ||
|
||
const HorizontalMarkingPane: FC<HorizontalMarkingPaneProps> = ({ | ||
questions, | ||
sectionIDs, | ||
onActiveSectionsUpdate, | ||
}) => { | ||
const [horizontalMarkingState, setHorizontalMarkingState] = useState<Record<string, boolean>>( | ||
fromPairs(sectionIDs.map((id) => [id, true])) | ||
) | ||
useEffect(() => { | ||
onActiveSectionsUpdate(keys(pickBy(horizontalMarkingState, Boolean))) | ||
}, [horizontalMarkingState, onActiveSectionsUpdate]) | ||
|
||
function handleUpdateAll(value: boolean) { | ||
setHorizontalMarkingState((current) => mapValues(current, () => value)) | ||
} | ||
|
||
function handleBulkToggleByPrefix(prefix: string) { | ||
setHorizontalMarkingState((current) => { | ||
let flippedState = !hasPrefix(keys(pickBy(current, Boolean)), prefix) | ||
return mapValues(current, (v, k) => (k.startsWith(`${prefix}-`) ? flippedState : v)) | ||
}) | ||
} | ||
|
||
function handleSectionSelectionToggle(partID: string, selectedIDs: string[]) { | ||
setHorizontalMarkingState((current) => | ||
mapValues(current, (v, k) => (k.startsWith(`${partID}-`) ? selectedIDs.includes(k) : v)) | ||
) | ||
} | ||
|
||
return ( | ||
<Popover.Root> | ||
<Popover.Trigger> | ||
<Button variant="soft"> | ||
<MixerHorizontalIcon width="16" height="16" /> | ||
Horizontal Marking | ||
</Button> | ||
</Popover.Trigger> | ||
<Popover.Content minWidth="20vw"> | ||
<Grid gap="5" columns={size(questions).toString()}> | ||
{Object.entries(questions).map(([q, question]) => ( | ||
<Box key={q} p="2"> | ||
<Heading className="clickable" onClick={() => handleBulkToggleByPrefix(q)}> | ||
Question {q} | ||
</Heading> | ||
<Separator size="4" /> | ||
{Object.entries(question.parts).map(([p, part]) => { | ||
const partID = `${q}-${p}` | ||
return ( | ||
<Box key={partID} p="1"> | ||
<Heading | ||
className="clickable" | ||
size="5" | ||
as="h2" | ||
onClick={() => handleBulkToggleByPrefix(partID)} | ||
> | ||
Part {numberToLetter(Number(p))} | ||
</Heading> | ||
<Box p="1"> | ||
<CheckboxGroup.Root | ||
value={keys(pickBy(horizontalMarkingState, Boolean))} | ||
onValueChange={(vs) => handleSectionSelectionToggle(partID, vs)} | ||
> | ||
{Object.keys(part.sections).map((s) => { | ||
const sectionID = `${q}-${p}-${s}` | ||
return ( | ||
<CheckboxGroup.Item key={sectionID} value={sectionID}> | ||
Section {numberToRoman(Number(s))} | ||
</CheckboxGroup.Item> | ||
) | ||
})} | ||
</CheckboxGroup.Root> | ||
</Box> | ||
</Box> | ||
) | ||
})} | ||
</Box> | ||
))} | ||
</Grid> | ||
<Flex justify="center" align="center" className="button-group"> | ||
<Button onClick={() => handleUpdateAll(true)}>All</Button> | ||
<Button color="gray" onClick={() => handleUpdateAll(false)}> | ||
None | ||
</Button> | ||
</Flex> | ||
</Popover.Content> | ||
</Popover.Root> | ||
) | ||
} | ||
|
||
export default HorizontalMarkingPane |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.