-
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.
Add button to sync grades in dashboard assignment view (#6700)
- Loading branch information
Showing
6 changed files
with
518 additions
and
48 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
128 changes: 128 additions & 0 deletions
128
lms/static/scripts/frontend_apps/components/dashboard/SyncGradesButton.tsx
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,128 @@ | ||
import { | ||
Button, | ||
LeaveIcon, | ||
SpinnerCircleIcon, | ||
} from '@hypothesis/frontend-shared'; | ||
import { useCallback, useMemo, useState } from 'preact/hooks'; | ||
import { useParams } from 'wouter-preact'; | ||
|
||
import type { GradingSync } from '../../api-types'; | ||
import { useConfig } from '../../config'; | ||
import { apiCall, usePolledAPIFetch } from '../../utils/api'; | ||
import type { QueryParams } from '../../utils/url'; | ||
import { replaceURLParams } from '../../utils/url'; | ||
|
||
export type SyncGradesButtonProps = { | ||
/** | ||
* List of students and their grades, which should be synced when the button | ||
* is clicked. | ||
* Passing `undefined` means students are not yet known and/or being loaded. | ||
*/ | ||
studentsToSync?: Array<{ h_userid: string; grade: number }>; | ||
}; | ||
|
||
export default function SyncGradesButton({ | ||
studentsToSync, | ||
}: SyncGradesButtonProps) { | ||
const { assignmentId } = useParams<{ assignmentId: string }>(); | ||
const { dashboard, api } = useConfig(['dashboard', 'api']); | ||
const { routes } = dashboard; | ||
const [schedulingSyncFailed, setSchedulingSyncFailed] = useState(false); | ||
|
||
const syncURL = useMemo( | ||
() => | ||
replaceURLParams(routes.assignment_grades_sync, { | ||
assignment_id: assignmentId, | ||
}), | ||
[assignmentId, routes.assignment_grades_sync], | ||
); | ||
const [lastSyncParams, setLastSyncParams] = useState<QueryParams>({}); | ||
const lastSync = usePolledAPIFetch<GradingSync>({ | ||
path: syncURL, | ||
params: lastSyncParams, | ||
// Keep polling as long as sync is in progress | ||
shouldRefresh: result => | ||
!result.data || ['scheduled', 'in_progress'].includes(result.data.status), | ||
}); | ||
|
||
const buttonContent = useMemo(() => { | ||
if (!studentsToSync || (lastSync.isLoading && !lastSync.data)) { | ||
return 'Loading...'; | ||
} | ||
|
||
if ( | ||
lastSync.data && | ||
['scheduled', 'in_progress'].includes(lastSync.data.status) | ||
) { | ||
return ( | ||
<> | ||
Syncing grades | ||
<SpinnerCircleIcon className="ml-1.5" /> | ||
</> | ||
); | ||
} | ||
|
||
// TODO Maybe these should be represented differently | ||
if ( | ||
schedulingSyncFailed || | ||
lastSync.error || | ||
lastSync.data?.status === 'failed' | ||
) { | ||
return ( | ||
<> | ||
Error syncing. Click to retry | ||
<LeaveIcon /> | ||
</> | ||
); | ||
} | ||
|
||
if (studentsToSync.length > 0) { | ||
return `Sync ${studentsToSync.length} grades`; | ||
} | ||
|
||
return 'Grades synced'; | ||
}, [ | ||
studentsToSync, | ||
lastSync.isLoading, | ||
lastSync.data, | ||
lastSync.error, | ||
schedulingSyncFailed, | ||
]); | ||
|
||
const buttonDisabled = | ||
lastSync.isLoading || | ||
lastSync.data?.status === 'scheduled' || | ||
lastSync.data?.status === 'in_progress' || | ||
!studentsToSync || | ||
studentsToSync.length === 0; | ||
|
||
const syncGrades = useCallback(async () => { | ||
lastSync.mutate({ status: 'scheduled' }); | ||
setSchedulingSyncFailed(false); | ||
|
||
await apiCall({ | ||
authToken: api.authToken, | ||
path: syncURL, | ||
method: 'POST', | ||
data: { | ||
grades: (studentsToSync ?? []).map(({ grade, h_userid }) => ({ | ||
h_userid, | ||
// FIXME This will be fixed separately, but the BE is currently | ||
// returning grades from 0 to 100, but expects them to be sent back | ||
// from 0 to 1. | ||
grade: grade / 100, | ||
})), | ||
}, | ||
}).catch(() => setSchedulingSyncFailed(true)); | ||
|
||
// Once the request succeeds, we update the params so that polling the | ||
// status is triggered again | ||
setLastSyncParams({ t: `${Date.now()}` }); | ||
}, [api.authToken, lastSync, studentsToSync, syncURL]); | ||
|
||
return ( | ||
<Button variant="primary" onClick={syncGrades} disabled={buttonDisabled}> | ||
{buttonContent} | ||
</Button> | ||
); | ||
} |
Oops, something went wrong.