-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WAITP-1452: Recent survey response page (#5157)
* WAITP-1478 Add types definition for RecentSurveyResponseRoute * WAITP-1478 Regen types * WAITP-1478 Add route for RecentSurveyResponse * WAITP-1478 Fixup export * WAITP-1478 Update url * WAITP-1478 Regen types * useSurveyResponse * survey review screen * wip set default values * set up route * Update useLogin.ts * refactor survey context to be at survey level * remove bad merge * survey data * basic working setup * remove debug changes * split out paginator * put page back in survey layout * refactor layout components * Update index.ts * Update SurveyContext.tsx * Update SurveyReviewSection.tsx --------- Co-authored-by: Ethan McQuarrie <ethan@beyondessential.com.au>
- Loading branch information
Showing
22 changed files
with
422 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/datatrak-web/src/api/queries/useSurveyResponse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd | ||
*/ | ||
import { useQuery } from 'react-query'; | ||
import { DatatrakWebSingleSurveyResponseRequest } from '@tupaia/types'; | ||
import { get } from '../api'; | ||
|
||
export const useSurveyResponse = (surveyResponseId?: string) => { | ||
return useQuery( | ||
['surveyResponse', surveyResponseId], | ||
(): Promise<DatatrakWebSingleSurveyResponseRequest.ResBody> => | ||
get(`surveyResponse/${surveyResponseId}`), | ||
{ enabled: !!surveyResponseId }, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/datatrak-web/src/features/Survey/Components/SurveyPaginator.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import { useOutletContext } from 'react-router-dom'; | ||
import ArrowBackIosIcon from '@material-ui/icons/ArrowBackIos'; | ||
import { useSurveyForm } from '../SurveyContext'; | ||
import { Button } from '../../../components'; | ||
import { useIsMobile } from '../../../utils'; | ||
|
||
const FormActions = styled.div` | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
padding: 1rem 0.5rem; | ||
border-top: 1px solid ${props => props.theme.palette.divider}; | ||
button:last-child { | ||
margin-left: auto; | ||
} | ||
${({ theme }) => theme.breakpoints.up('md')} { | ||
padding: 1rem; | ||
} | ||
`; | ||
|
||
const ButtonGroup = styled.div` | ||
display: flex; | ||
button, | ||
a { | ||
&:last-child { | ||
margin-left: 1rem; | ||
} | ||
} | ||
`; | ||
|
||
const BackButton = styled(Button).attrs({ | ||
startIcon: <ArrowBackIosIcon />, | ||
variant: 'text', | ||
color: 'default', | ||
})` | ||
${({ theme }) => theme.breakpoints.down('md')} { | ||
padding-left: 0.8rem; | ||
.MuiButton-startIcon { | ||
margin-right: 0.25rem; | ||
} | ||
} | ||
`; | ||
|
||
type SurveyLayoutContextT = { isLoading: boolean; onStepPrevious: () => void }; | ||
|
||
export const SurveyPaginator = () => { | ||
const { isLast, isReviewScreen, openCancelConfirmation } = useSurveyForm(); | ||
const isMobile = useIsMobile(); | ||
const { isLoading, onStepPrevious } = useOutletContext<SurveyLayoutContextT>(); | ||
|
||
const getNextButtonText = () => { | ||
if (isReviewScreen) return 'Submit'; | ||
if (isLast) { | ||
return isMobile ? 'Review' : 'Review and submit'; | ||
} | ||
return 'Next'; | ||
}; | ||
|
||
const nextButtonText = getNextButtonText(); | ||
|
||
return ( | ||
<FormActions> | ||
<BackButton onClick={onStepPrevious} disabled={isLoading}> | ||
Back | ||
</BackButton> | ||
<ButtonGroup> | ||
<Button onClick={openCancelConfirmation} variant="outlined" disabled={isLoading}> | ||
Cancel | ||
</Button> | ||
<Button type="submit" disabled={isLoading}> | ||
{nextButtonText} | ||
</Button> | ||
</ButtonGroup> | ||
</FormActions> | ||
); | ||
}; |
79 changes: 79 additions & 0 deletions
79
packages/datatrak-web/src/features/Survey/Components/SurveyReviewSection.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import React from 'react'; | ||
import { QuestionType } from '@tupaia/types'; | ||
import styled from 'styled-components'; | ||
import { Typography } from '@material-ui/core'; | ||
import { SurveyQuestionGroup } from './SurveyQuestionGroup'; | ||
import { formatSurveyScreenQuestions, getSurveyScreenNumber } from '../utils'; | ||
import { useSurveyForm } from '../SurveyContext'; | ||
|
||
const Section = styled.section` | ||
padding: 1rem 0; | ||
&:first-child { | ||
padding-top: 0; | ||
} | ||
`; | ||
|
||
const SectionHeader = styled(Typography).attrs({ | ||
variant: 'h3', | ||
})` | ||
font-size: 1rem; | ||
font-weight: ${({ theme }) => theme.typography.fontWeightMedium}; | ||
margin-bottom: 1rem; | ||
${({ theme }) => theme.breakpoints.up('sm')} { | ||
font-size: 1.125rem; | ||
} | ||
`; | ||
|
||
const Fieldset = styled.fieldset.attrs({ | ||
disabled: true, | ||
})` | ||
border: none; | ||
margin: 0; | ||
padding: 0; | ||
input, | ||
label, | ||
button, | ||
.MuiInputBase-root, | ||
link { | ||
pointer-events: none; | ||
} | ||
`; | ||
export const SurveyReviewSection = () => { | ||
const { visibleScreens } = useSurveyForm(); | ||
|
||
if (!visibleScreens || !visibleScreens.length) { | ||
return null; | ||
} | ||
|
||
// split the questions into sections by screen so it's easier to read the long form | ||
const questionSections = visibleScreens.map(screen => { | ||
const { surveyScreenComponents } = screen; | ||
const screenNumber = getSurveyScreenNumber(visibleScreens, screen); | ||
const heading = surveyScreenComponents[0].text; | ||
const firstQuestionIsInstruction = surveyScreenComponents[0].type === QuestionType.Instruction; | ||
|
||
// if the first question is an instruction, don't display it, because it will be displayed as the heading | ||
const questionsToDisplay = firstQuestionIsInstruction | ||
? surveyScreenComponents.slice(1) | ||
: surveyScreenComponents; | ||
return { | ||
heading, | ||
questions: formatSurveyScreenQuestions(questionsToDisplay, screenNumber), | ||
}; | ||
}); | ||
return ( | ||
<Fieldset> | ||
{questionSections.map(({ heading, questions }, index) => ( | ||
<Section key={index}> | ||
<SectionHeader>{heading}</SectionHeader> | ||
<SurveyQuestionGroup questions={questions} /> | ||
</Section> | ||
))} | ||
</Fieldset> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.