From ee2e23f56f19205ce0f901fef31ab0750939f2d5 Mon Sep 17 00:00:00 2001 From: David Huang Date: Sat, 4 May 2024 20:50:25 +0800 Subject: [PATCH] add support for select all, none, and other --- .../EventApplication/ApplicationPopup.tsx | 52 +++++++++++++++---- utils/constants.ts | 9 ++++ 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/components/Event/EventApplication/ApplicationPopup.tsx b/components/Event/EventApplication/ApplicationPopup.tsx index 3e849b2..4267d6b 100644 --- a/components/Event/EventApplication/ApplicationPopup.tsx +++ b/components/Event/EventApplication/ApplicationPopup.tsx @@ -1,4 +1,4 @@ -import { Modal } from 'antd'; +import { Modal, message } from 'antd'; import 'survey-core/defaultV2.min.css'; import { Model } from 'survey-core'; import { Survey } from 'survey-react-ui'; @@ -8,6 +8,7 @@ import { ApplicationAnswer, QueriedVolunteerEventDTO, } from 'bookem-shared/src/types/database'; +import * as constants from '@/utils/constants'; // choices can type string[] or null const processChoices = (choices: String[] | undefined) => { @@ -15,12 +16,36 @@ const processChoices = (choices: String[] | undefined) => { return null; } - return choices.map(choice => { - return { - value: choice, - text: choice, - }; + const processedChoices: any[] = []; + let showNoneItem = false; + let showOtherItem = false; + let showSelectAllItem = false; + + choices.forEach(choice => { + switch (choice) { + case constants.NONE: + showNoneItem = true; + break; + case constants.OTHER: + showOtherItem = true; + break; + case constants.SELECT_ALL: + showSelectAllItem = true; + break; + default: + processedChoices.push({ + value: choice, + text: choice, + }); + } }); + + return { + showNoneItem, + showOtherItem, + showSelectAllItem, + choices: processedChoices, + }; }; // process answes to fit the answer type. return the answer casted into application answer @@ -40,6 +65,9 @@ const processAnswers = (answerObj: any) => { return answers; }; +// TODO modify survey model +// TODO modify process response + const makeSurveyModel = (questions: ApplicationQuestionData[]) => { const elements = questions.map(question => { return { @@ -47,7 +75,7 @@ const makeSurveyModel = (questions: ApplicationQuestionData[]) => { title: question.title, type: question.type, isRequired: question.isRequired, - choices: processChoices(question.choices), + ...processChoices(question.choices), }; }); @@ -72,11 +100,11 @@ export default function ApplicationPopup({ let survey = new Model(makeSurveyModel(questions)); - survey.onComplete.add(result => { + survey.onComplete.add(async result => { const answers = processAnswers(result.data); console.log('Survey results: ', answers); // send the answers to the server - fetch('/api/event/' + event._id + '/apply', { + const res = await fetch('/api/event/' + event._id + '/apply', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -86,6 +114,12 @@ export default function ApplicationPopup({ }), }); + if (res.status !== 200) { + message.error('Failed to submit application'); + console.error('Failed to submit application'); + return; + } + onClose(); onSubmit(); }); diff --git a/utils/constants.ts b/utils/constants.ts index e73e160..c46ddfa 100644 --- a/utils/constants.ts +++ b/utils/constants.ts @@ -111,3 +111,12 @@ export const EVENT_CONTACT_ICON_WIDTH = 23; * Default height of event contact icons */ export const EVENT_CONTACT_ICON_HEIGHT = 23; + + +/** + * Constants for application form choices + */ + +export const NONE = 'None'; +export const OTHER = 'Other'; +export const SELECT_ALL = 'Select All';