Skip to content

Commit 28ee26d

Browse files
committed
Update apollo cache logic on updating programs and modules
1 parent 801f4b5 commit 28ee26d

File tree

6 files changed

+15
-102
lines changed

6 files changed

+15
-102
lines changed

frontend/src/app/my/mentorship/programs/[programKey]/edit/page.tsx

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import type React from 'react'
77
import { useState, useEffect } from 'react'
88
import { ErrorDisplay, handleAppError } from 'app/global-error'
99
import { UPDATE_PROGRAM } from 'server/mutations/programsMutations'
10-
import { GET_PROGRAM_DETAILS, GET_PROGRAM_AND_MODULES } from 'server/queries/programsQueries'
10+
import { GET_PROGRAM_DETAILS } from 'server/queries/programsQueries'
1111
import type { ExtendedSession } from 'types/auth'
12-
import { Module, Program } from 'types/mentorship'
1312
import { formatDateForInput } from 'utils/dateFormatter'
1413
import { parseCommaSeparated } from 'utils/parser'
1514
import slugify from 'utils/slugify'
@@ -104,41 +103,7 @@ const EditProgramPage = () => {
104103
status: formData.status,
105104
}
106105

107-
await updateProgram({
108-
variables: { input },
109-
update: (cache, { data: mutationData }) => {
110-
const updated = mutationData?.updateProgram
111-
if (!updated) return
112-
try {
113-
cache.writeQuery<{ getProgram?: Program; getProgramModules?: Module[] }>({
114-
query: GET_PROGRAM_DETAILS,
115-
variables: { programKey: input.key },
116-
data: { getProgram: updated },
117-
})
118-
} catch (_err) {
119-
handleAppError(_err)
120-
}
121-
try {
122-
const existing = cache.readQuery<{
123-
getProgram?: Program
124-
getProgramModules?: Module[]
125-
}>({
126-
query: GET_PROGRAM_AND_MODULES,
127-
variables: { programKey: input.key },
128-
})
129-
if (existing?.getProgram) {
130-
cache.writeQuery<{ getProgram?: Program; getProgramModules?: Module[] }>({
131-
query: GET_PROGRAM_AND_MODULES,
132-
variables: { programKey: input.key },
133-
data: { ...existing, getProgram: { ...existing.getProgram, ...updated } },
134-
})
135-
}
136-
} catch (_err) {
137-
handleAppError(_err)
138-
return
139-
}
140-
},
141-
})
106+
await updateProgram({ variables: { input } })
142107

143108
addToast({
144109
title: 'Program Updated',

frontend/src/app/my/mentorship/programs/[programKey]/modules/[moduleKey]/edit/page.tsx

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import React, { useEffect, useState } from 'react'
88
import { ErrorDisplay, handleAppError } from 'app/global-error'
99
import { UPDATE_MODULE } from 'server/mutations/moduleMutations'
1010
import { GET_PROGRAM_ADMINS_AND_MODULES } from 'server/queries/moduleQueries'
11-
import { GET_PROGRAM_AND_MODULES } from 'server/queries/programsQueries'
1211
import type { ExtendedSession } from 'types/auth'
13-
import { EXPERIENCE_LEVELS, Module, type ModuleFormData } from 'types/mentorship'
12+
import { EXPERIENCE_LEVELS, type ModuleFormData } from 'types/mentorship'
1413
import { formatDateForInput } from 'utils/dateFormatter'
1514
import { parseCommaSeparated } from 'utils/parser'
1615
import LoadingSpinner from 'components/LoadingSpinner'
@@ -109,32 +108,7 @@ const EditModulePage = () => {
109108
mentorLogins: parseCommaSeparated(formData.mentorLogins),
110109
}
111110

112-
await updateModule({
113-
variables: { input },
114-
update: (cache, { data: mutationData }) => {
115-
const updated = mutationData?.updateModule
116-
if (!updated) return
117-
try {
118-
const existing = cache.readQuery({
119-
query: GET_PROGRAM_AND_MODULES,
120-
variables: { programKey },
121-
}) as { getProgramModules: Module[] }
122-
if (existing?.getProgramModules) {
123-
const nextModules = existing.getProgramModules.map((m: Module) =>
124-
m.key === updated.key ? { ...m, ...updated } : m
125-
)
126-
cache.writeQuery({
127-
query: GET_PROGRAM_AND_MODULES,
128-
variables: { programKey },
129-
data: { ...existing, getProgramModules: nextModules },
130-
})
131-
}
132-
} catch (_err) {
133-
handleAppError(_err)
134-
return
135-
}
136-
},
137-
})
111+
await updateModule({ variables: { input } })
138112

139113
addToast({
140114
title: 'Module Updated',

frontend/src/app/my/mentorship/programs/[programKey]/page.tsx

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,32 +69,6 @@ const ProgramDetailsPage = () => {
6969
status: newStatus,
7070
},
7171
},
72-
update: (cache, { data: mutationData }) => {
73-
const updated = mutationData?.updateProgramStatus
74-
if (!updated) return
75-
try {
76-
const existing = cache.readQuery<{
77-
getProgram?: Program
78-
getProgramModules?: Module[]
79-
}>({
80-
query: GET_PROGRAM_AND_MODULES,
81-
variables: { programKey },
82-
})
83-
if (existing?.getProgram) {
84-
cache.writeQuery({
85-
query: GET_PROGRAM_AND_MODULES,
86-
variables: { programKey },
87-
data: {
88-
...existing,
89-
getProgram: { ...existing.getProgram, status: updated.status },
90-
},
91-
})
92-
}
93-
} catch (_err) {
94-
handleAppError(_err)
95-
return
96-
}
97-
},
9872
})
9973

10074
addToast({

frontend/src/middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ export default async function authenticationMiddleware(request: NextRequest) {
1313

1414
export const config = {
1515
//protected routes
16-
matcher: ['/my/mentorship/:path*'],
16+
matcher: [],
1717
}

frontend/src/types/__generated__/moduleMutations.generated.ts

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)