11import React , { useEffect , useState } from 'react' ;
22import Cookies from 'js-cookie' ;
3+ import Notiflix from 'notiflix' ;
4+ import { Modal } from '../Modal.js' ;
35
46export function CourseDashboard ( ) {
57 const [ courseInfo , setCourseInfo ] = useState ( null ) ;
8+ const [ userInfo , setUserInfo ] = useState ( null ) ;
9+ const [ newCourse , setNewCourse ] = useState ( {
10+ title : "" ,
11+ description : "" ,
12+ } ) ;
613 const [ error , setError ] = useState ( null ) ;
714
15+ const handleChange = ( e ) => {
16+ const { name, value } = e . target ;
17+ setNewCourse ( { ...newCourse , [ name ] : value } ) ;
18+ } ;
19+
20+ const handleCreateCourse = async ( e ) => {
21+ if ( newCourse . title === "" || newCourse . description === "" ) {
22+ return
23+ }
24+
25+ const userId = Cookies . get ( 'userId' ) ;
26+ try {
27+ const response = await fetch ( `http://localhost:4000/create-course/${ userId } ` , {
28+ method : "POST" ,
29+ headers : {
30+ "Content-Type" : "application/json" ,
31+ } ,
32+ body : JSON . stringify ( {
33+ title : newCourse . title ,
34+ description : newCourse . description
35+ } ) ,
36+ } ) ;
37+
38+ const data = await response . json ( ) ;
39+ if ( response . ok ) {
40+ Notiflix . Notify . success ( "Course creation successful!" ) ;
41+ setTimeout ( ( ) => {
42+ window . location . reload ( ) ;
43+ } , 500 ) ;
44+ } else {
45+ Notiflix . Notify . failure ( data . message || "Course creation failed" ) ;
46+ }
47+ } catch ( error ) {
48+ Notiflix . Notify . failure ( "Error occurred during course creation" ) ;
49+ }
50+ } ;
51+
852 useEffect ( ( ) => {
953 const userId = Cookies . get ( 'userId' ) ;
1054
@@ -13,7 +57,8 @@ export function CourseDashboard() {
1357 return ;
1458 }
1559
16- fetch ( `http://localhost:4000/courses/${ userId } ` )
60+ async function fetchCourses ( ) {
61+ await fetch ( `http://localhost:4000/courses/${ userId } ` )
1762 . then ( ( response ) => {
1863 if ( ! response . ok ) {
1964 throw new Error ( 'Network response was not ok' ) ;
@@ -22,27 +67,64 @@ export function CourseDashboard() {
2267 } )
2368 . then ( ( data ) => setCourseInfo ( data . courses ) )
2469 . catch ( ( error ) => setError ( error . message ) ) ;
70+ }
71+
72+ async function fetchUser ( ) {
73+ 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 ) ) ;
82+ }
83+
84+ fetchCourses ( ) ;
85+ fetchUser ( ) ;
2586 } , [ ] ) ;
2687
2788 if ( error ) return < p > Error: { error } </ p > ;
28- if ( ! courseInfo ) return < p > Loading...</ p > ;
89+ if ( ! courseInfo || ! userInfo ) return < p > Loading...</ p > ;
90+
91+ 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+ />
107+ }
29108
30109 var courseList = [ ] ;
31110 //Push all courses into courseList
32111 courseInfo . forEach ( course => {
33112 courseList . push (
34113 < a href = { `/courses/${ course . ID } ` } >
35114 < div className = "bg-gray-100 p-4 rounded shadow hover:bg-gray-300" >
36- < h3 className = "text-xl font-semibold" > { course . title } </ h3 >
115+ < h3 className = "text-xl font-semibold truncate overflow-hidden " > { course . title } </ h3 >
37116 < p className = "mt-2" > { course . description } </ p >
38117 </ div >
39118 </ a >
40119 )
41120 } ) ;
42-
121+
43122 return (
44123 < div className = "p-6" >
45- < h1 className = "text-2xl font-bold mb-4" > Courses</ h1 >
124+ < div className = "flex justify-between flex-wrap mb-5" >
125+ < h1 className = "text-2xl font-bold" > Courses</ h1 >
126+ { createButton }
127+ </ div >
46128 < div className = "grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6" >
47129 { courseList }
48130 </ div >
0 commit comments