1
- import React , { useState } from 'react' ;
1
+ import React , { useState , useEffect } from 'react' ;
2
2
import { useParams } from 'react-router-dom' ;
3
- import { ExpressValidationError } from 'devu-shared-modules' ;
3
+ import { ExpressValidationError , AssignmentProblem } from 'devu-shared-modules' ;
4
4
import { SET_ALERT } from 'redux/types/active.types' ;
5
5
import { useActionless } from 'redux/hooks' ;
6
6
import RequestService from 'services/request.service' ;
@@ -9,9 +9,11 @@ import Modal from 'components/shared/layouts/modal';
9
9
interface Props {
10
10
open : boolean ;
11
11
onClose : ( ) => void ;
12
+ edit ?: boolean ;
13
+ problemId ?: number ;
12
14
}
13
15
14
- const CodeProblemModal = ( { open, onClose } : Props ) => {
16
+ const CodeProblemModal = ( { open, onClose, edit , problemId } : Props ) => {
15
17
const [ setAlert ] = useActionless ( SET_ALERT ) ;
16
18
const { assignmentId } = useParams < { assignmentId : string } > ( ) ;
17
19
const { courseId } = useParams < { courseId : string } > ( ) ;
@@ -22,31 +24,56 @@ const CodeProblemModal = ({ open, onClose }: Props) => {
22
24
maxScore : '' ,
23
25
} ) ;
24
26
27
+ const setInitalFormData = async ( ) => {
28
+ if ( ! problemId ) {
29
+ return
30
+ }
31
+
32
+ const assignmentProblemData = await RequestService . get < AssignmentProblem > ( `/api/course/${ courseId } /assignment/${ assignmentId } /assignment-problems/${ problemId } ` ) ;
33
+ setFormData ( ( {
34
+ title : assignmentProblemData . problemName ,
35
+ maxScore : '' + assignmentProblemData . maxScore ,
36
+ } ) )
37
+ }
38
+
39
+ useEffect ( ( ) => { setInitalFormData ( ) } , [ problemId ] )
25
40
26
41
const submittable = ( ) => {
27
- if ( ! formData . title || ! formData . maxScore ) { return false }
28
- else { return true }
42
+ if ( ! formData . title || ! formData . maxScore ) { return false }
43
+ else { return true }
29
44
}
30
45
31
46
const handleSubmit = ( ) => {
32
47
if ( ! submittable ) return ;
33
48
34
-
35
49
const problemFormData = {
36
50
assignmentId : parseInt ( assignmentId ) ,
37
51
problemName : formData . title ,
38
52
maxScore : parseInt ( formData . maxScore ) ,
53
+ metadata : {
54
+ type : 'File'
55
+ }
39
56
} ;
40
57
41
- RequestService . post ( `/api/course/${ courseId } /assignment/${ assignmentId } /assignment-problems` , problemFormData )
42
- . then ( ( ) => {
43
- setAlert ( { autoDelete : true , type : 'success' , message : 'Problem Added' } ) ;
44
- } )
45
- . catch ( ( err : ExpressValidationError [ ] | Error ) => {
46
- const message = Array . isArray ( err ) ? err . map ( ( e ) => `${ e . param } ${ e . msg } ` ) . join ( ', ' ) : err . message ;
47
- setAlert ( { autoDelete : false , type : 'error' , message } ) ;
48
- } ) ;
49
-
58
+ if ( edit ) {
59
+ RequestService . put ( `/api/course/${ courseId } /assignment/${ assignmentId } /assignment-problems/${ problemId } ` , problemFormData )
60
+ . then ( ( ) => {
61
+ setAlert ( { autoDelete : true , type : 'success' , message : 'Problem Added' } ) ;
62
+ } )
63
+ . catch ( ( err : ExpressValidationError [ ] | Error ) => {
64
+ const message = Array . isArray ( err ) ? err . map ( ( e ) => `${ e . param } ${ e . msg } ` ) . join ( ', ' ) : err . message ;
65
+ setAlert ( { autoDelete : false , type : 'error' , message } ) ;
66
+ } ) ;
67
+ } else {
68
+ RequestService . post ( `/api/course/${ courseId } /assignment/${ assignmentId } /assignment-problems` , problemFormData )
69
+ . then ( ( ) => {
70
+ setAlert ( { autoDelete : true , type : 'success' , message : 'Problem Added' } ) ;
71
+ } )
72
+ . catch ( ( err : ExpressValidationError [ ] | Error ) => {
73
+ const message = Array . isArray ( err ) ? err . map ( ( e ) => `${ e . param } ${ e . msg } ` ) . join ( ', ' ) : err . message ;
74
+ setAlert ( { autoDelete : false , type : 'error' , message } ) ;
75
+ } ) ;
76
+ }
50
77
51
78
closeModal ( ) ;
52
79
} ;
@@ -55,7 +82,7 @@ const CodeProblemModal = ({ open, onClose }: Props) => {
55
82
setFormData ( {
56
83
title : '' ,
57
84
maxScore : ''
58
- } )
85
+ } )
59
86
onClose ( )
60
87
}
61
88
@@ -67,25 +94,27 @@ const CodeProblemModal = ({ open, onClose }: Props) => {
67
94
} ;
68
95
69
96
return (
70
- < Modal title = " Add Code/ File Input Problem" buttonAction = { handleSubmit } open = { open } onClose = { closeModal } isSubmittable = { submittable } >
97
+ < Modal title = { edit ? "Edit File Upload Problem" : " Add File Upload Problem"} buttonAction = { handleSubmit } open = { open } onClose = { closeModal } isSubmittable = { submittable } >
71
98
< div className = "input-group" >
72
99
< label htmlFor = "title" className = "input-label" > Problem Title:</ label >
73
- < input
74
- type = "text"
75
- id = "title"
76
- placeholder = "e.g. Application Objective 3"
77
- onChange = { handleChange }
100
+ < input
101
+ type = "text"
102
+ id = "title"
103
+ placeholder = "e.g. Application Objective 3"
104
+ onChange = { handleChange }
105
+ value = { formData . title }
78
106
/>
79
107
</ div >
80
-
108
+
81
109
< div className = "input-group" >
82
110
< label htmlFor = "maxScore" className = "input-label" > Maximum Score:</ label >
83
- < input
84
- type = "number"
85
- id = "maxScore"
86
- placeholder = "e.g. 10"
87
- min = "0"
88
- onChange = { handleChange }
111
+ < input
112
+ type = "number"
113
+ id = "maxScore"
114
+ placeholder = "e.g. 10"
115
+ min = "0"
116
+ onChange = { handleChange }
117
+ value = { formData . maxScore }
89
118
/>
90
119
</ div >
91
120
</ Modal >
0 commit comments