@@ -2,8 +2,7 @@ import React, { Component } from 'react'
22import Modal from 'react-modal' ;
33import MyCalendar from '../reusables/Calendar'
44import { AuthContext } from '../../context'
5- import MyLoader from '../reusables/MyLoader' ;
6-
5+ import MyLoader from '../reusables/MyLoader'
76
87const customStyles = {
98 content : {
@@ -25,16 +24,15 @@ export default class AddStudent extends Component {
2524 this . englishEl = React . createRef ( ) ; this . mathsEl = React . createRef ( ) ;
2625 this . sexEl = React . createRef ( ) ; this . feesEl = React . createRef ( ) ;
2726 this . state = {
28- modalIsOpen : false ,
27+ modalJoinIsOpen : false , modalDOBIsOpen : false ,
2928 date : new Date ( ) ,
30- addedStudent : { } ,
31- loading : false ,
32- loggedIn : false
29+ loading : false , loggedIn : false ,
30+ dateOfJoining : 'pick a date' , dateOfBirth : new Date ( )
3331 }
3432 }
3533
36- componentDidMount = ( ) => {
37- this . setState ( { loading : true } )
34+ componentDidMount = ( ) => {
35+ this . setState ( { loading : true } )
3836 fetch ( '/auth' , {
3937 method : 'GET' ,
4038 mode : 'cors' ,
@@ -43,27 +41,36 @@ export default class AddStudent extends Component {
4341 }
4442 } ) . then ( res => {
4543 if ( res . status === 200 ) {
46- this . setState ( { loggedIn : true , loading :false } )
44+ this . setState ( { loggedIn : true , loading : false } )
4745 } else {
48- this . setState ( { loggedIn : false , loading :false } )
46+ this . setState ( { loggedIn : false , loading : false } )
4947 }
5048 } ) . catch ( err => {
51- this . setState ( { loggedIn : false , loading :false } )
49+ this . setState ( { loggedIn : false , loading : false } )
5250 throw new Error ( err )
5351 } )
5452 }
5553
5654 onChange = date => {
57- this . setState ( { date } )
55+ this . setState ( { dateOfJoining : date } )
56+ this . closeModal ( )
57+ }
58+
59+ onDOBChange = date => {
60+ this . setState ( { dateOfBirth : date } )
5861 this . closeModal ( )
5962 }
6063
61- openModal = ( ) => {
62- this . setState ( { modalIsOpen : true } ) ;
64+ openJoiningModal = ( ) => {
65+ this . setState ( { modalJoinIsOpen : true } ) ;
66+ }
67+
68+ openDOBModal = ( ) => {
69+ this . setState ( { modalDOBIsOpen : true } ) ;
6370 }
6471
6572 closeModal = ( ) => {
66- this . setState ( { modalIsOpen : false } ) ;
73+ this . setState ( { modalJoinIsOpen : false , modalDOBIsOpen : false } ) ;
6774 }
6875
6976 redirectHome = ( e ) => {
@@ -78,52 +85,41 @@ export default class AddStudent extends Component {
7885 handleAddForm = ( e ) => {
7986 e . preventDefault ( ) ;
8087 this . setState ( { loading : true } )
81- const fName = this . firstNameEl . current . value ;
82- const mName = this . firstNameEl . current . value ;
83- const lName = this . lastNameEl . current . value ;
84- const std = this . standardEl . current . value ;
85- const addr = this . addressEl . current . value ;
86- const brd = this . boardEl . current . value ;
87- const phy = this . physicsEl . current . value ;
88- const eng = this . englishEl . current . value ;
89- const maths = this . mathsEl . current . value ;
90- const sex = this . sexEl . current . value ;
91- const fees = this . feesEl . current . value ;
88+ const fName = this . firstNameEl . current . value ; const mName = this . firstNameEl . current . value ;
89+ const lName = this . lastNameEl . current . value ; const std = this . standardEl . current . value ;
90+ const addr = this . addressEl . current . value ; const brd = this . boardEl . current . value ;
91+ const phy = this . physicsEl . current . value ; const eng = this . englishEl . current . value ;
92+ const maths = this . mathsEl . current . value ; const sex = this . sexEl . current . value ; const fees = this . feesEl . current . value ;
9293
9394 const addStudentRequest = {
9495 "firstName" : fName , "middleName" : mName , "lastName" : lName ,
95- "Address" : addr , "standard" : std ,
96- "Board" : brd ,
97- "lastYearmarks" : {
98- "physics" : phy ,
99- "english" : eng ,
100- "maths" : maths
101- } , "sex" : sex ,
102- "fees" : {
103- "total" : fees
104- }
96+ "dateOfBirth" : this . state . dateOfBirth ,
97+ "joinedOn" : this . state . dateOfJoining ,
98+ "Address" : addr , "standard" : std , "Board" : brd ,
99+ "lastYearmarks" : { "physics" : phy , "english" : eng , "maths" : maths } ,
100+ "sex" : sex , "fees" : { "total" : fees }
105101 }
106102 fetch ( '/api/student' , {
107103 method : 'POST' , mode : 'cors' ,
108104 headers : {
109105 'Content-Type' : 'application/json' ,
110106 'Accept' : 'application/json'
111- } ,
112- body : JSON . stringify ( addStudentRequest )
113- } ) . then ( res => {
114- if ( res . status !== 200 && res . status !== 201 ) {
115- if ( res . status === 401 ) {
116- this . setState ( { loading : false } )
117- this . props . history . push ( '/login' )
118- } else {
119- this . setState ( { loading : false } )
120- }
121- }
122- this . setState ( { loading : false } )
123- return res . json ( ) ;
107+ } , body : JSON . stringify ( addStudentRequest )
124108 } )
109+ . then ( res => {
110+ if ( res . status !== 200 && res . status !== 201 ) {
111+ if ( res . status === 401 ) {
112+ this . setState ( { loading : false } )
113+ this . props . history . push ( '/login' )
114+ } else {
115+ this . setState ( { loading : false } )
116+ }
117+ }
118+ this . setState ( { loading : false } )
119+ return res . json ( ) ;
120+ } )
125121 . then ( resData => {
126- this . setState ( { addedStudent : resData , loading : false } )
122+ this . setState ( { loading : false } )
127123 this . props . history . push ( '/list' )
128124 } )
129125 . catch ( err => {
@@ -132,13 +128,14 @@ export default class AddStudent extends Component {
132128 }
133129
134130 render ( ) {
135- return ( this . state . loading ) ? < MyLoader loading = { this . state . loading } /> :
131+ return ( this . state . loading ) ? < MyLoader loading = { this . state . loading } /> :
136132 this . state . loggedIn ? < AuthContext . Consumer >
137- { context => (
133+ { context => (
138134 < div className = "container text-align-center p-3" >
139135 < div className = "container w-75 text-align-center" >
140- < div className = "container" style = { { textAlign :'center' , fontSize :'25px' } } > Add New Student</ div >
136+ < div className = "container" style = { { textAlign : 'center' , fontSize : '25px' } } > Add New Student</ div >
141137 < form className = "py-3" onSubmit = { this . handleAddForm } >
138+ < hr />
142139 < div className = "row py-3" >
143140 < div className = "col" >
144141 < input type = "text" ref = { this . firstNameEl } className = "form-control form-control-sm" placeholder = "First name" required />
@@ -152,6 +149,16 @@ export default class AddStudent extends Component {
152149 </ div >
153150 < div className = "form-group" >
154151 < input type = "text" ref = { this . addressEl } className = "form-control form-control-sm" id = "InputAddress" placeholder = "Address" required />
152+ < div className = "input-group w-25" >
153+ < label htmlFor = "dateOfBirth" > Date Of Birth </ label >
154+ < div className = "w-100" > </ div >
155+ < input type = "text" className = 'form-control form-control-sm' value = { this . state . dateOfBirth . toLocaleString ( 'en-IN' ) . split ( ',' ) [ 0 ] } name = "dateOfBirth" readOnly />
156+ < div className = "input-group-append" >
157+ < span className = "input-group-text" title = 'Date Of Joining' id = "basic-addon1" onClick = { this . openDOBModal } >
158+ < i className = "far fa-calendar-alt" id = "date" />
159+ </ span >
160+ </ div >
161+ </ div >
155162 </ div >
156163 < hr />
157164 < div className = "row" >
@@ -176,10 +183,15 @@ export default class AddStudent extends Component {
176183 </ select >
177184 </ div >
178185 </ div >
179- < div className = "form-group" >
180- < label htmlFor = "InputDate" > Date Of Joining </ label >
181- < i className = "far fa-calendar-alt ml-2 mr-4 mb-2 btn-lg" id = "InputDate" onClick = { this . openModal } />
182- < span > Selected Date:- { this . state . date . toLocaleString ( 'en-IN' ) . split ( ',' ) [ 0 ] } </ span >
186+ < div className = "input-group w-25" >
187+ < label htmlFor = "dateOfJoining" > Date Of Joining </ label >
188+ < div className = "w-100" > </ div >
189+ < input type = "text" className = 'form-control form-control-sm' value = { this . state . dateOfJoining . toLocaleString ( 'en-IN' ) . split ( ',' ) [ 0 ] } name = "dateOfJoining" readOnly />
190+ < div className = "input-group-append" >
191+ < span className = "input-group-text" title = 'Date Of Joining' id = "basic-addon1" onClick = { this . openJoiningModal } >
192+ < i className = "far fa-calendar-alt" id = "date" />
193+ </ span >
194+ </ div >
183195 </ div >
184196 < hr />
185197 < div >
@@ -196,7 +208,7 @@ export default class AddStudent extends Component {
196208 < input type = "text" className = "form-control form-control-sm" ref = { this . mathsEl } id = "InputMaths" placeholder = "Maths" required />
197209 </ div >
198210 </ div >
199- < hr />
211+ < hr />
200212 < div className = "row" >
201213 < div className = "form-group col" >
202214 < label htmlFor = "InputSex" > Gender</ label >
@@ -210,14 +222,14 @@ export default class AddStudent extends Component {
210222 < input type = "number" className = "form-control form-control-sm" ref = { this . feesEl } id = "InputFees" placeholder = "Total Fees" />
211223 </ div >
212224 </ div >
213- < hr />
225+ < hr />
214226 < button type = "button" onClick = { this . redirectHome } className = "btn btn-light float-right ml-3" > Cancel</ button >
215227 < button type = "submit" className = "btn btn-primary float-right" > Submit</ button >
216228 </ form >
217229 </ div >
218230
219231 < Modal
220- isOpen = { this . state . modalIsOpen }
232+ isOpen = { this . state . modalJoinIsOpen }
221233 onRequestClose = { this . closeModal }
222234 style = { customStyles }
223235 contentLabel = "Date Picker Modal"
@@ -228,11 +240,23 @@ export default class AddStudent extends Component {
228240 value = { this . state . date }
229241 />
230242 </ Modal >
243+ < Modal
244+ isOpen = { this . state . modalDOBIsOpen }
245+ onRequestClose = { this . closeModal }
246+ style = { customStyles }
247+ contentLabel = "Date Picker Modal"
248+ ariaHideApp = { false }
249+ >
250+ < MyCalendar
251+ onChange = { this . onDOBChange }
252+ value = { this . state . date }
253+ />
254+ </ Modal >
231255 </ div >
232256 ) }
233- </ AuthContext . Consumer > : < div className = 'container p-5' >
234- < h3 > OOPS !! < br /> Not Logged In</ h3 >
235- < a href = "/login" onClick = { this . redirectLogin } > Login</ a >
257+ </ AuthContext . Consumer > : < div className = 'container p-5' >
258+ < h3 > OOPS !! < br /> Not Logged In</ h3 >
259+ < a href = "/login" onClick = { this . redirectLogin } > Login</ a >
236260 </ div >
237- }
261+ }
238262}
0 commit comments