Skip to content

Commit 6244689

Browse files
committed
fix creazione nuovo degree
1 parent 25499e4 commit 6244689

4 files changed

Lines changed: 48 additions & 22 deletions

File tree

api/controllers/ModelController.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function queryFieldsToPipeline(query={}, fields={}) {
8888
match: {$literal: $match},
8989
}},
9090
]
91-
console.log(`queryFieldsToPipeline ${JSON.stringify(query)} => ${JSON.stringify(pipeline)}`)
91+
// console.log(`queryFieldsToPipeline ${JSON.stringify(query)} => ${JSON.stringify(pipeline)}`)
9292
return pipeline
9393
}
9494

@@ -106,7 +106,8 @@ const ModelController = {
106106
// vengono serializzati correttamente:
107107
// gli ObjectId sembrano stringhe,
108108
// le RegExp sembrano oggetti vuoti
109-
console.log(`${Model.modelName}.index PIPELINE ${JSON.stringify(pipeline)}`)
109+
//
110+
// console.log(`${Model.modelName}.index PIPELINE ${JSON.stringify(pipeline)}`)
110111

111112
const [ res ] = await Model.aggregate(pipeline)
112113

@@ -131,14 +132,17 @@ const ModelController = {
131132
if (empty) obj._id = undefined
132133
return obj
133134
} catch(err) {
134-
console.log(`not found ${id}`)
135-
throw new NotFoundError()
135+
if (err instanceof mongoose.Error.NotFoundError) {
136+
console.log(`not found ${id}`)
137+
throw new NotFoundError()
138+
}
139+
throw err
136140
}
137141
},
138142

139143
patch: async (Model, id, data) => {
140144
try {
141-
console.log(`ModelController.patch ${id} ${JSON.stringify(data)}`)
145+
console.log(`ModelController.patch ${Model.modelName} ${id} ${JSON.stringify(data)}`)
142146
await Model.findByIdAndUpdate(id, data, { runValidators: true })
143147
return {ok: true}
144148
} catch(err) {

api/models/Degree.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ const Degree = mongoose.model('Degree', {
2727
type: mongoose.Schema.Types.ObjectId,
2828
ref: Exam
2929
}
30-
]
30+
],
31+
default: () => new Map()
3132
},
3233
enabled: {
3334
type: Boolean,

frontend/src/pages/DegreePage.tsx

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

188188
function 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

390409
type 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}

frontend/src/pages/DegreesPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99

1010
export default function DegreesPage() {
1111
return <>
12-
<h1>Corsi di Laurea</h1>
12+
<h1>Corsi di Studio</h1>
1313
<QueryTableCard sort="academic_year" direction={-1}>
1414
<QueryTableBar>
1515
<FilterButton>
@@ -21,7 +21,7 @@ export default function DegreesPage() {
2121
</FilterButton>
2222

2323
<ItemAddButton>
24-
Aggiungi corso di Laurea
24+
Aggiungi corso di studio
2525
</ItemAddButton>
2626

2727
<TableTopRightButtons>

0 commit comments

Comments
 (0)