|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Heading, Button } from '@codeday/topo/Atom'; |
| 3 | +import { Content } from '@codeday/topo/Molecule'; |
| 4 | +import { Form } from '@rjsf/chakra-ui'; |
| 5 | +import Page from '../../../../../../../components/Page'; |
| 6 | +import { useSurveyResponses } from '../../../../../../../utils'; |
| 7 | +import { SurveyQuery, SurveyRespondMutation } from './index.gql'; |
| 8 | +import { apiFetch } from '@codeday/topo/utils'; |
| 9 | + |
| 10 | +export default function SurveyPage({ student, survey, token, surveyId, occurrenceId }) { |
| 11 | + const [isLoading, setIsLoading] = useState(false); |
| 12 | + const [isSubmitted, setIsSubmitted] = useState(false); |
| 13 | + const [responses, setResponse, getResponse] = useSurveyResponses(); |
| 14 | + |
| 15 | + const projects = student.projects.filter((p) => p.status === 'MATCHED'); |
| 16 | + const mentors = projects |
| 17 | + .map((p) => p.mentors.map((s) => ({ ...s, projectId: p.id }))) |
| 18 | + .flat(); |
| 19 | + const teammates = projects |
| 20 | + .map((p) => p.students.map((s) => ({ ...s, projectId: p.id }))) |
| 21 | + .flat() |
| 22 | + .filter((s) => s.id !== student.id); |
| 23 | + |
| 24 | + if (isSubmitted) { |
| 25 | + return ( |
| 26 | + <Page slug={`/dash/s/${token}/survey/${surveyId}/${occurrenceId}`} title="Form Submission"> |
| 27 | + <Content> |
| 28 | + Thanks for submitting this survey. You can now close the page. |
| 29 | + </Content> |
| 30 | + </Page> |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + return ( |
| 35 | + <Page slug={`/dash/s/${token}/survey/${surveyId}/${occurrenceId}`} title="Form Submission"> |
| 36 | + {survey?.selfSchema && ( |
| 37 | + <Content> |
| 38 | + <Heading fontSize="5xl">Self-Reflection</Heading> |
| 39 | + <Form |
| 40 | + schema={survey.selfSchema} |
| 41 | + uiSchema={survey.selfUi} |
| 42 | + onChange={(e) => setResponse({ response: e.formData, student: student.id })} |
| 43 | + formData={getResponse({ student: student.id })?.response || {}} |
| 44 | + disabled={isLoading} |
| 45 | + children={true} |
| 46 | + /> |
| 47 | + </Content> |
| 48 | + )} |
| 49 | + {survey?.projectSchema && projects.map((p) => ( |
| 50 | + <Content> |
| 51 | + <Heading fontSize="5xl"> |
| 52 | + Project Reflection: Your project with {[...p.mentors, ...p.students].map(m => m.givenName).join('/')} |
| 53 | + </Heading> |
| 54 | + <Form |
| 55 | + schema={survey.projectSchema} |
| 56 | + uiSchema={survey.projectUi} |
| 57 | + onChange={(e) => setResponse({ response: e.formData, project: p.id })} |
| 58 | + formData={getResponse({ project: p.id })?.response || {}} |
| 59 | + disabled={isLoading} |
| 60 | + children={true} |
| 61 | + /> |
| 62 | + </Content> |
| 63 | + ))} |
| 64 | + {survey?.peerSchema && teammates.map((p) => ( |
| 65 | + <Content> |
| 66 | + <Heading fontSize="5xl">Peer Reflection: {p.givenName} {p.surname}</Heading> |
| 67 | + <Form |
| 68 | + schema={survey.peerSchema} |
| 69 | + uiSchema={survey.peerUi} |
| 70 | + onChange={(e) => setResponse({ response: e.formData, student: p.id })} |
| 71 | + formData={getResponse({ student: p.id })?.response || {}} |
| 72 | + disabled={isLoading} |
| 73 | + children={true} |
| 74 | + /> |
| 75 | + </Content> |
| 76 | + ))} |
| 77 | + {survey?.mentorSchema && mentors.map((m) => ( |
| 78 | + <Content> |
| 79 | + <Heading fontSize="5xl">Mentor Reflection: {m.givenName} {m.surname}</Heading> |
| 80 | + <Form |
| 81 | + schema={survey.mentorSchema} |
| 82 | + uiSchema={survey.mentorUi} |
| 83 | + onChange={(e) => setResponse({ response: e.formData, mentor: m.id })} |
| 84 | + formData={getResponse({ mentor: m.id })?.response || {}} |
| 85 | + disabled={isLoading} |
| 86 | + children={true} |
| 87 | + /> |
| 88 | + </Content> |
| 89 | + ))} |
| 90 | + <Content textAlign="center" mt={8}> |
| 91 | + <Button |
| 92 | + isLoading={isLoading} |
| 93 | + size="lg" |
| 94 | + colorScheme="green" |
| 95 | + onClick={async () => { |
| 96 | + setIsLoading(true); |
| 97 | + try { |
| 98 | + const res = await apiFetch( |
| 99 | + SurveyRespondMutation, |
| 100 | + { occurrenceId, responses }, |
| 101 | + { 'X-Labs-Authorization': `Bearer ${token}` }, |
| 102 | + ); |
| 103 | + setIsSubmitted(true); |
| 104 | + } catch (ex) { console.error(ex); } |
| 105 | + setIsLoading(false); |
| 106 | + }} |
| 107 | + > |
| 108 | + Submit |
| 109 | + </Button> |
| 110 | + </Content> |
| 111 | + </Page> |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +export async function getServerSideProps({ params: { token, surveyId, occurrenceId }}) { |
| 116 | + const res = await apiFetch(SurveyQuery, { surveyId }, { 'X-Labs-Authorization': `Bearer ${token}` }); |
| 117 | + if (res?.labs?.student?.status !== 'ACCEPTED') throw new Error('Not accepted.'); |
| 118 | + if (!res?.labs?.survey) throw new Error('No survey.'); |
| 119 | + return { |
| 120 | + props: { |
| 121 | + student: res.labs.student, |
| 122 | + survey: res.labs.survey, |
| 123 | + surveyId, |
| 124 | + occurrenceId, |
| 125 | + token, |
| 126 | + }, |
| 127 | + }; |
| 128 | +} |
0 commit comments