@@ -62,7 +62,7 @@ export default class AddAttendance extends Component {
6262 present : [ ] , presentStuds : [ ] ,
6363 errors : [ ] , modalIsOpen : false ,
6464 alertModalIsOpen : false , successModalIsOpen : false ,
65- loading : false
65+ loading : false , saving : false
6666 }
6767 }
6868
@@ -89,9 +89,7 @@ export default class AddAttendance extends Component {
8989 }
9090 return res . json ( )
9191 } ) . then ( resdata => {
92- this . setState ( { students : resdata , present :resdata , errors : [ ] , loading : false } , ( ) => {
93- console . log ( this . state . present )
94- } )
92+ this . setState ( { students : resdata , present :resdata , errors : [ ] , loading : false } )
9593 } ) . catch ( err => {
9694 console . log ( err )
9795 this . setState ( { loading : false } )
@@ -111,6 +109,8 @@ export default class AddAttendance extends Component {
111109
112110 handleFormSubmit = e => {
113111 e . preventDefault ( ) ;
112+ this . setState ( { saving : true } )
113+ this . openSuccessModal ( )
114114 const attendanceRequest = {
115115 "date" : this . state . date . toLocaleDateString ( ) ,
116116 "present" : this . state . present
@@ -131,10 +131,9 @@ export default class AddAttendance extends Component {
131131 this . setState ( { loading : false } )
132132 throw new Error ( res . status )
133133 }
134- this . setState ( { loading : false } )
134+ this . setState ( { loading : false , saving : false } )
135135 return res . json ( )
136136 } )
137- . then ( data => this . openSuccessModal ( ) )
138137 . catch ( err => {
139138 this . setState ( { loading : false } )
140139 console . log ( err )
@@ -155,7 +154,7 @@ export default class AddAttendance extends Component {
155154 } ;
156155
157156 closeModal = ( ) => {
158- this . setState ( { modalIsOpen : false , alertModalIsOpen : false , successModalIsOpen : false } ) ;
157+ this . setState ( { modalIsOpen : false , alertModalIsOpen : false , successModalIsOpen : false , errors : [ ] } ) ;
159158 } ;
160159
161160 openAlertModal = ( ) => {
@@ -178,38 +177,54 @@ export default class AddAttendance extends Component {
178177 }
179178 } ) . then ( res => {
180179 if ( res . status !== 200 && res . status !== 201 ) {
181- if ( res . status === 401 ) {
182- this . setState ( { loading : false } )
183- this . props . history . push ( '/login' )
184- }
185- if ( res . status === 404 ) {
186- throw new Error ( res . status )
187-
180+ switch ( res . status ) {
181+ case 400 :
182+ console . log ( 400 )
183+ this . setState ( { loading : false , errors : [ ...this . state . errors , 'Request Body Missing' ] } )
184+ this . openAlertModal ( )
185+
186+ break ;
187+ case 401 :
188+ // Not authenticated Error
189+ console . log ( 401 )
190+ this . setState ( { loading : false } )
191+ this . props . history . push ( '/login' )
192+
193+ break ;
194+ case 404 :
195+ this . setState ( { loading : false , errors : [ ...this . state . errors , 'Not Found' ] } )
196+ console . log ( 404 )
197+ this . openAlertModal ( )
198+
199+ break ;
200+ default :
201+ this . setState ( { loading : false } )
202+ throw new Error ( res . status )
203+
188204 }
189- this . setState ( { loading : true } )
190- throw new Error ( res . status )
205+
191206 }
192207 return res . json ( )
193208 } ) . then ( data => {
194- this . setState ( {
195- presentStuds : data . present ,
196- errors : [ ] ,
197- loading : false
198- } )
209+ if ( this . state . errors . length === 0 )
210+ this . setState ( {
211+ presentStuds : data . present ,
212+ errors : [ ] ,
213+ loading : false
214+ } )
215+ } ) . catch ( err => {
216+ console . log ( err )
217+ this . setState ( { loading : false } )
199218 } )
200- . catch ( err => {
201- this . setState ( { loading : false , errors : [ ...this . state . errors , err ] } )
202- console . log ( err )
203- this . openAlertModal ( )
204- } )
205219 }
206220
207221 render ( ) {
208222 const listItem = this . state . students . map ( stud => (
209223 < Checkbox key = { stud . _id } stud = { stud } checked = { true } handleChange = { this . handleCheckboxChange } clearList = { this . clearListItem } />
210224 ) )
211225 return ( this . state . loading ) ? < MyLoader loading = { this . state . loading } /> :
212- < div className = "container bg-light mt-5" >
226+ < div className = "container bg-light pt-5 h-100" >
227+ < h3 > Attendance Ppge</ h3 >
213228 < div className = "row" >
214229 < div className = "col-md p-5" >
215230 < form onSubmit = { this . handleFormSubmit } >
@@ -269,8 +284,8 @@ export default class AddAttendance extends Component {
269284 Error
270285 < span className = "float-right" style = { { cursor : 'pointer' } } onClick = { this . closeModal } > X</ span >
271286 </ div > < hr />
272- < div className = "panel-body" > < p > Something Went Wrong!!</ p > </ div > < hr />
273- < div className = "panel-footer" > < span className = 'float-right mr-2 text-danger' > { this . state . errors . length } Errors</ span > </ div >
287+ < div className = "panel-body" > < p > Something Went Wrong!!< br /> { this . state . errors . map ( ( err , i ) => < span key = { i } > { err . toString ( ) } </ span > ) } < /p > </ div > < hr />
288+ < div className = "panel-footer" > < span className = 'float-right mr-2 text-danger' > { this . state . errors . length } Errors</ span > </ div >
274289 </ div >
275290 }
276291 </ Modal >
@@ -284,10 +299,10 @@ export default class AddAttendance extends Component {
284299 >
285300 < div className = "panel panel-success" >
286301 < div className = 'panel-heading' >
287- Success
302+ Saving Attendance
288303 < span className = "float-right" style = { { cursor : 'pointer' } } onClick = { this . closeModal } > X</ span >
289304 </ div > < hr />
290- < div className = "panel-body" > < p > SuccessFully Marked Attendance!!</ p > </ div > < hr />
305+ { this . state . saving ? < div className = "panel-body" > < i className = "fas fa-spinner" /> Saving... </ div > : < div className = "panel-body" > < p > SuccessFully Marked Attendance!!</ p > </ div > } < hr />
291306 < div className = "panel-footer" > < button className = "btn btn-success" onClick = { this . closeModal } > Ok</ button > </ div >
292307 </ div >
293308 </ Modal >
0 commit comments