1- import React , { useState } from 'react' ;
1+ import React , { useState , useEffect } from 'react' ;
22import { useParams } from 'react-router-dom' ;
3- import { ExpressValidationError } from 'devu-shared-modules' ;
3+ import { ExpressValidationError , AssignmentProblem } from 'devu-shared-modules' ;
44import { SET_ALERT } from 'redux/types/active.types' ;
55import { useActionless } from 'redux/hooks' ;
66import RequestService from 'services/request.service' ;
@@ -9,9 +9,11 @@ import Modal from 'components/shared/layouts/modal';
99interface Props {
1010 open : boolean ;
1111 onClose : ( ) => void ;
12+ edit ?: boolean ;
13+ problemId ?: number ;
1214}
1315
14- const CodeProblemModal = ( { open, onClose } : Props ) => {
16+ const CodeProblemModal = ( { open, onClose, edit , problemId } : Props ) => {
1517 const [ setAlert ] = useActionless ( SET_ALERT ) ;
1618 const { assignmentId } = useParams < { assignmentId : string } > ( ) ;
1719 const { courseId } = useParams < { courseId : string } > ( ) ;
@@ -22,31 +24,56 @@ const CodeProblemModal = ({ open, onClose }: Props) => {
2224 maxScore : '' ,
2325 } ) ;
2426
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 ] )
2540
2641 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 }
2944 }
3045
3146 const handleSubmit = ( ) => {
3247 if ( ! submittable ) return ;
3348
34-
3549 const problemFormData = {
3650 assignmentId : parseInt ( assignmentId ) ,
3751 problemName : formData . title ,
3852 maxScore : parseInt ( formData . maxScore ) ,
53+ metadata : {
54+ type : 'File'
55+ }
3956 } ;
4057
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+ }
5077
5178 closeModal ( ) ;
5279 } ;
@@ -55,7 +82,7 @@ const CodeProblemModal = ({ open, onClose }: Props) => {
5582 setFormData ( {
5683 title : '' ,
5784 maxScore : ''
58- } )
85+ } )
5986 onClose ( )
6087 }
6188
@@ -67,25 +94,27 @@ const CodeProblemModal = ({ open, onClose }: Props) => {
6794 } ;
6895
6996 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 } >
7198 < div className = "input-group" >
7299 < 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 }
78106 />
79107 </ div >
80-
108+
81109 < div className = "input-group" >
82110 < 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 }
89118 />
90119 </ div >
91120 </ Modal >
0 commit comments