Skip to content

Commit 54733a2

Browse files
authored
refactor mutations to accept args on mutation function itself rather than on instantiation, then on success of the create enrollment hook redirect to the courseware url (#2480)
1 parent 60bfc76 commit 54733a2

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

frontends/api/src/mitxonline/hooks/enrollment/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,38 @@ import {
66
EnrollmentsApiEnrollmentsPartialUpdateRequest,
77
} from "@mitodl/mitxonline-api-axios/v2"
88

9-
const useCreateEnrollment = (opts: B2bApiB2bEnrollCreateRequest) => {
9+
const useCreateEnrollment = () => {
1010
const queryClient = useQueryClient()
1111
return useMutation({
12-
mutationFn: () => b2bApi.b2bEnrollCreate(opts),
13-
onSuccess: () => {
12+
mutationFn: (opts: B2bApiB2bEnrollCreateRequest) =>
13+
b2bApi.b2bEnrollCreate(opts),
14+
onSettled: () => {
1415
queryClient.invalidateQueries({
1516
queryKey: enrollmentKeys.courseRunEnrollmentsList(),
1617
})
1718
},
1819
})
1920
}
2021

21-
const useUpdateEnrollment = (
22-
opts: EnrollmentsApiEnrollmentsPartialUpdateRequest,
23-
) => {
22+
const useUpdateEnrollment = () => {
2423
const queryClient = useQueryClient()
2524
return useMutation({
26-
mutationFn: () => courseRunEnrollmentsApi.enrollmentsPartialUpdate(opts),
27-
onSuccess: () => {
25+
mutationFn: (opts: EnrollmentsApiEnrollmentsPartialUpdateRequest) =>
26+
courseRunEnrollmentsApi.enrollmentsPartialUpdate(opts),
27+
onSettled: () => {
2828
queryClient.invalidateQueries({
2929
queryKey: enrollmentKeys.courseRunEnrollmentsList(),
3030
})
3131
},
3232
})
3333
}
3434

35-
const useDestroyEnrollment = (enrollmentId: number) => {
35+
const useDestroyEnrollment = () => {
3636
const queryClient = useQueryClient()
3737
return useMutation({
38-
mutationFn: () =>
38+
mutationFn: (enrollmentId: number) =>
3939
courseRunEnrollmentsApi.enrollmentsDestroy({ id: enrollmentId }),
40-
onSuccess: () => {
40+
onSettled: () => {
4141
queryClient.invalidateQueries({
4242
queryKey: enrollmentKeys.courseRunEnrollmentsList(),
4343
})

frontends/main/src/app-pages/DashboardPage/CoursewareDisplay/DashboardCard.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { EnrollmentStatusIndicator } from "./EnrollmentStatusIndicator"
2828
import { EmailSettingsDialog, UnenrollDialog } from "./DashboardDialogs"
2929
import NiceModal from "@ebay/nice-modal-react"
3030
import { useCreateEnrollment } from "api/mitxonline-hooks/enrollment"
31+
import { redirect } from "next/navigation"
3132

3233
const CardRoot = styled.div<{
3334
screenSize: "desktop" | "mobile"
@@ -159,9 +160,7 @@ const CoursewareButton = styled(
159160
const hasStarted = startDate && isInPast(startDate)
160161
const hasEnrolled =
161162
enrollmentStatus && enrollmentStatus !== EnrollmentStatus.NotEnrolled
162-
const createEnrollment = useCreateEnrollment({
163-
readable_id: coursewareId ?? "",
164-
})
163+
const createEnrollment = useCreateEnrollment()
165164
return (hasStarted && href) || !hasEnrolled ? (
166165
hasEnrolled && href ? (
167166
<ButtonLink
@@ -181,7 +180,16 @@ const CoursewareButton = styled(
181180
className={className}
182181
disabled={createEnrollment.isPending || !coursewareId}
183182
onClick={async () => {
184-
await createEnrollment.mutateAsync()
183+
await createEnrollment.mutateAsync(
184+
{ readable_id: coursewareId ?? "" },
185+
{
186+
onSuccess: () => {
187+
if (href) {
188+
redirect(href)
189+
}
190+
},
191+
},
192+
)
185193
}}
186194
{...others}
187195
>

frontends/main/src/app-pages/DashboardPage/CoursewareDisplay/DashboardDialogs.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ const EmailSettingsDialogInner: React.FC<DashboardDialogProps> = ({
4242
receive_emails: enrollment.receiveEmails ?? true,
4343
},
4444
onSubmit: async () => {
45-
await updateEnrollment.mutateAsync()
45+
await updateEnrollment.mutateAsync({
46+
id: enrollment.id,
47+
PatchedUpdateCourseRunEnrollmentRequest: {
48+
receive_emails: formik.values.receive_emails,
49+
},
50+
})
4651
if (!updateEnrollment.isError) {
4752
modal.hide()
4853
}
4954
},
5055
})
51-
const updateEnrollment = useUpdateEnrollment({
52-
id: enrollment.id,
53-
PatchedUpdateCourseRunEnrollmentRequest: {
54-
receive_emails: formik.values.receive_emails,
55-
},
56-
})
56+
const updateEnrollment = useUpdateEnrollment()
5757
return (
5858
<FormDialog
5959
title={"Email Settings"}
@@ -121,14 +121,14 @@ const UnenrollDialogInner: React.FC<DashboardDialogProps> = ({
121121
enrollment,
122122
}) => {
123123
const modal = NiceModal.useModal()
124-
const destroyEnrollment = useDestroyEnrollment(enrollment.id)
124+
const destroyEnrollment = useDestroyEnrollment()
125125
const formik = useFormik({
126126
enableReinitialize: true,
127127
validateOnChange: false,
128128
validateOnBlur: false,
129129
initialValues: {},
130130
onSubmit: async () => {
131-
await destroyEnrollment.mutateAsync()
131+
await destroyEnrollment.mutateAsync(enrollment.id)
132132
if (!destroyEnrollment.isError) {
133133
modal.hide()
134134
}

0 commit comments

Comments
 (0)