@@ -5,40 +5,48 @@ import { Typography, TextField, Button } from "@mui/material";
55import axios from "axios" ;
66import { Loading } from "./Loading" ;
77import { BASE_URL } from "../config.js" ;
8+ import { courseState } from "../store/atoms/course" ;
9+ import { useRecoilState , useRecoilValue , useSetRecoilState } from "recoil" ;
10+ import { courseTitle , coursePrice , isCourseLoading , courseImage } from "../store/selectors/course" ;
811
912function Course ( ) {
1013 let { courseId } = useParams ( ) ;
11- const [ course , setCourse ] = useState ( null ) ;
12-
14+ const setCourse = useSetRecoilState ( courseState ) ;
15+ const courseLoading = useRecoilValue ( isCourseLoading ) ;
16+
1317 useEffect ( ( ) => {
1418 axios . get ( `${ BASE_URL } /admin/course/${ courseId } ` , {
1519 method : "GET" ,
1620 headers : {
1721 "Authorization" : "Bearer " + localStorage . getItem ( "token" )
1822 }
1923 } ) . then ( res => {
20- setCourse ( res . data . course ) ;
24+ setCourse ( { isLoading : false , course : res . data . course } ) ;
25+ } )
26+ . catch ( e => {
27+ setCourse ( { isLoading : false , course : null } ) ;
2128 } ) ;
2229 } , [ ] ) ;
2330
24- if ( ! course ) {
31+ if ( courseLoading ) {
2532 return < Loading />
2633 }
2734
2835 return < div >
29- < GrayTopper title = { course . title } />
36+ < GrayTopper />
3037 < Grid container >
3138 < Grid item lg = { 8 } md = { 12 } sm = { 12 } >
32- < UpdateCard course = { course } setCourse = { setCourse } />
39+ < UpdateCard />
3340 </ Grid >
3441 < Grid item lg = { 4 } md = { 12 } sm = { 12 } >
35- < CourseCard course = { course } />
42+ < CourseCard />
3643 </ Grid >
3744 </ Grid >
3845 </ div >
3946}
4047
41- function GrayTopper ( { title} ) {
48+ function GrayTopper ( ) {
49+ const title = useRecoilValue ( courseTitle ) ;
4250 return < div style = { { height : 250 , background : "#212121" , top : 0 , width : "100vw" , zIndex : 0 , marginBottom : - 250 } } >
4351 < div style = { { height : 250 , display : "flex" , justifyContent : "center" , flexDirection : "column" } } >
4452 < div >
@@ -50,11 +58,13 @@ function GrayTopper({title}) {
5058 </ div >
5159}
5260
53- function UpdateCard ( { course, setCourse} ) {
54- const [ title , setTitle ] = useState ( course . title ) ;
55- const [ description , setDescription ] = useState ( course . description ) ;
56- const [ image , setImage ] = useState ( course . imageLink ) ;
57- const [ price , setPrice ] = useState ( course . price ) ;
61+ function UpdateCard ( ) {
62+ const [ courseDetails , setCourse ] = useRecoilState ( courseState ) ;
63+
64+ const [ title , setTitle ] = useState ( courseDetails . course . title ) ;
65+ const [ description , setDescription ] = useState ( courseDetails . course . description ) ;
66+ const [ image , setImage ] = useState ( courseDetails . course . imageLink ) ;
67+ const [ price , setPrice ] = useState ( courseDetails . course . price ) ;
5868
5969 return < div style = { { display : "flex" , justifyContent : "center" } } >
6070 < Card varint = { "outlined" } style = { { maxWidth : 600 , marginTop : 200 } } >
@@ -106,7 +116,7 @@ function UpdateCard({course, setCourse}) {
106116 < Button
107117 variant = "contained"
108118 onClick = { async ( ) => {
109- axios . put ( `${ BASE_URL } /admin/courses/` + course . _id , {
119+ axios . put ( `${ BASE_URL } /admin/courses/` + courseDetails . course . _id , {
110120 title : title ,
111121 description : description ,
112122 imageLink : image ,
@@ -119,13 +129,13 @@ function UpdateCard({course, setCourse}) {
119129 }
120130 } ) ;
121131 let updatedCourse = {
122- _id : course . _id ,
132+ _id : courseDetails . course . _id ,
123133 title : title ,
124134 description : description ,
125135 imageLink : image ,
126136 price
127137 } ;
128- setCourse ( updatedCourse ) ;
138+ setCourse ( { course : updatedCourse , isLoading : false } ) ;
129139 } }
130140 > Update course</ Button >
131141 </ div >
@@ -134,7 +144,9 @@ function UpdateCard({course, setCourse}) {
134144}
135145
136146function CourseCard ( props ) {
137- const course = props . course ;
147+ const title = useRecoilValue ( courseTitle ) ;
148+ const imageLink = useRecoilValue ( courseImage ) ;
149+
138150 return < div style = { { display : "flex" , marginTop : 50 , justifyContent : "center" , width : "100%" } } >
139151 < Card style = { {
140152 margin : 10 ,
@@ -145,14 +157,14 @@ function CourseCard(props) {
145157 paddingBottom : 15 ,
146158 zIndex : 2
147159 } } >
148- < img src = { course . imageLink } style = { { width : 350 } } > </ img >
160+ < img src = { imageLink } style = { { width : 350 } } > </ img >
149161 < div style = { { marginLeft : 10 } } >
150- < Typography variant = "h5" > { course . title } </ Typography >
162+ < Typography variant = "h5" > { title } </ Typography >
151163 < Typography variant = "subtitle2" style = { { color : "gray" } } >
152164 Price
153165 </ Typography >
154166 < Typography variant = "subtitle1" >
155- < b > Rs { course . price } </ b >
167+ < b > Rs { price } </ b >
156168 </ Typography >
157169 </ div >
158170 </ Card >
0 commit comments