-
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.
- Loading branch information
Showing
6 changed files
with
244 additions
and
44 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
93 changes: 93 additions & 0 deletions
93
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,93 @@ | ||
import { Button, 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 { replaceURLParams } from '../../utils/url'; | ||
|
||
export type SyncGradesButtonProps = { | ||
/** | ||
* List of students and their grades, which should be synced when the button | ||
* is clicked. | ||
*/ | ||
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 [syncing, setSyncing] = useState(false); | ||
|
||
const syncUrl = useMemo( | ||
() => | ||
replaceURLParams(routes.assignment_grades_sync ?? ':assignment_id', { | ||
assignment_id: assignmentId, | ||
}), | ||
[assignmentId, routes.assignment_grades_sync], | ||
); | ||
const lastSync = usePolledAPIFetch<GradingSync>({ | ||
path: syncUrl, | ||
// Keep polling as long as sync is in progress | ||
shouldRetry: result => | ||
!!result.data && | ||
['scheduled', 'in_progress'].includes(result.data.status), | ||
}); | ||
|
||
const buttonContent = useMemo(() => { | ||
if (lastSync.isLoading) { | ||
return 'Loading...'; | ||
} | ||
|
||
if ( | ||
syncing || | ||
(lastSync.data && | ||
['scheduled', 'in_progress'].includes(lastSync.data.status)) | ||
) { | ||
return ( | ||
<> | ||
Syncing grades | ||
<SpinnerCircleIcon className="ml-1.5" /> | ||
</> | ||
); | ||
} | ||
|
||
if (lastSync.data?.status === 'failed') { | ||
return 'Error syncing'; | ||
} | ||
|
||
if (studentsToSync.length > 0) { | ||
return `Sync ${studentsToSync.length} students`; | ||
} | ||
|
||
return 'Grades synced'; | ||
}, [lastSync.isLoading, lastSync.data, syncing, studentsToSync.length]); | ||
|
||
const buttonDisabled = useMemo( | ||
() => syncing || lastSync.isLoading || studentsToSync.length === 0, | ||
[syncing, lastSync.isLoading, studentsToSync.length], | ||
); | ||
|
||
const syncGrades = useCallback(() => { | ||
setSyncing(true); | ||
|
||
apiCall({ | ||
authToken: api.authToken, | ||
path: syncUrl, | ||
method: 'POST', | ||
data: { | ||
grades: studentsToSync, | ||
}, | ||
}).finally(() => setSyncing(false)); | ||
}, [api.authToken, studentsToSync, syncUrl]); | ||
|
||
return ( | ||
<Button variant="primary" onClick={syncGrades} disabled={buttonDisabled}> | ||
{buttonContent} | ||
</Button> | ||
); | ||
} |
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