@@ -126,8 +126,8 @@ export default function DegreePage() {
126126 < table className = "table" >
127127 < tbody >
128128 < tr >
129- < th > scelta libera</ th >
130- < td > { `gruppo ${ degree . default_group } ` || " tutti gli esami" } </ td >
129+ < th > < i > scelta libera</ i > </ th >
130+ < td > { degree . default_group ? `gruppo ${ degree . default_group } ` : < i > tutti gli esami</ i > } </ td >
131131 </ tr >
132132 { Object . entries ( degree . groups ) . map ( ( [ name , exams ] ) =>
133133 < ExamGroup key = { name } name = { name } exam_ids = { exams } /> ) }
@@ -186,16 +186,19 @@ type ExamGroup = {
186186}
187187
188188function DegreeForm ( { mutate, degree, exams} ) {
189+ const current_year = new Date ( ) . getFullYear ( )
189190 const [ error , setError ] = useState < string > ( '' )
190- const [ data , setData ] = useState ( degree )
191+ const [ data , setData ] = useState ( {
192+ academic_year : current_year ,
193+ years : 3 ,
194+ ...degree } )
191195 const [ groups , setGroups ] = useState < ExamGroup [ ] > ( Object . entries ( degree . groups as Record < string , string [ ] > )
192196 . map ( ( [ name , exam_ids ] ) => ( { name, exam_ids} ) )
193197 . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
194198 )
195199 const [ validation , setValidation ] = useState < any > ( { } )
196200 const engine = useEngine ( )
197201 const navigate = useNavigate ( )
198- const current_year = new Date ( ) . getFullYear ( )
199202
200203 return < Card >
201204 { error && < div className = "alert alert-danger" > { error } </ div > }
@@ -212,15 +215,15 @@ function DegreeForm({mutate, degree, exams}) {
212215 controlId = "academic_year"
213216 label = "Anno accademico (solo anno di inizio)"
214217 type = "number"
215- value = { data . academic_year || current_year }
218+ value = { data . academic_year }
216219 onChange = { onChange ( "academic_year" ) }
217220 />
218221 < Group
219222 validationError = { validation . years }
220223 controlId = "years"
221224 label = "Anni"
222225 type = "number"
223- value = { data . years || 3 }
226+ value = { data . years }
224227 onChange = { onChange ( "years" ) }
225228 />
226229 < Group
@@ -343,10 +346,17 @@ function DegreeForm({mutate, degree, exams}) {
343346 }
344347
345348 function submit ( ) {
346- data . groups = Object . fromEntries ( groups . map ( group => [ group . name , group . exam_ids ] ) )
347- data . academic_year = parseInt ( data . academic_year )
348- data . years = parseInt ( data . years )
349- mutate ( data ,
349+ const groups_array = Object . fromEntries ( groups . map ( group => [ group . name , group . exam_ids ] ) )
350+ const academic_year = parseInt ( data . academic_year )
351+ const years = parseInt ( data . years )
352+ const payload = {
353+ ...data ,
354+ academic_year,
355+ years,
356+ groups : groups_array ,
357+ }
358+ console . log ( `submitting` , JSON . stringify ( { payload, data, degree} , null , 2 ) )
359+ mutate ( payload ,
350360 {
351361 onSuccess : ( res ) => {
352362 const id = degree . _id || res . data
@@ -355,6 +365,7 @@ function DegreeForm({mutate, degree, exams}) {
355365 onError : ( err ) => {
356366 if ( err . response ?. status === 422 ) {
357367 setValidation ( err . response . data . issues )
368+ setError ( `${ err . response . data . message } ` )
358369 } else {
359370 setError ( `${ err } ` )
360371 }
@@ -383,21 +394,30 @@ function EditGroups({groups, setGroups, exams}:{
383394 ? { name : g . name , exam_ids : ids }
384395 : g ) ) ) }
385396 examDict = { examDict }
397+ erase = { ( ) => setGroups ( groups . filter ( g => g . name !== group . name ) ) }
386398 /> ) }
399+ < Button onClick = { add_group } disabled = { groups . filter ( group => group . name === "" ) . length > 0 } >
400+ aggiungi gruppo di esami
401+ </ Button >
387402 </ >
403+
404+ function add_group ( ) {
405+ setGroups ( [ ...groups , { name : "" , exam_ids : [ ] } ] )
406+ }
388407}
389408
390409type Option = {
391410 value : string ;
392411 label : string ;
393412 } ;
394413
395- function EditGroup ( { name, setName, group, setGroup, examDict} :{
396- name : string ,
397- setName : Dispatch < string > ,
398- group : string [ ] ,
414+ function EditGroup ( { name, setName, group, setGroup, examDict, erase } :{
415+ name : string
416+ setName : Dispatch < string >
417+ group : string [ ]
399418 setGroup : Dispatch < string [ ] >
400419 examDict : Record < string , { _id : string , name : string , code : string } >
420+ erase : ( ) => void
401421} ) {
402422 const sort_fun = ( a , b ) => a . label . localeCompare ( b . label )
403423 const options : Option [ ] = Object . values ( examDict ) . map ( option_from_exam ) . sort ( sort_fun )
@@ -408,6 +428,7 @@ function EditGroup({name, setName, group, setGroup, examDict}:{
408428
409429 return < Group controlId = { `group-${ name } ` } label = "gruppo" >
410430 { } < input value = { name } onChange = { e => setName ( e . target . value ) } />
431+ { } < Button variant = "danger" onClick = { erase } > elimina gruppo</ Button >
411432 < Select isMulti isSearchable
412433 options = { options }
413434 value = { selected }
0 commit comments