From 1ee268107b0a4cd68e6908de5e0ca959e6491741 Mon Sep 17 00:00:00 2001 From: Tran Manh Date: Mon, 24 Jul 2023 10:12:31 +0700 Subject: [PATCH] [#32] Navigate to survey completion screen after submitting the survey successfully --- src/adapters/Survey/index.ts | 5 +- src/components/Answer/index.tsx | 61 +++++++++++++++++++++--- src/components/MultiInputs/index.tsx | 25 ++++------ src/components/TextArea/index.tsx | 20 ++------ src/routes/index.tsx | 11 +++++ src/screens/Question/Complete/index.tsx | 7 +++ src/screens/Question/index.test.tsx | 35 +++++++++++++- src/screens/Question/index.tsx | 58 ++++++++++++++++++---- src/screens/Survey/index.test.tsx | 2 + src/store/reducers/Survey/action.ts | 25 +++++++++- src/store/reducers/Survey/index.test.ts | 2 + src/store/reducers/Survey/index.ts | 29 +++++++++-- src/types/request/surveySubmitRequest.ts | 8 ++-- 13 files changed, 226 insertions(+), 62 deletions(-) create mode 100644 src/screens/Question/Complete/index.tsx diff --git a/src/adapters/Survey/index.ts b/src/adapters/Survey/index.ts index 4a0636d..057b933 100644 --- a/src/adapters/Survey/index.ts +++ b/src/adapters/Survey/index.ts @@ -1,8 +1,11 @@ import { getAuth, postAuth } from 'adapters/BaseAuth'; +import { JSONObject } from 'helpers/json'; import { SurveySubmitRequest } from 'types/request/surveySubmitRequest'; export const getSurveys = () => getAuth('surveys?page[number]=1&page[size]=10'); export const getSurvey = (surveyId: string) => getAuth(`surveys/${surveyId}`); -export const submitSurvey = (surveySubmitRequest: SurveySubmitRequest) => postAuth('responses', surveySubmitRequest); +export const submitSurvey = (surveySubmitRequest: SurveySubmitRequest) => { + return postAuth('responses', surveySubmitRequest as unknown as JSONObject); +}; diff --git a/src/components/Answer/index.tsx b/src/components/Answer/index.tsx index a9ed91c..6793a1a 100644 --- a/src/components/Answer/index.tsx +++ b/src/components/Answer/index.tsx @@ -8,12 +8,14 @@ import Nps from 'components/Nps'; import Rating from 'components/Rating'; import TextArea from 'components/TextArea'; import { DisplayType, Question, getDisplayTypeEnum } from 'types/question'; +import { AnswerRequest } from 'types/request/surveySubmitRequest'; interface AnswerProps { question: Question; + onAnswerChanged?: (answers: AnswerRequest[]) => void; 'data-test-id'?: string; } -const Answer = ({ question, ...rest }: AnswerProps): JSX.Element => { +const Answer = ({ question, onAnswerChanged, ...rest }: AnswerProps): JSX.Element => { const displayTypeEnum = getDisplayTypeEnum(question); switch (displayTypeEnum) { @@ -27,6 +29,7 @@ const Answer = ({ question, ...rest }: AnswerProps): JSX.Element => { items={question.answers} displayType={displayTypeEnum} data-test-id={rest['data-test-id']} + onValueChanged={(answer) => onAnswerChanged?.([{ id: answer.id }])} /> ); case DisplayType.Choice: @@ -36,18 +39,64 @@ const Answer = ({ question, ...rest }: AnswerProps): JSX.Element => { items={question.answers} isPickOne={question?.pick === 'one'} data-test-id={rest['data-test-id']} + onValuesChanged={(answers) => { + const answerRequests: AnswerRequest[] = answers.map((answer) => ({ + id: answer.id, + })); + return onAnswerChanged?.(answerRequests); + }} /> ); case DisplayType.Nps: - return ; + return ( + { + const answerRequests: AnswerRequest[] = answers.map((answer) => ({ + id: answer.id, + })); + return onAnswerChanged?.(answerRequests); + }} + /> + ); case DisplayType.Textarea: - return