diff --git a/src/admin/NewVote.jsx b/src/admin/NewVote.jsx index 03f30fd..f049d49 100644 --- a/src/admin/NewVote.jsx +++ b/src/admin/NewVote.jsx @@ -54,34 +54,43 @@ export default function NewVote() { const berlinStartTime = moment.tz(startTime, "Europe/Berlin").toDate(); const berlinEndTime = moment.tz(endTime, "Europe/Berlin").toDate(); - const vote = await setDoc(doc(db, "/votes", id), { - title: title, - description: description, - selectCount: selectCount, - startTime: Timestamp.fromDate(berlinStartTime), - endTime: Timestamp.fromDate(berlinEndTime), - active: true, - version: 3, - }); - const option = options.map(async (e, index) => { - return addDoc(collection(db, `/votes/${id}/options`), { - title: e.title, - max: e.max, - teacher: e.teacher, - description: e.description, + try { + await setDoc(doc(db, "/votes", id), { + title: title, + description: description, + selectCount: selectCount, + startTime: Timestamp.fromDate(berlinStartTime), + endTime: Timestamp.fromDate(berlinEndTime), + active: true, + version: 3, + extraFields: extraFields, + }); + const option = options.map(async (e) => { + return addDoc(collection(db, `/votes/${id}/options`), { + title: e.title, + max: e.max, + teacher: e.teacher, + description: e.description, + }); }); - }); - await Promise.all(option); + await Promise.all(option); - console.log("Vote created successfully."); + console.log("Vote created successfully."); - snackbar({ - message: "Wahl erfolgreich erstellt.", - timeout: 5000, - }); + snackbar({ + message: "Wahl erfolgreich erstellt.", + timeout: 5000, + }); - navigate(`/admin/${id}`); + navigate(`/admin/${id}`); + } catch (e) { + console.error(e); + snackbar({ + message: "Fehler beim Erstellen der Wahl.", + timeout: 5000, + }); + } } const submitDisabled = () => { @@ -268,6 +277,7 @@ export default function NewVote() { )} {options.map((e, i) => ( 0 ? extraFields : [], - }, - { merge: true } - ); - const optionsPromises = options.map(async (e) => { - return setDoc(doc(db, `/votes/${vote.id}/options/${e.id}`), { - title: e.title, - max: e.max, - teacher: e.teacher, - description: e.description, + await setDoc( + doc(db, "/votes", vote.id), + { + title, + description: description || "", + extraFields: extraFields.length > 0 ? extraFields : [], + }, + { merge: true } + ); + const optionsPromises = options.map(async (e) => { + return setDoc(doc(db, `/votes/${vote.id}/options/${e.id}`), { + title: e.title, + max: e.max, + teacher: e.teacher, + description: e.description, + }); }); - }); await Promise.all(optionsPromises); @@ -244,25 +244,24 @@ export default function Edit() { >

{extraFields.map((e, i) => ( - {extraFields.map((e, i) => ( - -

- editExtraField(i, e.target.value)} - > - removeExtraField(i)} - /> - -
-

- - ))} + +

+ editExtraField(i, e.target.value)} + > + removeExtraField(i)} + /> + +
+

+ + ))}

({ id: doc.id, ...doc.data() })); - + const optionData = options.docs.map((doc) => ({ + id: doc.id, + ...doc.data(), + })); + return { vote: voteData, options: optionData,