-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up code dependencies for dashboard tables
- Loading branch information
Showing
13 changed files
with
135 additions
and
100 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
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
75 changes: 75 additions & 0 deletions
75
apps/smart-forms-app/src/features/dashboard/hooks/useFetchExistingResponses.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,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<Bundle>( | ||
['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; |
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.