|
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'; |
4 | 3 | import { Link } from 'react-router-dom'; |
5 | 4 | import Course from './Course'; |
6 | | -import { useCoursesQuery } from '../../generated/graphql'; |
7 | 5 | import CourseType from '../../Types/course'; |
8 | 6 |
|
9 | 7 | const listStyle = 'w-full text-left'; |
10 | 8 |
|
11 | 9 | const CourseList: React.FC = () => { |
12 | | - const dispatch = useDispatch(); |
13 | | - const [{ fetching, data, error }] = useCoursesQuery({ variables: {} }); |
14 | | - const courses = data?.getCourses?.data; |
15 | | - |
16 | 10 | 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 |
18 | 12 | ); |
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)); |
40 | 13 |
|
41 | 14 | return ( |
42 | 15 | <div className={listStyle}> |
43 | | - { |
| 16 | + {!!coursesState && |
44 | 17 | // useredux state here instead of DB response |
45 | | - coursesState!.map((course) => { |
| 18 | + coursesState.map((course) => { |
46 | 19 | return ( |
47 | 20 | <article key={course?.id}> |
48 | 21 | <Link to={`/homepage/classes/${course?.id}`}> |
49 | 22 | {!!course && <Course course={course} />} |
50 | 23 | </Link> |
51 | 24 | </article> |
52 | 25 | ); |
53 | | - }) |
54 | | - } |
| 26 | + })} |
55 | 27 | </div> |
56 | 28 | ); |
57 | 29 | }; |
58 | 30 |
|
59 | | -// return <article></article>; |
60 | | -// }; |
61 | | - |
62 | 31 | export default CourseList; |
0 commit comments