-
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
5 changed files
with
157 additions
and
35 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
65 changes: 65 additions & 0 deletions
65
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,65 @@ | ||
import { Button } from '@hypothesis/frontend-shared'; | ||
import { useCallback, useMemo } from 'preact/hooks'; | ||
import { useParams } from 'wouter-preact'; | ||
|
||
import type { GradingSync } from '../../api-types'; | ||
import { useConfig } from '../../config'; | ||
import { useAPIFetch } 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 } = useConfig(['dashboard']); | ||
const { routes } = dashboard; | ||
|
||
const syncStatus = useAPIFetch<GradingSync>( | ||
replaceURLParams(routes.assignment_grades_sync, { | ||
assignment_id: assignmentId, | ||
}), | ||
); | ||
const buttonContent = useMemo(() => { | ||
if (syncStatus.isLoading || !syncStatus.data) { | ||
return 'Loading...'; | ||
} | ||
|
||
const { status } = syncStatus.data; | ||
if (['scheduled', 'in_progress'].includes(status)) { | ||
return 'Syncing grades'; | ||
} | ||
|
||
if (status === 'failed') { | ||
return 'Error syncing'; | ||
} | ||
|
||
if (studentsToSync.length > 0) { | ||
return `Sync ${studentsToSync.length} students`; | ||
} | ||
|
||
return 'Grades synced'; | ||
}, [studentsToSync.length, syncStatus.data, syncStatus.isLoading]); | ||
const buttonDisabled = useMemo(() => { | ||
// TODO This needs more checks, and there are more reasons for the button | ||
// to be disabled | ||
return syncStatus.isLoading || studentsToSync.length === 0; | ||
}, [studentsToSync.length, syncStatus.isLoading]); | ||
|
||
const syncGrades = useCallback(() => { | ||
console.log(studentsToSync); | ||
}, [studentsToSync]); | ||
|
||
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