Skip to content

Commit d61dcba

Browse files
authored
Merge pull request #178 from MEITREX/refetch-course-data
Refetch when navigate back
2 parents c281c58 + 347796f commit d61dcba

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

components/courses/student/StudentCourseLayout.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,34 @@ export default function CourseLayout({
118118
const router = useRouter();
119119
const [error, setError] = useState<any>(null);
120120
const confirm = useConfirmation();
121+
const [refreshKey, setRefreshKey] = useState(0);
121122

122123
const pathname = usePathname();
123124
const noContentPaths = ["/quiz", "/media", "/quiz", "/submissions"];
124125
const hideContent = noContentPaths.some((path) => pathname?.includes(path));
125126

126127
const data = useLazyLoadQuery<StudentCourseLayoutCourseIdQuery>(
127128
studentCourseIdQuery,
128-
{ id: courseId }
129+
{ id: courseId },
130+
{ fetchPolicy: "network-only", fetchKey: refreshKey }
129131
);
132+
130133
const course = data.coursesByIds?.[0];
131134
const userId = data.currentUserInfo.id;
132135

136+
// Force a layout remount to refetch data when navigating back.
137+
// In Next.js, layouts stay mounted between subpages, so their data requests aren't re-run.
138+
// This simple refresh ensures updated data (e.g. course progress).
139+
// The clean solution would be to move data fetching into the subpages,
140+
// so data refetches automatically when those components are mounted.
141+
useEffect(() => {
142+
const chaptersPageRegex = /^\/courses\/[^\/]+(\/chapters)?$/;
143+
const coursePageRegex = /^\/courses\/[^\/]+$/;
144+
if (coursePageRegex.test(pathname) || chaptersPageRegex.test(pathname)) {
145+
setRefreshKey((prev) => prev + 1);
146+
}
147+
}, [pathname]);
148+
133149
const [leave] = useMutation<StudentCourseLayoutLeaveMutation>(graphql`
134150
mutation StudentCourseLayoutLeaveMutation($courseId: UUID!) {
135151
leaveCourse(courseId: $courseId) {

0 commit comments

Comments
 (0)