11import React , { useEffect , useState } from 'react' ;
22import Cookies from 'js-cookie' ;
33import Notiflix from 'notiflix' ;
4- import { Modal } from '../Modal.js' ;
4+ import { Modal } from '../Modal.js' ;
5+ import SearchBar from '../search-bar.js'
56
67export function CourseDashboard ( ) {
78 const [ courseInfo , setCourseInfo ] = useState ( null ) ;
@@ -10,17 +11,22 @@ export function CourseDashboard() {
1011 title : "" ,
1112 description : "" ,
1213 } ) ;
13- const [ error , setError ] = useState ( null ) ;
14+ const [ searchQuery , setSearchQuery ] = useState ( "" ) ;
15+ const [ error , setError ] = useState ( null ) ;
1416
1517 const handleChange = ( e ) => {
1618 const { name, value } = e . target ;
1719 setNewCourse ( { ...newCourse , [ name ] : value } ) ;
1820 } ;
1921
20- const handleCreateCourse = async ( e ) => {
21- if ( newCourse . title === "" || newCourse . description === "" ) {
22- return
23- }
22+ const handleSearchChange = ( e ) => {
23+ setSearchQuery ( e . target . value ) ;
24+ } ;
25+
26+ const handleCreateCourse = async ( ) => {
27+ if ( newCourse . title === "" || newCourse . description === "" ) {
28+ return ;
29+ }
2430
2531 const userId = Cookies . get ( 'userId' ) ;
2632 try {
@@ -31,7 +37,7 @@ export function CourseDashboard() {
3137 } ,
3238 body : JSON . stringify ( {
3339 title : newCourse . title ,
34- description : newCourse . description
40+ description : newCourse . description ,
3541 } ) ,
3642 } ) ;
3743
@@ -59,26 +65,26 @@ export function CourseDashboard() {
5965
6066 async function fetchCourses ( ) {
6167 await fetch ( `http://localhost:4000/courses/${ userId } ` )
62- . then ( ( response ) => {
63- if ( ! response . ok ) {
64- throw new Error ( 'Network response was not ok' ) ;
65- }
66- return response . json ( ) ;
67- } )
68- . then ( ( data ) => setCourseInfo ( data . courses ) )
69- . catch ( ( error ) => setError ( error . message ) ) ;
68+ . then ( ( response ) => {
69+ if ( ! response . ok ) {
70+ throw new Error ( 'Network response was not ok' ) ;
71+ }
72+ return response . json ( ) ;
73+ } )
74+ . then ( ( data ) => setCourseInfo ( data . courses ) )
75+ . catch ( ( error ) => setError ( error . message ) ) ;
7076 }
7177
7278 async function fetchUser ( ) {
7379 await fetch ( `http://localhost:4000/user/${ userId } ` )
74- . then ( ( response ) => {
75- if ( ! response . ok ) {
76- throw new Error ( 'Network response was not ok' ) ;
77- }
78- return response . json ( ) ;
79- } )
80- . then ( ( data ) => setUserInfo ( data . user ) )
81- . catch ( ( error ) => setError ( error . message ) ) ;
80+ . then ( ( response ) => {
81+ if ( ! response . ok ) {
82+ throw new Error ( 'Network response was not ok' ) ;
83+ }
84+ return response . json ( ) ;
85+ } )
86+ . then ( ( data ) => setUserInfo ( data . user ) )
87+ . catch ( ( error ) => setError ( error . message ) ) ;
8288 }
8389
8490 fetchCourses ( ) ;
@@ -89,41 +95,51 @@ export function CourseDashboard() {
8995 if ( ! courseInfo || ! userInfo ) return < p > Loading...</ p > ;
9096
9197 let createButton = null ;
92- if ( userInfo . role === "educator" ) {
93- createButton = < Modal
94- title = { "Create New Course" }
95- trigger = {
96- < div className = "bg-blue-500 p-2 rounded shadow hover:bg-blue-700 m-auto text-center text-sm text-white font-semibold hover:cursor-pointer" >
97- < div > + New Course</ div >
98- </ div >
99- }
100- inputFields = { {
101- title : "Course Name" ,
102- description : "Course Description"
103- } }
104- changeHandler = { handleChange }
105- confirmHandler = { handleCreateCourse }
106- />
98+ if ( userInfo . role === "educator" ) {
99+ createButton = (
100+ < Modal
101+ title = { "Create New Course" }
102+ trigger = {
103+ < div className = "bg-blue-500 p-3 rounded shadow hover:bg-blue-700 m-auto text-center text-sm text-white font-semibold hover:cursor-pointer" >
104+ < div > + New Course</ div >
105+ </ div >
106+ }
107+ inputFields = { {
108+ title : "Course Name" ,
109+ description : "Course Description" ,
110+ } }
111+ changeHandler = { handleChange }
112+ confirmHandler = { async ( ) => {
113+ await handleCreateCourse ( ) ;
114+ } }
115+ />
116+
117+ ) ;
107118 }
108119
109- var courseList = [ ] ;
110- //Push all courses into courseList
111- courseInfo . forEach ( course => {
112- courseList . push (
113- < a href = { `/courses/ ${ course . ID } ` } >
114- < div className = "bg-gray-100 p-4 rounded shadow hover:bg-gray-300" >
115- < h3 className = "text-xl font-semibold truncate overflow-hidden" > { course . title } </ h3 >
116- < p className = "mt-2 " > { course . description } </ p >
117- </ div >
118- </ a >
119- )
120- } ) ;
121-
120+ const filteredCourses = courseInfo . filter ( ( course ) =>
121+ course . title . toLowerCase ( ) . includes ( searchQuery . toLowerCase ( ) )
122+ ) ;
123+
124+ const courseList = filteredCourses . map ( ( course ) => (
125+ < a href = { `/courses/ ${ course . ID } ` } key = { course . ID } >
126+ < div className = "bg-gray-100 p-4 rounded shadow hover:bg-gray-300" >
127+ < h3 className = "text-xl font-semibold truncate overflow-hidden " > { course . title } </ h3 >
128+ < p className = "mt-2" > { course . description } </ p >
129+ </ div >
130+ </ a >
131+ ) ) ;
132+
122133 return (
123134 < div className = "p-6" >
124- < div className = "flex justify-between flex-wrap mb-5" >
135+ < div className = "mb-5" >
125136 < h1 className = "text-2xl font-bold" > Courses</ h1 >
126- { createButton }
137+ </ div >
138+ < div className = "mb-5" >
139+ < div className = "flex flex-col sm:flex-row sm:justify-between sm:items-center gap-4" >
140+ { createButton }
141+ < SearchBar onChange = { handleSearchChange } />
142+ </ div >
127143 </ div >
128144 < div className = "grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6" >
129145 { courseList }
0 commit comments