From 489d98b5d12618d376ef96909225c297b3f36d97 Mon Sep 17 00:00:00 2001 From: Sean Fong Date: Tue, 1 Aug 2023 23:02:28 +0930 Subject: [PATCH] Clean up code dependencies for dashboard tables --- .../DashboardNav/DashboardNavSection.tsx | 6 ++ .../Buttons/CreateNewResponseButton.tsx | 10 +-- .../Buttons/ViewExistingResponsesButton.tsx | 53 ++----------- .../Buttons/OpenResponseButton.tsx | 13 ++-- .../TableComponents/ResponseLabel.styles.ts | 8 +- .../TableComponents/ResponseLabel.tsx | 4 +- .../contexts/SelectedQuestionnaireContext.tsx | 11 +-- .../hooks/useFetchExistingResponses.ts | 75 +++++++++++++++++++ .../dashboard/hooks/useFetchQuestionnaires.ts | 18 ++--- .../dashboard/hooks/useFetchResponses.ts | 22 ++---- .../src/features/save/api/saveQr.ts | 2 +- .../components/Authorisation.tsx | 6 +- .../src/stores/useConfigStore.ts | 7 +- 13 files changed, 135 insertions(+), 100 deletions(-) create mode 100644 apps/smart-forms-app/src/features/dashboard/hooks/useFetchExistingResponses.ts diff --git a/apps/smart-forms-app/src/features/dashboard/components/DashboardNav/DashboardNavSection.tsx b/apps/smart-forms-app/src/features/dashboard/components/DashboardNav/DashboardNavSection.tsx index b115db3b0..19256c235 100644 --- a/apps/smart-forms-app/src/features/dashboard/components/DashboardNav/DashboardNavSection.tsx +++ b/apps/smart-forms-app/src/features/dashboard/components/DashboardNav/DashboardNavSection.tsx @@ -26,6 +26,7 @@ import { NavSectionWrapper } from '../../../../components/Nav/Nav.styles.ts'; import useConfigStore from '../../../../stores/useConfigStore.ts'; +import { useLocation } from 'react-router-dom'; interface DashboardNavSectionProps { onCloseNav: () => void; @@ -35,6 +36,11 @@ const DashboardNavSection = memo(function DashboardNavSection(props: DashboardNa const { onCloseNav } = props; const smartClient = useConfigStore((state) => state.smartClient); + const { pathname } = useLocation(); + + if (pathname === '/dashboard/existing') { + return null; + } return ( diff --git a/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/QuestionnairePage/Buttons/CreateNewResponseButton.tsx b/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/QuestionnairePage/Buttons/CreateNewResponseButton.tsx index 9b129e4c6..2f67a0d9b 100644 --- a/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/QuestionnairePage/Buttons/CreateNewResponseButton.tsx +++ b/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/QuestionnairePage/Buttons/CreateNewResponseButton.tsx @@ -33,20 +33,20 @@ function CreateNewResponseButton() { const buildSourceResponse = useQuestionnaireResponseStore((state) => state.buildSourceResponse); const { selectedQuestionnaire } = useContext(SelectedQuestionnaireContext); + const launchQuestionnaire = useConfigStore((state) => state.launchQuestionnaire); + const questionnaire = selectedQuestionnaire ?? launchQuestionnaire; const navigate = useNavigate(); const [isLoading, setIsLoading] = useState(false); async function handleClick() { - if (!selectedQuestionnaire) return; + if (!questionnaire) return; setIsLoading(true); - const questionnaire = selectedQuestionnaire.resource; - // Post questionnaire to client if it is SMART Health IT and its variants - if (smartClient?.state.serverUrl.includes('/v/r4/fhir')) { + if (smartClient?.state.serverUrl.includes('https://launch.smarthealthit.org/v/r4/fhir')) { questionnaire.id = questionnaire.id + '-SMARTcopy'; postQuestionnaireToSMARTHealthIT(smartClient, questionnaire); } @@ -69,7 +69,7 @@ function CreateNewResponseButton() { setIsLoading(false); } - const buttonIsDisabled = !selectedQuestionnaire?.listItem || isLoading; + const buttonIsDisabled = !questionnaire || isLoading; return ( diff --git a/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/QuestionnairePage/Buttons/ViewExistingResponsesButton.tsx b/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/QuestionnairePage/Buttons/ViewExistingResponsesButton.tsx index 33a3019b7..798dda812 100644 --- a/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/QuestionnairePage/Buttons/ViewExistingResponsesButton.tsx +++ b/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/QuestionnairePage/Buttons/ViewExistingResponsesButton.tsx @@ -15,70 +15,31 @@ * limitations under the License. */ -import { useContext, useMemo } from 'react'; -import { getClientBundlePromise, getResponsesFromBundle } from '../../../../utils/dashboard.ts'; -import { useQuery } from '@tanstack/react-query'; -import type { Bundle, QuestionnaireResponse } from 'fhir/r4'; +import { useContext } from 'react'; import { SelectedQuestionnaireContext } from '../../../../contexts/SelectedQuestionnaireContext.tsx'; import { useNavigate } from 'react-router-dom'; import { useSnackbar } from 'notistack'; -import useConfigStore from '../../../../../../stores/useConfigStore.ts'; import { CircularProgress, IconButton, Stack, Typography } from '@mui/material'; import HighlightOffIcon from '@mui/icons-material/HighlightOff'; import GradingIcon from '@mui/icons-material/Grading'; +import useFetchExistingResponses from '../../../../hooks/useFetchExistingResponses.ts'; function ViewExistingResponsesButton() { const { selectedQuestionnaire, setExistingResponses } = useContext(SelectedQuestionnaireContext); - const smartClient = useConfigStore((state) => state.smartClient); - const patient = useConfigStore((state) => state.patient); - const navigate = useNavigate(); const { enqueueSnackbar } = useSnackbar(); - // search responses from selected questionnaire - let questionnaireRefParam = ''; - - // Have different questionnaireRef config due to SMART Health IT limitation - if (smartClient) { - const questionnaireRef = smartClient.state.serverUrl.includes('/v/r4/fhir') - ? `Questionnaire/${selectedQuestionnaire?.resource?.id}-SMARTcopy` - : selectedQuestionnaire?.resource?.url; - - if (questionnaireRef) { - questionnaireRefParam = `questionnaire=${questionnaireRef}&`; - } - } - - const patientIdParam = patient?.id ? `patient=${patient?.id}&` : ''; - const queryUrl = '/QuestionnaireResponse?' + questionnaireRefParam + patientIdParam; + const { existingResponses, fetchError, isFetching } = useFetchExistingResponses(); - const { data, isFetching, error } = useQuery( - ['existingResponses', queryUrl], - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - () => getClientBundlePromise(smartClient!, queryUrl), - { - enabled: - !!selectedQuestionnaire && - questionnaireRefParam !== '' && - patientIdParam !== '' && - !!smartClient - } - ); - - if (error) { - console.error(error); + if (fetchError) { + console.error(fetchError); enqueueSnackbar('An error occurred while fetching existing responses', { variant: 'error', preventDuplicate: true }); } - const existingResponses: QuestionnaireResponse[] = useMemo( - () => getResponsesFromBundle(data), - [data] - ); - function handleClick() { setExistingResponses(existingResponses); navigate('/dashboard/responses'); @@ -95,7 +56,7 @@ function ViewExistingResponsesButton() { data-test="button-view-responses"> {isFetching && selectedQuestionnaire ? ( - ) : data && existingResponses.length === 0 ? ( + ) : existingResponses.length === 0 ? ( ) : ( @@ -110,7 +71,7 @@ function ViewExistingResponsesButton() { sx={{ mt: -0.5, mb: 0.5 }}> {isFetching && selectedQuestionnaire ? 'Loading responses' - : data && existingResponses.length === 0 + : existingResponses.length === 0 ? 'No responses found' : 'View responses'} diff --git a/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/ResponsesPage/Buttons/OpenResponseButton.tsx b/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/ResponsesPage/Buttons/OpenResponseButton.tsx index 2216c2925..3c0127ac8 100644 --- a/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/ResponsesPage/Buttons/OpenResponseButton.tsx +++ b/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/ResponsesPage/Buttons/OpenResponseButton.tsx @@ -17,7 +17,7 @@ import { useMemo, useState } from 'react'; import { useQuery } from '@tanstack/react-query'; -import type { Bundle, Questionnaire } from 'fhir/r4'; +import type { Bundle, Questionnaire, QuestionnaireResponse } from 'fhir/r4'; import { getFormsServerBundleOrQuestionnairePromise, getReferencedQuestionnaire @@ -25,7 +25,6 @@ import { import { useNavigate } from 'react-router-dom'; import { useSnackbar } from 'notistack'; import { postQuestionnaireToSMARTHealthIT } from '../../../../../save/api/saveQr.ts'; -import type { SelectedResponse } from '../../../../types/list.interface.ts'; import { assembleIfRequired } from '../../../../../assemble/utils/assemble.ts'; import useConfigStore from '../../../../../../stores/useConfigStore.ts'; import useQuestionnaireStore from '../../../../../../stores/useQuestionnaireStore.ts'; @@ -34,7 +33,7 @@ import { CircularProgress, IconButton, Stack, Typography } from '@mui/material'; import OpenInNewIcon from '@mui/icons-material/OpenInNew'; interface Props { - selectedResponse: SelectedResponse | null; + selectedResponse: QuestionnaireResponse | null; } function OpenResponseButton(props: Props) { const { selectedResponse } = props; @@ -53,7 +52,7 @@ function OpenResponseButton(props: Props) { const { enqueueSnackbar } = useSnackbar(); // reference could either be a canonical or an id - const questionnaireRef = selectedResponse?.resource.questionnaire; + const questionnaireRef = selectedResponse?.questionnaire; let queryUrl = ''; if (questionnaireRef) { @@ -109,7 +108,7 @@ function OpenResponseButton(props: Props) { } // Post questionnaire to client if it is SMART Health IT - if (smartClient?.state.serverUrl.includes('/v/r4/fhir')) { + if (smartClient?.state.serverUrl.includes('https://launch.smarthealthit.org/v/r4/fhir')) { referencedQuestionnaire.id = referencedQuestionnaire.id + '-SMARTcopy'; postQuestionnaireToSMARTHealthIT(smartClient, referencedQuestionnaire); } @@ -118,8 +117,8 @@ function OpenResponseButton(props: Props) { await buildSourceQuestionnaire(referencedQuestionnaire); // Assign questionnaireResponse to questionnaireResponse provider - buildSourceResponse(selectedResponse.resource); - updatePopulatedProperties(selectedResponse.resource); + buildSourceResponse(selectedResponse); + updatePopulatedProperties(selectedResponse); navigate('/viewer'); setIsLoading(false); diff --git a/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/ResponsesPage/TableComponents/ResponseLabel.styles.ts b/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/ResponsesPage/TableComponents/ResponseLabel.styles.ts index 24b04fc2c..f5b3b6c44 100644 --- a/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/ResponsesPage/TableComponents/ResponseLabel.styles.ts +++ b/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/ResponsesPage/TableComponents/ResponseLabel.styles.ts @@ -19,9 +19,9 @@ import { alpha, styled } from '@mui/material/styles'; import type { Theme } from '@mui/material'; import { Box } from '@mui/material'; -import type { ResponseListItem } from '../../../../types/list.interface.ts'; +import type { QuestionnaireResponse } from 'fhir/r4'; -const handleColorType = (color: ResponseListItem['status'], theme: Theme) => { +const handleColorType = (color: QuestionnaireResponse['status'], theme: Theme) => { switch (color) { case 'in-progress': return theme.palette.primary.dark; @@ -36,7 +36,7 @@ const handleColorType = (color: ResponseListItem['status'], theme: Theme) => { } }; -const handleBgColorType = (color: ResponseListItem['status'], theme: Theme) => { +const handleBgColorType = (color: QuestionnaireResponse['status'], theme: Theme) => { switch (color) { case 'in-progress': return alpha(theme.palette.primary.main, 0.16); @@ -53,7 +53,7 @@ const handleBgColorType = (color: ResponseListItem['status'], theme: Theme) => { export const ResponseStyledLabel = styled(Box, { shouldForwardProp: (prop) => prop !== 'color' -})<{ color: ResponseListItem['status'] }>(({ theme, color }) => ({ +})<{ color: QuestionnaireResponse['status'] }>(({ theme, color }) => ({ height: 24, minWidth: 22, lineHeight: 0, diff --git a/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/ResponsesPage/TableComponents/ResponseLabel.tsx b/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/ResponsesPage/TableComponents/ResponseLabel.tsx index fe491abc5..98b09cdce 100644 --- a/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/ResponsesPage/TableComponents/ResponseLabel.tsx +++ b/apps/smart-forms-app/src/features/dashboard/components/DashboardPages/ResponsesPage/TableComponents/ResponseLabel.tsx @@ -21,10 +21,10 @@ import { useTheme } from '@mui/material/styles'; import type { SxProps, Theme } from '@mui/material'; import { Box } from '@mui/material'; import { ResponseStyledLabel } from './ResponseLabel.styles.ts'; -import type { ResponseListItem } from '../../../../types/list.interface.ts'; +import type { QuestionnaireResponse } from 'fhir/r4'; interface Props { - color: ResponseListItem['status']; + color: QuestionnaireResponse['status']; children: ReactNode; endIcon?: ReactNode; startIcon?: ReactNode; diff --git a/apps/smart-forms-app/src/features/dashboard/contexts/SelectedQuestionnaireContext.tsx b/apps/smart-forms-app/src/features/dashboard/contexts/SelectedQuestionnaireContext.tsx index e84939787..d6af462d9 100644 --- a/apps/smart-forms-app/src/features/dashboard/contexts/SelectedQuestionnaireContext.tsx +++ b/apps/smart-forms-app/src/features/dashboard/contexts/SelectedQuestionnaireContext.tsx @@ -17,13 +17,12 @@ import type { ReactNode } from 'react'; import { createContext, useState } from 'react'; -import type { QuestionnaireResponse } from 'fhir/r4'; -import type { SelectedQuestionnaire } from '../types/list.interface.ts'; +import type { Questionnaire, QuestionnaireResponse } from 'fhir/r4'; export interface SelectedQuestionnaireContextType { - selectedQuestionnaire: SelectedQuestionnaire | null; + selectedQuestionnaire: Questionnaire | null; existingResponses: QuestionnaireResponse[]; - setSelectedQuestionnaire: (selected: SelectedQuestionnaire | null) => unknown; + setSelectedQuestionnaire: (selected: Questionnaire | null) => unknown; setExistingResponses: (responses: QuestionnaireResponse[]) => unknown; clearSelectedQuestionnaire: () => unknown; } @@ -39,9 +38,7 @@ export const SelectedQuestionnaireContext = createContext( - null - ); + const [selectedQuestionnaire, setSelectedQuestionnaire] = useState(null); const [existingResponses, setExistingResponses] = useState([]); const selectedQuestionnaireContext: SelectedQuestionnaireContextType = { diff --git a/apps/smart-forms-app/src/features/dashboard/hooks/useFetchExistingResponses.ts b/apps/smart-forms-app/src/features/dashboard/hooks/useFetchExistingResponses.ts new file mode 100644 index 000000000..0b2838cfd --- /dev/null +++ b/apps/smart-forms-app/src/features/dashboard/hooks/useFetchExistingResponses.ts @@ -0,0 +1,75 @@ +/* + * Copyright 2023 Commonwealth Scientific and Industrial Research + * Organisation (CSIRO) ABN 41 687 119 230. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { useQuery } from '@tanstack/react-query'; +import type { Bundle, QuestionnaireResponse } from 'fhir/r4'; +import { getClientBundlePromise, getResponsesFromBundle } from '../utils/dashboard.ts'; +import { useContext, useMemo } from 'react'; +import useConfigStore from '../../../stores/useConfigStore.ts'; +import { SelectedQuestionnaireContext } from '../contexts/SelectedQuestionnaireContext.tsx'; + +interface useFetchExistingResponsesReturnParams { + existingResponses: QuestionnaireResponse[]; + fetchError: unknown; + isFetching: boolean; +} + +function useFetchExistingResponses(): useFetchExistingResponsesReturnParams { + const { selectedQuestionnaire } = useContext(SelectedQuestionnaireContext); + const launchQuestionnaire = useConfigStore((state) => state.launchQuestionnaire); + const questionnaire = selectedQuestionnaire ?? launchQuestionnaire; + + const smartClient = useConfigStore((state) => state.smartClient); + const patient = useConfigStore((state) => state.patient); + + // search responses from selected questionnaire + let questionnaireRefParam = ''; + + // Have different questionnaireRef config due to SMART Health IT limitation + if (smartClient) { + const questionnaireRef = smartClient.state.serverUrl.includes( + 'https://launch.smarthealthit.org/v/r4/fhir' + ) + ? `Questionnaire/${questionnaire?.id}-SMARTcopy` + : questionnaire?.url; + + if (questionnaireRef) { + questionnaireRefParam = `questionnaire=${questionnaireRef}&`; + } + } + + const patientIdParam = patient?.id ? `patient=${patient?.id}&` : ''; + const queryUrl = '/QuestionnaireResponse?' + questionnaireRefParam + patientIdParam; + + const { data, isFetching, error } = useQuery( + ['existingResponses', queryUrl], + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + () => getClientBundlePromise(smartClient!, queryUrl), + { + enabled: + !!questionnaire && questionnaireRefParam !== '' && patientIdParam !== '' && !!smartClient + } + ); + + const existingResponses: QuestionnaireResponse[] = useMemo( + () => getResponsesFromBundle(data), + [data] + ); + return { existingResponses, fetchError: error, isFetching }; +} + +export default useFetchExistingResponses; diff --git a/apps/smart-forms-app/src/features/dashboard/hooks/useFetchQuestionnaires.ts b/apps/smart-forms-app/src/features/dashboard/hooks/useFetchQuestionnaires.ts index 762cb55da..4e8d2c6ff 100644 --- a/apps/smart-forms-app/src/features/dashboard/hooks/useFetchQuestionnaires.ts +++ b/apps/smart-forms-app/src/features/dashboard/hooks/useFetchQuestionnaires.ts @@ -16,14 +16,12 @@ */ import { useQuery } from '@tanstack/react-query'; -import type { Bundle } from 'fhir/r4'; -import { getFormsServerBundlePromise, getQuestionnaireListItems } from '../utils/dashboard.ts'; +import type { Bundle, Questionnaire } from 'fhir/r4'; +import { filterQuestionnaires, getFormsServerBundlePromise } from '../utils/dashboard.ts'; import { useMemo } from 'react'; -import type { QuestionnaireListItem } from '../types/list.interface.ts'; interface useFetchQuestionnairesReturnParams { - remoteQuestionnaires: Bundle | undefined; - questionnaireListItems: QuestionnaireListItem[]; + questionnaires: Questionnaire[]; fetchStatus: 'error' | 'success' | 'loading'; fetchError: unknown; isInitialLoading: boolean; @@ -42,7 +40,7 @@ function useFetchQuestionnaires( } const { - data: remoteQuestionnaires, + data: bundle, status, isInitialLoading, error, @@ -52,14 +50,10 @@ function useFetchQuestionnaires( }); // construct questionnaire list items for data display - const questionnaireListItems: QuestionnaireListItem[] = useMemo( - () => getQuestionnaireListItems(remoteQuestionnaires), - [remoteQuestionnaires] - ); + const questionnaires: Questionnaire[] = useMemo(() => filterQuestionnaires(bundle), [bundle]); return { - remoteQuestionnaires, - questionnaireListItems, + questionnaires, fetchStatus: status, fetchError: error, isInitialLoading, diff --git a/apps/smart-forms-app/src/features/dashboard/hooks/useFetchResponses.ts b/apps/smart-forms-app/src/features/dashboard/hooks/useFetchResponses.ts index 6edc009b8..613a48de7 100644 --- a/apps/smart-forms-app/src/features/dashboard/hooks/useFetchResponses.ts +++ b/apps/smart-forms-app/src/features/dashboard/hooks/useFetchResponses.ts @@ -16,20 +16,14 @@ */ import { useQuery } from '@tanstack/react-query'; -import type { Bundle } from 'fhir/r4'; -import { - constructBundle, - getClientBundlePromise, - getResponseListItems -} from '../utils/dashboard.ts'; +import type { Bundle, QuestionnaireResponse } from 'fhir/r4'; +import { constructBundle, filterResponses, getClientBundlePromise } from '../utils/dashboard.ts'; import { useContext, useMemo } from 'react'; -import type { ResponseListItem } from '../types/list.interface.ts'; import useConfigStore from '../../../stores/useConfigStore.ts'; import { SelectedQuestionnaireContext } from '../contexts/SelectedQuestionnaireContext.tsx'; interface useFetchResponsesReturnParams { - responses: Bundle | undefined; - responseListItems: ResponseListItem[]; + responses: QuestionnaireResponse[]; fetchStatus: 'error' | 'success' | 'loading'; fetchError: unknown; isFetching: boolean; @@ -52,7 +46,7 @@ function useFetchResponses( } const { - data: responses, + data: bundle, status, error, isFetching @@ -72,12 +66,12 @@ function useFetchResponses( ); // construct response list items for data display - const responseListItems: ResponseListItem[] = useMemo( - () => getResponseListItems(existingResponses.length === 0 ? responses : existingResponseBundle), - [responses, existingResponseBundle, existingResponses.length] + const responses: QuestionnaireResponse[] = useMemo( + () => filterResponses(existingResponses.length === 0 ? bundle : existingResponseBundle), + [bundle, existingResponseBundle, existingResponses.length] ); - return { responses, responseListItems, fetchStatus: status, fetchError: error, isFetching }; + return { responses, fetchStatus: status, fetchError: error, isFetching }; } export default useFetchResponses; diff --git a/apps/smart-forms-app/src/features/save/api/saveQr.ts b/apps/smart-forms-app/src/features/save/api/saveQr.ts index de58a7d91..cd97db450 100644 --- a/apps/smart-forms-app/src/features/save/api/saveQr.ts +++ b/apps/smart-forms-app/src/features/save/api/saveQr.ts @@ -117,7 +117,7 @@ function addQuestionnaireReference( endpointUrl: string ): QuestionnaireResponse { let questionnaireReference: string; - if (endpointUrl.includes('/v/r4/fhir')) { + if (endpointUrl.includes('https://launch.smarthealthit.org/v/r4/fhir')) { // Plugging questionnaire.id in because SMART Health IT has these weird requirements for canonicals questionnaireReference = questionnaire.id ? `Questionnaire/${questionnaire.id}` : ''; } else { diff --git a/apps/smart-forms-app/src/features/smartAppLaunch/components/Authorisation.tsx b/apps/smart-forms-app/src/features/smartAppLaunch/components/Authorisation.tsx index 30687dfcf..4800462eb 100644 --- a/apps/smart-forms-app/src/features/smartAppLaunch/components/Authorisation.tsx +++ b/apps/smart-forms-app/src/features/smartAppLaunch/components/Authorisation.tsx @@ -75,6 +75,7 @@ function Authorisation() { const setPatient = useConfigStore((state) => state.setPatient); const setUser = useConfigStore((state) => state.setUser); const setEncounter = useConfigStore((state) => state.setEncounter); + const setLaunchQuestionnaire = useConfigStore((state) => state.setLaunchQuestionnaire); const buildSourceQuestionnaire = useQuestionnaireStore((state) => state.buildSourceQuestionnaire); @@ -145,12 +146,15 @@ function Authorisation() { assembleIfRequired(questionnaire).then(async (questionnaire) => { if (questionnaire) { // Post questionnaire to client if it is SMART Health IT - if (client.state.serverUrl.includes('/v/r4/fhir')) { + if ( + client.state.serverUrl.includes('https://launch.smarthealthit.org/v/r4/fhir') + ) { questionnaire.id = questionnaire.id + '-SMARTcopy'; postQuestionnaireToSMARTHealthIT(client, questionnaire); } await buildSourceQuestionnaire(questionnaire); + setLaunchQuestionnaire(questionnaire); dispatch({ type: 'UPDATE_HAS_QUESTIONNAIRE', payload: true }); } else { enqueueSnackbar( diff --git a/apps/smart-forms-app/src/stores/useConfigStore.ts b/apps/smart-forms-app/src/stores/useConfigStore.ts index c50c7e00e..16fd0a414 100644 --- a/apps/smart-forms-app/src/stores/useConfigStore.ts +++ b/apps/smart-forms-app/src/stores/useConfigStore.ts @@ -1,5 +1,5 @@ import { create } from 'zustand'; -import type { Encounter, Patient, Practitioner } from 'fhir/r4'; +import type { Encounter, Patient, Practitioner, Questionnaire } from 'fhir/r4'; import type Client from 'fhirclient/lib/Client'; export interface ConfigState { @@ -8,12 +8,14 @@ export interface ConfigState { user: Practitioner | null; encounter: Encounter | null; launchIntent: string | null; + launchQuestionnaire: Questionnaire | null; debugMode: boolean; setSmartClient: (client: Client) => void; setPatient: (patient: Patient) => void; setUser: (user: Practitioner) => void; setEncounter: (encounter: Encounter) => void; setLaunchIntent: (launchIntent: string | null) => void; + setLaunchQuestionnaire: (launchQuestionnaire: Questionnaire | null) => void; activateDebugMode: () => void; } @@ -23,12 +25,15 @@ const useConfigStore = create()((set) => ({ user: null, encounter: null, launchIntent: null, + launchQuestionnaire: null, debugMode: false, setSmartClient: (client: Client) => set(() => ({ smartClient: client })), setPatient: (patient: Patient) => set(() => ({ patient: patient })), setUser: (user: Practitioner) => set(() => ({ user: user })), setEncounter: (encounter: Encounter) => set(() => ({ encounter: encounter })), setLaunchIntent: (launchIntent: string | null) => set(() => ({ launchIntent: launchIntent })), + setLaunchQuestionnaire: (launchQuestionnaire: Questionnaire | null) => + set(() => ({ launchQuestionnaire: launchQuestionnaire })), activateDebugMode: () => set(() => ({ debugMode: true })) }));