Skip to content

Commit 207d870

Browse files
authored
Merge pull request #216 from mp-access/213-improve-example-status-context
213 improve example status context
2 parents e452588 + 7fe1122 commit 207d870

3 files changed

Lines changed: 45 additions & 29 deletions

File tree

src/components/Hooks.tsx

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ const usePath = (prefix: string): string[] => {
6060
"assignments",
6161
assignmentSlug,
6262
prefix !== "assignments" && ["tasks", taskSlug],
63-
]
64-
)
65-
)
63+
],
64+
),
65+
),
6666
)
6767
}
6868

6969
export const useCreate = (slug: string) => {
7070
const target = slug === "" ? "/create" : "/edit"
7171
const { mutate, isLoading } = useMutation<string, AxiosError, object>(
7272
(repository) => axios.post(target, repository),
73-
{ onSuccess: () => window.location.reload() }
73+
{ onSuccess: () => window.location.reload() },
7474
)
7575
return { mutate, isLoading }
7676
}
@@ -79,7 +79,7 @@ export const usePull = () => {
7979
const path = usePath("")
8080
const { mutate, isLoading } = useMutation(
8181
() => axios.post("/courses" + `/${path[1]}/pull`, {}),
82-
{ onSuccess: () => window.location.reload() }
82+
{ onSuccess: () => window.location.reload() },
8383
)
8484
return { mutate, isLoading }
8585
}
@@ -121,6 +121,17 @@ export const useExtendExample = () => {
121121
return { extendExampleDuration }
122122
}
123123

