-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create hook to track selected dashboard filters in query string
- Loading branch information
Showing
8 changed files
with
250 additions
and
64 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 |
---|---|---|
|
@@ -2,11 +2,13 @@ import { MultiSelect } from '@hypothesis/frontend-shared'; | |
import { | ||
checkAccessibility, | ||
mockImportedComponents, | ||
waitFor, | ||
} from '@hypothesis/frontend-testing'; | ||
import { mount } from 'enzyme'; | ||
import sinon from 'sinon'; | ||
|
||
import { Config } from '../../../config'; | ||
import { urlPath } from '../../../utils/api'; | ||
import DashboardActivityFilters, { | ||
$imports, | ||
} from '../DashboardActivityFilters'; | ||
|
@@ -35,17 +37,20 @@ describe('DashboardActivityFilters', () => { | |
const studentsWithName = [ | ||
{ | ||
lms_id: '1', | ||
h_userid: 'acct:[email protected]', | ||
display_name: 'First student', | ||
}, | ||
{ | ||
lms_id: '2', | ||
h_userid: 'acct:[email protected]', | ||
display_name: 'Second student', | ||
}, | ||
]; | ||
const students = [ | ||
...studentsWithName, | ||
{ | ||
lms_id: '3', | ||
h_userid: 'acct:[email protected]', | ||
display_name: '', // Student with an empty name won't be displayed | ||
}, | ||
]; | ||
|
@@ -204,33 +209,85 @@ describe('DashboardActivityFilters', () => { | |
[ | ||
{ | ||
id: 'courses-select', | ||
selection: courses.map(c => `${c.id}`), | ||
getExpectedCallback: () => onCoursesChange, | ||
}, | ||
{ | ||
id: 'assignments-select', | ||
selection: assignments.map(a => `${a.id}`), | ||
getExpectedCallback: () => onAssignmentsChange, | ||
}, | ||
{ | ||
id: 'students-select', | ||
selection: studentsWithName.map(s => s.h_userid), | ||
getExpectedCallback: () => onStudentsChange, | ||
}, | ||
].forEach(({ id, getExpectedCallback }) => { | ||
].forEach(({ id, selection, getExpectedCallback }) => { | ||
it('invokes corresponding change callback', () => { | ||
const wrapper = createComponent(); | ||
const select = getSelect(wrapper, id); | ||
|
||
select.props().onChange(); | ||
assert.called(getExpectedCallback()); | ||
select.props().onChange(selection); | ||
assert.calledWith(getExpectedCallback(), selection); | ||
}); | ||
}); | ||
|
||
[ | ||
{ | ||
initialQueryString: `?course_id=${courses[0].id}&assignment_id=${assignments[0].id}`, | ||
expectedCourses: [`${courses[0].id}`], | ||
expectedAssignments: [`${assignments[0].id}`], | ||
expectedStudents: [], | ||
}, | ||
{ | ||
initialQueryString: `?course_id=${courses[0].id}&course_id=${courses[1].id}`, | ||
expectedCourses: courses.map(c => `${c.id}`), | ||
expectedAssignments: [], | ||
expectedStudents: [], | ||
}, | ||
{ | ||
initialQueryString: urlPath`?h_userid=${studentsWithName[0].h_userid}&h_userid=${studentsWithName[1].h_userid}&assignment_id=${assignments[1].id}`, | ||
expectedCourses: [], | ||
expectedAssignments: [`${assignments[1].id}`], | ||
expectedStudents: studentsWithName.map(s => s.h_userid), | ||
}, | ||
{ | ||
initialQueryString: urlPath`?h_userid=${studentsWithName[1].h_userid}`, | ||
expectedCourses: [], | ||
expectedAssignments: [], | ||
expectedStudents: [studentsWithName[1].h_userid], | ||
}, | ||
].forEach( | ||
({ | ||
initialQueryString, | ||
expectedCourses, | ||
expectedAssignments, | ||
expectedStudents, | ||
}) => { | ||
it.skip('should initialize selection based on query string', async () => { | ||
history.replaceState(null, '', initialQueryString); | ||
|
||
createComponent(); | ||
|
||
// Callbacks will be invoked only after corresponding courses, | ||
// assignments and students have been asynchronously loaded via | ||
// useAPIFetch, so we need to wait for it. | ||
await waitFor(() => onCoursesChange.called); | ||
|
||
assert.calledWith(onCoursesChange, expectedCourses); | ||
assert.calledWith(onAssignmentsChange, expectedAssignments); | ||
assert.calledWith(onStudentsChange, expectedStudents); | ||
}); | ||
}, | ||
); | ||
|
||
context('when items are selected', () => { | ||
[0, 1].forEach(index => { | ||
it('shows item name when only one is selected', () => { | ||
const wrapper = createComponent({ | ||
selectedCourses: [courses[index]], | ||
selectedAssignments: [assignments[index]], | ||
selectedStudents: [students[index]], | ||
selectedCourses: [`${courses[index].id}`], | ||
selectedAssignments: [`${assignments[index].id}`], | ||
selectedStudents: [students[index].h_userid], | ||
}); | ||
|
||
assert.equal( | ||
|
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,54 @@ | ||
import { useCallback, useMemo } from 'preact/hooks'; | ||
import { useLocation, useSearch } from 'wouter-preact'; | ||
|
||
import { queryStringToRecord, recordToQueryStringFragment } from '../url'; | ||
|
||
export type DashboardFilters = { | ||
studentIds: string[]; | ||
assignmentIds: string[]; | ||
courseIds: string[]; | ||
}; | ||
|
||
export type UseDashboardQueryFilters = { | ||
filters: DashboardFilters; | ||
updateFilters: (filtersToUpdate: Partial<DashboardFilters>) => void; | ||
}; | ||
|
||
/** | ||
* Allows to track dashboard filters as a piece of state synced in the query string | ||
*/ | ||
export function useDashboardQueryFilters(): UseDashboardQueryFilters { | ||
const search = useSearch(); | ||
const queryParams = useMemo(() => queryStringToRecord(search), [search]); | ||
const filters = useMemo(() => { | ||
const { student_id = [], course_id = [], assignment_id = [] } = queryParams; | ||
const courseIds = Array.isArray(course_id) ? course_id : [course_id]; | ||
const assignmentIds = Array.isArray(assignment_id) | ||
? assignment_id | ||
: [assignment_id]; | ||
const studentIds = Array.isArray(student_id) ? student_id : [student_id]; | ||
|
||
return { courseIds, assignmentIds, studentIds }; | ||
}, [queryParams]); | ||
|
||
const [, navigate] = useLocation(); | ||
const updateFilters = useCallback( | ||
({ courseIds, assignmentIds, studentIds }: Partial<DashboardFilters>) => { | ||
const newQueryParams = { ...queryParams }; | ||
if (courseIds) { | ||
newQueryParams.course_id = courseIds; | ||
} | ||
if (assignmentIds) { | ||
newQueryParams.assignment_id = assignmentIds; | ||
} | ||
if (studentIds) { | ||
newQueryParams.student_id = studentIds; | ||
} | ||
|
||
navigate(recordToQueryStringFragment(newQueryParams), { replace: true }); | ||
}, | ||
[navigate, queryParams], | ||
); | ||
|
||
return { filters, updateFilters }; | ||
} |
Oops, something went wrong.