Skip to content

Commit 0d69245

Browse files
committed
Only refetch when navigating from media content
1 parent d61dcba commit 0d69245

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

components/courses/student/StudentCourseLayout.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import ExitToAppIcon from "@mui/icons-material/ExitToApp";
1414
import { Box, Button, Typography } from "@mui/material";
1515
import { useParams, usePathname, useRouter } from "next/navigation";
1616
import * as React from "react";
17-
import { useEffect, useState } from "react";
17+
import { useEffect, useRef, useState } from "react";
1818
import { graphql, useLazyLoadQuery, useMutation } from "react-relay";
1919

2020
const studentCourseIdQuery = graphql`
@@ -138,12 +138,25 @@ export default function CourseLayout({
138138
// This simple refresh ensures updated data (e.g. course progress).
139139
// The clean solution would be to move data fetching into the subpages,
140140
// so data refetches automatically when those components are mounted.
141+
const prevPathname = useRef(pathname);
142+
141143
useEffect(() => {
142-
const chaptersPageRegex = /^\/courses\/[^\/]+(\/chapters)?$/;
143-
const coursePageRegex = /^\/courses\/[^\/]+$/;
144-
if (coursePageRegex.test(pathname) || chaptersPageRegex.test(pathname)) {
144+
const subPages = [
145+
/^\/courses\/[^\/]+\/forum$/,
146+
/^\/courses\/[^\/]+\/progress$/,
147+
/^\/courses\/[^\/]+\/quests$/,
148+
/^\/courses\/[^\/]+\/leaderboard$/,
149+
/^\/courses\/[^\/]+\/chapters$/,
150+
/^\/courses\/[^\/]+$/,
151+
];
152+
153+
const wasSubPage = subPages.some((regex) => regex.test(prevPathname.current));
154+
155+
if ((!wasSubPage && (pathname.match(/^\/courses\/[^\/]+$/)) || (!wasSubPage && (pathname.match(/^\/courses\/[^\/]+\/chapters$/))))) {
145156
setRefreshKey((prev) => prev + 1);
146157
}
158+
159+
prevPathname.current = pathname;
147160
}, [pathname]);
148161

149162
const [leave] = useMutation<StudentCourseLayoutLeaveMutation>(graphql`

0 commit comments

Comments
 (0)