-
Notifications
You must be signed in to change notification settings - Fork 16
fix: Subject Dropdown Is Not Displaying All Subjects in campaign #4742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,6 +19,7 @@ import DataTableBody, { type Column } from '../components/DataTableBody'; | |||||||||||||||||
| import DataTablePagination from '../components/DataTablePagination'; | ||||||||||||||||||
| import type { | ||||||||||||||||||
| CampaignAssignmentSummaryRow, | ||||||||||||||||||
| CampaignAssignmentUniqueSubject, | ||||||||||||||||||
| CampaignOption, | ||||||||||||||||||
| } from '../../services/api/ServiceApi'; | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -49,7 +50,9 @@ const CampaignAssignmentTab: React.FC<CampaignAssignmentTabProps> = ({ | |||||||||||||||||
| const isMediumScreen = viewportWidth > 600 && viewportWidth <= 900; | ||||||||||||||||||
|
|
||||||||||||||||||
| const [grades, setGrades] = useState<CampaignOption[]>([]); | ||||||||||||||||||
| const [subjects, setSubjects] = useState<CampaignOption[]>([]); | ||||||||||||||||||
| const [subjects, setSubjects] = useState<CampaignAssignmentUniqueSubject[]>( | ||||||||||||||||||
| [], | ||||||||||||||||||
| ); | ||||||||||||||||||
| const [assignments, setAssignments] = useState< | ||||||||||||||||||
| CampaignAssignmentSummaryRow[] | ||||||||||||||||||
| >([]); | ||||||||||||||||||
|
|
@@ -99,6 +102,13 @@ const CampaignAssignmentTab: React.FC<CampaignAssignmentTabProps> = ({ | |||||||||||||||||
| }; | ||||||||||||||||||
| }, [api, campaignId]); | ||||||||||||||||||
|
|
||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||
| setSelectedGrades([]); | ||||||||||||||||||
| setSelectedSubjects([]); | ||||||||||||||||||
| setSubjects([]); | ||||||||||||||||||
| setPage(1); | ||||||||||||||||||
| }, [campaignId]); | ||||||||||||||||||
|
|
||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||
| let cancelled = false; | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -122,13 +132,14 @@ const CampaignAssignmentTab: React.FC<CampaignAssignmentTabProps> = ({ | |||||||||||||||||
| if (cancelled) return; | ||||||||||||||||||
|
|
||||||||||||||||||
| setAssignments(response.assignments ?? []); | ||||||||||||||||||
| setSubjects(response.uniqueSubjects ?? []); | ||||||||||||||||||
| if (selectedGrades.length === 0 && selectedSubjects.length === 0) { | ||||||||||||||||||
| setSubjects(response.uniqueSubjects ?? []); | ||||||||||||||||||
| } | ||||||||||||||||||
| setTotal(response.total ?? 0); | ||||||||||||||||||
| } catch (err) { | ||||||||||||||||||
| logger.error('Failed to load campaign assignments:', err); | ||||||||||||||||||
| if (!cancelled) { | ||||||||||||||||||
| setAssignments([]); | ||||||||||||||||||
| setSubjects([]); | ||||||||||||||||||
| setTotal(0); | ||||||||||||||||||
| } | ||||||||||||||||||
| } finally { | ||||||||||||||||||
|
|
@@ -162,6 +173,51 @@ const CampaignAssignmentTab: React.FC<CampaignAssignmentTabProps> = ({ | |||||||||||||||||
| [subjects], | ||||||||||||||||||
| ); | ||||||||||||||||||
|
|
||||||||||||||||||
| const availableSubjects = subjects; | ||||||||||||||||||
|
|
||||||||||||||||||
| const availableGrades = useMemo(() => { | ||||||||||||||||||
| const relevantGradeIds = new Set( | ||||||||||||||||||
| subjects.flatMap((subject) => subject.gradeIds), | ||||||||||||||||||
| ); | ||||||||||||||||||
|
Comment on lines
+178
to
+181
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To prevent potential runtime crashes if
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| return relevantGradeIds.size === 0 | ||||||||||||||||||
| ? grades | ||||||||||||||||||
| : grades.filter((grade) => relevantGradeIds.has(grade.id)); | ||||||||||||||||||
| }, [grades, subjects]); | ||||||||||||||||||
|
|
||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||
| const availableGradeIds = new Set(availableGrades.map((grade) => grade.id)); | ||||||||||||||||||
| setSelectedGrades((current) => { | ||||||||||||||||||
| if (current.length === 0) return current; | ||||||||||||||||||
| const next = current.filter((id) => availableGradeIds.has(id)); | ||||||||||||||||||
| return next.length === current.length ? current : next; | ||||||||||||||||||
| }); | ||||||||||||||||||
| }, [availableGrades]); | ||||||||||||||||||
|
|
||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||
| const availableSubjectIds = new Set( | ||||||||||||||||||
| availableSubjects.map((subject) => subject.id), | ||||||||||||||||||
| ); | ||||||||||||||||||
| setSelectedSubjects((current) => { | ||||||||||||||||||
| if (current.length === 0) return current; | ||||||||||||||||||
| const next = current.filter((id) => availableSubjectIds.has(id)); | ||||||||||||||||||
| return next.length === current.length ? current : next; | ||||||||||||||||||
| }); | ||||||||||||||||||
| }, [availableSubjects]); | ||||||||||||||||||
|
|
||||||||||||||||||
| const gradeOptionIds = useMemo( | ||||||||||||||||||
| () => availableGrades.map((grade) => grade.id), | ||||||||||||||||||
| [availableGrades], | ||||||||||||||||||
| ); | ||||||||||||||||||
| const subjectOptionIds = useMemo( | ||||||||||||||||||
| () => availableSubjects.map((subject) => subject.id), | ||||||||||||||||||
| [availableSubjects], | ||||||||||||||||||
| ); | ||||||||||||||||||
| const gradeSelectValues = | ||||||||||||||||||
| selectedGrades.length === 0 ? gradeOptionIds : selectedGrades; | ||||||||||||||||||
| const subjectSelectValues = | ||||||||||||||||||
| selectedSubjects.length === 0 ? subjectOptionIds : selectedSubjects; | ||||||||||||||||||
|
|
||||||||||||||||||
| const dateColumnWidth = isSmallScreen ? 180 : isMediumScreen ? 240 : 280; | ||||||||||||||||||
| const gradeColumnWidth = isSmallScreen ? 100 : 140; | ||||||||||||||||||
| const subjectColumnWidth = isSmallScreen ? 120 : 180; | ||||||||||||||||||
|
|
@@ -218,10 +274,16 @@ const CampaignAssignmentTab: React.FC<CampaignAssignmentTabProps> = ({ | |||||||||||||||||
| const selectedSubjectLabel = (id: string) => subjectNameById.get(id) ?? id; | ||||||||||||||||||
|
|
||||||||||||||||||
| const handleMultiSelectChange = | ||||||||||||||||||
| (setter: React.Dispatch<React.SetStateAction<string[]>>) => | ||||||||||||||||||
| ( | ||||||||||||||||||
| options: CampaignOption[], | ||||||||||||||||||
| setter: React.Dispatch<React.SetStateAction<string[]>>, | ||||||||||||||||||
| ) => | ||||||||||||||||||
| (event: SelectChangeEvent<string[]>) => { | ||||||||||||||||||
| const value = event.target.value; | ||||||||||||||||||
| setter(typeof value === 'string' ? value.split(',') : value); | ||||||||||||||||||
| const nextValues = typeof value === 'string' ? value.split(',') : value; | ||||||||||||||||||
| const optionIds = options.map((option) => option.id); | ||||||||||||||||||
|
|
||||||||||||||||||
| setter(nextValues.length === optionIds.length ? [] : nextValues); | ||||||||||||||||||
| setPage(1); | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -245,22 +307,44 @@ const CampaignAssignmentTab: React.FC<CampaignAssignmentTabProps> = ({ | |||||||||||||||||
| <Select | ||||||||||||||||||
| multiple | ||||||||||||||||||
| displayEmpty | ||||||||||||||||||
| value={selectedGrades} | ||||||||||||||||||
| onChange={handleMultiSelectChange(setSelectedGrades)} | ||||||||||||||||||
| value={gradeSelectValues} | ||||||||||||||||||
| onChange={handleMultiSelectChange( | ||||||||||||||||||
| availableGrades, | ||||||||||||||||||
| setSelectedGrades, | ||||||||||||||||||
| )} | ||||||||||||||||||
| input={<OutlinedInput />} | ||||||||||||||||||
| renderValue={(selected) => { | ||||||||||||||||||
| const values = selected as string[]; | ||||||||||||||||||
| if (values.length === 0) return t('All grades'); | ||||||||||||||||||
| if ( | ||||||||||||||||||
| values.length === 0 || | ||||||||||||||||||
| values.length === gradeOptionIds.length | ||||||||||||||||||
| ) { | ||||||||||||||||||
| return t('All grades'); | ||||||||||||||||||
| } | ||||||||||||||||||
| return values.map(selectedGradeLabel).join(', '); | ||||||||||||||||||
| }} | ||||||||||||||||||
| className="campaign-assignment-select" | ||||||||||||||||||
| MenuProps={{ | ||||||||||||||||||
| PaperProps: { className: 'campaign-assignment-menu' }, | ||||||||||||||||||
| }} | ||||||||||||||||||
| > | ||||||||||||||||||
| {grades.map((grade) => ( | ||||||||||||||||||
| <MenuItem key={grade.id} value={grade.id}> | ||||||||||||||||||
| <Checkbox checked={selectedGrades.includes(grade.id)} /> | ||||||||||||||||||
| {availableGrades.map((grade) => ( | ||||||||||||||||||
| <MenuItem | ||||||||||||||||||
| key={grade.id} | ||||||||||||||||||
| value={grade.id} | ||||||||||||||||||
| className={ | ||||||||||||||||||
| selectedGrades.length === 0 || | ||||||||||||||||||
| selectedGrades.includes(grade.id) | ||||||||||||||||||
| ? 'campaign-assignment-menu-item-selected' | ||||||||||||||||||
| : undefined | ||||||||||||||||||
| } | ||||||||||||||||||
| > | ||||||||||||||||||
| <Checkbox | ||||||||||||||||||
| checked={ | ||||||||||||||||||
| selectedGrades.length === 0 || | ||||||||||||||||||
| selectedGrades.includes(grade.id) | ||||||||||||||||||
| } | ||||||||||||||||||
| /> | ||||||||||||||||||
| <ListItemText primary={grade.name} /> | ||||||||||||||||||
| </MenuItem> | ||||||||||||||||||
| ))} | ||||||||||||||||||
|
|
@@ -283,22 +367,44 @@ const CampaignAssignmentTab: React.FC<CampaignAssignmentTabProps> = ({ | |||||||||||||||||
| <Select | ||||||||||||||||||
| multiple | ||||||||||||||||||
| displayEmpty | ||||||||||||||||||
| value={selectedSubjects} | ||||||||||||||||||
| onChange={handleMultiSelectChange(setSelectedSubjects)} | ||||||||||||||||||
| value={subjectSelectValues} | ||||||||||||||||||
| onChange={handleMultiSelectChange( | ||||||||||||||||||
| availableSubjects, | ||||||||||||||||||
| setSelectedSubjects, | ||||||||||||||||||
| )} | ||||||||||||||||||
| input={<OutlinedInput />} | ||||||||||||||||||
| renderValue={(selected) => { | ||||||||||||||||||
| const values = selected as string[]; | ||||||||||||||||||
| if (values.length === 0) return t('All subjects'); | ||||||||||||||||||
| if ( | ||||||||||||||||||
| values.length === 0 || | ||||||||||||||||||
| values.length === subjectOptionIds.length | ||||||||||||||||||
| ) { | ||||||||||||||||||
| return t('All subjects'); | ||||||||||||||||||
| } | ||||||||||||||||||
| return values.map(selectedSubjectLabel).join(', '); | ||||||||||||||||||
| }} | ||||||||||||||||||
| className="campaign-assignment-select" | ||||||||||||||||||
| MenuProps={{ | ||||||||||||||||||
| PaperProps: { className: 'campaign-assignment-menu' }, | ||||||||||||||||||
| }} | ||||||||||||||||||
| > | ||||||||||||||||||
| {subjects.map((subject) => ( | ||||||||||||||||||
| <MenuItem key={subject.id} value={subject.id}> | ||||||||||||||||||
| <Checkbox checked={selectedSubjects.includes(subject.id)} /> | ||||||||||||||||||
| {availableSubjects.map((subject) => ( | ||||||||||||||||||
| <MenuItem | ||||||||||||||||||
| key={subject.id} | ||||||||||||||||||
| value={subject.id} | ||||||||||||||||||
| className={ | ||||||||||||||||||
| selectedSubjects.length === 0 || | ||||||||||||||||||
| selectedSubjects.includes(subject.id) | ||||||||||||||||||
| ? 'campaign-assignment-menu-item-selected' | ||||||||||||||||||
| : undefined | ||||||||||||||||||
| } | ||||||||||||||||||
| > | ||||||||||||||||||
| <Checkbox | ||||||||||||||||||
| checked={ | ||||||||||||||||||
| selectedSubjects.length === 0 || | ||||||||||||||||||
| selectedSubjects.includes(subject.id) | ||||||||||||||||||
| } | ||||||||||||||||||
| /> | ||||||||||||||||||
| <ListItemText primary={subject.name} /> | ||||||||||||||||||
| </MenuItem> | ||||||||||||||||||
| ))} | ||||||||||||||||||
|
|
||||||||||||||||||
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
campaignIdchanges, thisuseEffectresets the filter states (selectedGrades,selectedSubjects,subjects,page). However, because state updates are scheduled asynchronously, theloadAssignmentseffect (which also depends oncampaignId) will immediately trigger a fetch using the newcampaignIdbut the old filter states. This results in a redundant API call that is immediately cancelled once the state resets are processed and trigger a second render.To prevent this redundant network request and potential race conditions, adjust the state during the render phase instead of using a
useEffecthook.