124+
export const useInteractiveExample = (options: UseQueryOptions = {}) => {
125+
const { courseSlug } = useParams()
126+
127+
return useQuery<InteractiveExampleDTO>(
128+
["courses", courseSlug, "examples", "interactive"],
129+
{
130+
enabled: options.enabled,
131+
},
132+
)
133+
}
134+
124135
export const useTerminate = () => {
125136
const { courseSlug, exampleSlug } = useParams()
126137
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -175,7 +186,7 @@ export const useCategorize = () => {
175186
}
176187

177188
export const useExamples = (
178-
options: UseQueryOptions<PointDistribution> = {}
189+
options: UseQueryOptions<PointDistribution> = {},
179190
) => {
180191
const { courseSlug } = useParams()
181192

@@ -186,7 +197,7 @@ export const useExamples = (
186197

187198
export const usePendingSubmissions = (
188199
userId: string,
189-
options: UseQueryOptions<PointDistribution> = {}
200+
options: UseQueryOptions<PointDistribution> = {},
190201
) => {
191202
const { courseSlug, exampleSlug } = useParams()
192203
return useQuery<NewSubmissionProps[]>(
@@ -202,7 +213,7 @@ export const usePendingSubmissions = (
202213
{
203214
enabled: options.enabled,
204215
refetchOnMount: "always",
205-
}
216+
},
206217
)
207218
}
208219

@@ -242,7 +253,7 @@ export const useAssignment = () => {
242253
const { courseSlug, assignmentSlug } = useParams()
243254
return useQuery<AssignmentProps>(
244255
["courses", courseSlug, "assignments", assignmentSlug],
245-
{ enabled: !!assignmentSlug }
256+
{ enabled: !!assignmentSlug },
246257
)
247258
}
248259

@@ -251,15 +262,15 @@ export const useExample = (userId: string) => {
251262
const { courseSlug, exampleSlug } = useParams()
252263
const query = useQuery<TaskProps>(
253264
["courses", courseSlug, "examples", exampleSlug, "users", userId],
254-
{ enabled: !timer, refetchOnMount: "always" }
265+
{ enabled: !timer, refetchOnMount: "always" },
255266
)
256267
// eslint-disable-next-line
257268
const { mutateAsync } = useMutation<any, AxiosError, any[]>(
258269
["submit", courseSlug, "exampels", exampleSlug],
259270
{
260271
onMutate: () => setTimer(Date.now() + 30000),
261272
onSettled: () => setTimer(undefined),
262-
}
273+
},
263274
)
264275
const submit = (data: NewSubmissionProps) =>
265276
mutateAsync([
@@ -283,7 +294,7 @@ export const useTask = (userId: string) => {
283294
"users",
284295
userId,
285296
],
286-
{ enabled: !timer }
297+
{ enabled: !timer },
287298
)
288299
// eslint-disable-next-line
289300
const { mutateAsync } = useMutation<any, AxiosError, any[]>(
@@ -292,7 +303,7 @@ export const useTask = (userId: string) => {
292303
onMutate: () => setTimer(Date.now() + 30000),
293304
onSettled: () => setTimer(undefined),
294305
onSuccess: query.refetch,
295-
}
306+
},
296307
)
297308
const submit = (data: NewSubmissionProps) =>
298309
mutateAsync([
@@ -312,7 +323,7 @@ export const useTask = (userId: string) => {
312323

313324
export const useCountdown = (start: number | null, end: number | null) => {
314325
const [timeLeftInSeconds, setTimeLeftInSeconds] = useState<number | null>(
315-
null
326+
null,
316327
)
317328
const [circleValue, setCircleValue] = useState<number | null>(null)
318329

@@ -416,7 +427,7 @@ export const useInspect = () => {
416427

417428
if (!courseSlug || !exampleSlug) {
418429
throw new Error(
419-
`Course Slug ${courseSlug} or example slug ${exampleSlug} undefined`
430+
`Course Slug ${courseSlug} or example slug ${exampleSlug} undefined`,
420431
)
421432
}
422433

@@ -450,15 +461,15 @@ export const useStudentSubmissions = () => {
450461
}
451462

452463
export const useExamplePointDistribution = (
453-
options: UseQueryOptions<PointDistribution> = {}
464+
options: UseQueryOptions<PointDistribution> = {},
454465
) => {
455466
const { courseSlug, exampleSlug } = useParams()
456467
return useQuery<PointDistribution>(
457468
["courses", courseSlug, "examples", exampleSlug, "point-distribution"],
458469
{
459470
enabled: options.enabled,
460471
...options,
461-
}
472+
},
462473
)
463474
}
464475

@@ -488,10 +499,10 @@ const getStorageValue = <T,>(key: string, defaultValue: T) => {
488499

489500
export const useLocalStorage = <T,>(
490501
key: string,
491-
defaultValue: T
502+
defaultValue: T,
492503
): [T, Dispatch<SetStateAction<T>>] => {
493504
const [value, setValue] = useState<T>(() =>
494-
getStorageValue(key, defaultValue)
505+
getStorageValue(key, defaultValue),
495506
)
496507

497508
useEffect(() => {

src/context/ExampleStatusContext.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createContext, useEffect, useState } from "react"
22
import { useParams } from "react-router-dom"
3-
import { useExamples } from "../components/Hooks"
3+
import { useInteractiveExample } from "../components/Hooks"
44

55
type ExampleStatusContextType =
66
| {
@@ -27,24 +27,25 @@ export const ExampleStatusProvider: React.FC<{ children: React.ReactNode }> = ({
2727
children,
2828
}) => {
2929
const { courseSlug } = useParams()
30-
const { data: examples } = useExamples({ enabled: !!courseSlug })
30+
const { data: interactiveExampleDTO } = useInteractiveExample({
31+
enabled: !!courseSlug,
32+
})
3133
const [status, setStatus] = useState<ExampleStatusContextType>({
3234
hasInteractive: false,
3335
})
3436

3537
useEffect(() => {
36-
if (!courseSlug || !examples) return
37-
38-
const interactiveExample = examples.find(
39-
(example) => example.status === "Interactive",
40-
)
38+
if (!courseSlug || !interactiveExampleDTO) return
4139

42-
if (interactiveExample) {
43-
setStatus({ hasInteractive: true, exampleSlug: interactiveExample.slug })
40+
if (interactiveExampleDTO.exampleSlug) {
41+
setStatus({
42+
hasInteractive: true,
43+
exampleSlug: interactiveExampleDTO.exampleSlug,
44+
})
4445
} else {
4546
setStatus({ hasInteractive: false })
4647
}
47-
}, [courseSlug, examples])
48+
}, [courseSlug, interactiveExampleDTO])
4849

4950
const setInteractive = (slug: string) =>
5051
setStatus({ hasInteractive: true, exampleSlug: slug })

src/types.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,7 @@ declare interface ExampleSubmissionsDTO extends ExampleInformation {
279279
declare interface ExampleResetSsePayload {
280280
exampleSlug: string
281281
}
282+
283+
declare interface InteractiveExampleDTO {
284+
exampleSlug: string | null
285+
}

0 commit comments

Comments
 (0)