Skip to content

Commit 7baa346

Browse files
committed
fix: fixed state management for courseList
1 parent fdffd5a commit 7baa346

2 files changed

Lines changed: 23 additions & 40 deletions

File tree

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,31 @@
1-
import React, { useState, useEffect } from 'react';
2-
import { useSelector, useDispatch } from 'react-redux';
3-
import { setCourses } from '../../store/actions';
1+
import React from 'react';
2+
import { useSelector } from 'react-redux';
43
import { Link } from 'react-router-dom';
54
import Course from './Course';
6-
import { useCoursesQuery } from '../../generated/graphql';
75
import CourseType from '../../Types/course';
86

97
const listStyle = 'w-full text-left';
108

119
const CourseList: React.FC = () => {
12-
const dispatch = useDispatch();
13-
const [{ fetching, data, error }] = useCoursesQuery({ variables: {} });
14-
const courses = data?.getCourses?.data;
15-
1610
const coursesState = useSelector(
17-
(state: { courses: CourseType[] | null }) => state.courses // initialze with empty array instead of null
11+
(state: { courses: CourseType[] | null }) => state.courses // TODO: initialze with empty array instead of null
1812
);
19-
// const [state, setState] = useState(coursesState);
20-
21-
// useEffect(() => {
22-
// setState(coursesState);
23-
// }, [coursesState]);
24-
25-
useEffect(() => {
26-
if (courses) {
27-
// @ts-ignore
28-
dispatch(setCourses(courses));
29-
}
30-
}, []);
31-
32-
if (fetching) {
33-
} // TODO handle fetching
34-
if (error) {
35-
} // TODO handle error
36-
37-
// if (courses) {
38-
// // @ts-ignore
39-
// dispatch(setCourses(courses));
4013

4114
return (
4215
<div className={listStyle}>
43-
{
16+
{!!coursesState &&
4417
// useredux state here instead of DB response
45-
coursesState!.map((course) => {
18+
coursesState.map((course) => {
4619
return (
4720
<article key={course?.id}>
4821
<Link to={`/homepage/classes/${course?.id}`}>
4922
{!!course && <Course course={course} />}
5023
</Link>
5124
</article>
5225
);
53-
})
54-
}
26+
})}
5527
</div>
5628
);
5729
};
5830

59-
// return <article></article>;
60-
// };
61-
6231
export default CourseList;

client/src/Components/TeacherHomepage/Homepage.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
import React from 'react';
2-
import { useSelector } from 'react-redux';
2+
import { useSelector, useDispatch } from 'react-redux';
33
import { Outlet } from 'react-router-dom';
44
import Sidemenu from '../TeacherDashboard/Sidemenu';
55
import Navbar from './Navbar';
6-
import User from '../../Types/user';
76
import Course from '../../Types/course';
87
import Instruction from './Instruction';
8+
import { useCoursesQuery } from '../../generated/graphql';
9+
import { setCourses } from '../../store/actions';
910

1011
const Homepage: React.FC = () => {
12+
const dispatch = useDispatch();
13+
const [{ fetching, data, error }] = useCoursesQuery({ variables: {} });
14+
const courses = data?.getCourses?.data;
15+
16+
if (fetching) {
17+
} // TODO handle fetching
18+
if (error) {
19+
} // TODO handle error
20+
if (courses) {
21+
// @ts-ignore
22+
dispatch(setCourses(courses));
23+
}
24+
1125
const selectedCourse = useSelector(
1226
(state: { selectedCourse: Course | null }) => state.selectedCourse
1327
);
1428

1529
return (
1630
<div>
1731
<Navbar />
18-
<main className='flex flex-row justify-start w-full'>
32+
<main className="flex flex-row justify-start w-full">
1933
<Sidemenu />
2034
{selectedCourse ? <Outlet /> : <Instruction />}
2135
</main>

0 commit comments

Comments
 (0)