@@ -2,12 +2,53 @@ import React, { useEffect, useState } from 'react';
22import Cookies from 'js-cookie' ;
33import Popup from 'reactjs-popup' ;
44import 'reactjs-popup/dist/index.css' ;
5+ import Notiflix from 'notiflix' ;
56
67export function CourseDashboard ( ) {
78 const [ courseInfo , setCourseInfo ] = useState ( null ) ;
89 const [ userInfo , setUserInfo ] = useState ( null ) ;
10+ const [ newCourse , setNewCourse ] = useState ( {
11+ title : "" ,
12+ description : "" ,
13+ } ) ;
914 const [ error , setError ] = useState ( null ) ;
1015
16+ const handleChange = ( e ) => {
17+ const { name, value } = e . target ;
18+ setNewCourse ( { ...newCourse , [ name ] : value } ) ;
19+ } ;
20+
21+ const handleCreateCourse = async ( e ) => {
22+ if ( newCourse . title === "" || newCourse . description === "" ) {
23+ return
24+ }
25+
26+ const userId = Cookies . get ( 'userId' ) ;
27+ try {
28+ const response = await fetch ( `http://localhost:4000/create-course/${ userId } ` , {
29+ method : "POST" ,
30+ headers : {
31+ "Content-Type" : "application/json" ,
32+ } ,
33+ body : JSON . stringify ( {
34+ title : newCourse . title ,
35+ description : newCourse . description
36+ } ) ,
37+ } ) ;
38+
39+ const data = await response . json ( ) ;
40+ if ( response . ok ) {
41+ Notiflix . Notify . success ( "Course creation successful!" ) ;
42+ setTimeout ( ( ) => {
43+ window . location . reload ( ) ;
44+ } , 500 ) ;
45+ } else {
46+ Notiflix . Notify . failure ( data . message || "Course creation failed" ) ;
47+ }
48+ } catch ( error ) {
49+ Notiflix . Notify . failure ( "Error occurred during course creation" ) ;
50+ }
51+ } ;
1152 useEffect ( ( ) => {
1253 const userId = Cookies . get ( 'userId' ) ;
1354
@@ -68,15 +109,35 @@ export function CourseDashboard() {
68109 < Popup trigger = { < div className = "bg-blue-500 p-4 rounded shadow hover:bg-blue-700 m-auto text-center text-xl text-white font-semibold hover:cursor-pointer" >
69110 < div > + New Course</ div > </ div > } modal nested >
70111 { close => (
71- < div className = 'modal' >
72- < div className = 'content' >
112+ < div className = "flex-auto" >
113+ < h3 className = "font-semibold" >
73114 Create New Course
115+ </ h3 >
116+ < div className = "p-2" >
117+ < label > Course Name: </ label >
118+ < input className = "border-2 border-black rounded p-2 size-11/12"
119+ type = "text"
120+ name = "title"
121+ onChange = { handleChange }
122+ />
123+ </ div >
124+ < div className = "p-2" >
125+ < label > Course Description: </ label >
126+ < input className = "border-2 border-black rounded p-2 size-11/12"
127+ type = "text"
128+ name = "description"
129+ onChange = { handleChange }
130+ />
74131 </ div >
75- < div >
76- < button className = "border-2 border-black p-1 rounded" onClick =
132+ < div className = "grid grid-rows-1 grid-cols-2" >
133+ < button className = "justify-self-start border-2 border-black p-1 rounded" onClick =
77134 { ( ) => close ( ) } >
78135 Cancel
79136 </ button >
137+ < button className = "justify-self-end border-2 border-black p-1 rounded bg-green-400"
138+ onClick = { handleCreateCourse } >
139+ Create
140+ </ button >
80141 </ div >
81142 </ div >
82143 )
0 commit comments