Skip to content

Commit

Permalink
add generate teams mutation hook (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Opeyem1a authored Aug 26, 2024
1 parent 3d91f5e commit 3c24caf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/app/(app)/course/[courseId]/setup/(hooks)/useSetupSteps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useImportStudentsFromLms } from "@/hooks/use-import-students-from-lms"
import { useImportStudentGradebookData } from "@/hooks/use-import-student-gradebook-data"
import { GenerateOptInQuiz } from "@/app/(app)/course/[courseId]/setup/(components)/GenerateOptInQuiz"
import { useOnboardingProgress } from "@/hooks/use-onboarding-progress"
import { useGenerateTeams } from "@/hooks/use-generate-teams"

interface UseSetupStepsReturnType {
steps: NonEmptyArray<StepDefinition>;
Expand All @@ -21,12 +22,13 @@ export const useSetupSteps = (): UseSetupStepsReturnType => {

const { importStudentsFromLmsAsync } = useImportStudentsFromLms()
const { importStudentGradebookData } = useImportStudentGradebookData()

// todo: generate teams mutation
const { generateTeamsAsync } = useGenerateTeams()

if (!data || isLoading) {
return {
steps: ORDERED_STEPS.map((step) => BASE_STEPS[step]) as NonEmptyArray<StepDefinition>,
steps: ORDERED_STEPS.map(
(step) => BASE_STEPS[step],
) as NonEmptyArray<StepDefinition>,
isLoading,
onboardingProgress: data,
}
Expand All @@ -41,6 +43,10 @@ export const useSetupSteps = (): UseSetupStepsReturnType => {
content: "Import gradebook data",
onClick: () => importStudentGradebookData(undefined),
},
GENERATE_TEAMS: {
content: "Generate teams",
onClick: () => generateTeamsAsync(undefined),
},
}

const currentStep = getCurrentStep(data)
Expand Down
36 changes: 36 additions & 0 deletions src/hooks/use-generate-teams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useToast } from "@/hooks/use-toast"
import { useCourse } from "@/app/(app)/course/[courseId]/(hooks)/useCourse"
import { useMutation } from "@tanstack/react-query"
import { defaultMutationFn } from "@/app/(providers)/query-client-provider"

export const useGenerateTeams = () => {
const { toast } = useToast()
const { courseId } = useCourse()
const mutation = useMutation<void, any, void>({
mutationFn: async () => {
return defaultMutationFn(
`courses/${courseId}/generate_teams/`,
undefined,
{ allowEmptyResponse: true },
)
},
onSuccess: () => {
toast({
title: "Teams generated successfully",
})
},
onError: () => {
toast({
variant: "destructive",
title: "Unable to generate teams",
description:
"Something went wrong while generating teams. Please try again later.",
})
},
})

return {
generateTeamsAsync: mutation.mutateAsync,
...mutation,
}
}
2 changes: 1 addition & 1 deletion src/hooks/use-import-student-gradebook-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { defaultMutationFn } from "@/app/(providers)/query-client-provider"
export const useImportStudentGradebookData = () => {
const { toast } = useToast()
const { courseId } = useCourse()
const mutation = useMutation<undefined, unknown, undefined>({
const mutation = useMutation<void, unknown, void>({
mutationFn: async () => {
return defaultMutationFn(
`courses/${courseId}/import_gradebook_attribute_from_lms/`,
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/use-import-students-from-lms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { defaultMutationFn } from "@/app/(providers)/query-client-provider"
export const useImportStudentsFromLms = () => {
const { toast } = useToast()
const { courseId } = useCourse()
const mutation = useMutation<undefined, unknown, undefined>({
const mutation = useMutation<void, unknown, void>({
mutationFn: async () => {
return defaultMutationFn(`courses/${courseId}/import_students_from_lms/`, undefined, {allowEmptyResponse: true})
},
Expand Down

0 comments on commit 3c24caf

Please sign in to comment.