felicity-lims/webapp/stores/patient.ts

120 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-04-01 04:29:09 +08:00
import { defineStore } from 'pinia'
import { GET_ALL_PATIENTS, SEARCH_PATIENTS, GET_PATIENT_BY_UID, IDENTIFICATION_TYPES } from '../graphql/patient.queries';
2022-04-01 04:29:09 +08:00
import { addListsUnique } from '../utils';
import { IIdentification, IPatient } from '../models/patient'
2023-02-24 08:44:14 +08:00
import {
IPageInfo
} from "../models/pagination"
2022-04-01 04:29:09 +08:00
2022-04-04 02:54:31 +08:00
import { useApiUtil } from '../composables'
2022-04-01 04:29:09 +08:00
2022-04-04 02:54:31 +08:00
const { withClientQuery } = useApiUtil()
2022-04-01 04:29:09 +08:00
2022-04-04 02:54:31 +08:00
export const usePatientStore = defineStore('patient', {
2022-04-01 04:29:09 +08:00
state: () => {
return {
identifications: [],
2022-04-01 04:29:09 +08:00
patients: [],
searchQuery: "",
2022-04-15 16:32:24 +08:00
fetchingPatients: false,
2022-04-01 04:29:09 +08:00
patient: undefined,
2022-04-15 16:32:24 +08:00
fetchingPatient: false,
2022-04-01 04:29:09 +08:00
patientCount: 0,
patientPageInfo: undefined,
} as {
identifications: IIdentification[];
2022-04-01 04:29:09 +08:00
patients: IPatient[];
searchQuery: string,
2022-04-15 16:32:24 +08:00
fetchingPatients: boolean;
2022-04-01 04:29:09 +08:00
patient?: IPatient;
2022-04-15 16:32:24 +08:00
fetchingPatient: boolean;
2022-04-01 04:29:09 +08:00
patientCount?: number;
2023-02-24 08:44:14 +08:00
patientPageInfo?: IPageInfo
2022-04-01 04:29:09 +08:00
}
},
getters: {
getPatients: (state) => state.patients,
getIdentifications: (state) => state.identifications,
getSearchQuery: (state) => state.searchQuery,
getPatientByUid: (state) => (uid: string) => state.patients?.find(p => p.uid === uid),
2022-04-01 04:29:09 +08:00
getPatient: (state) => state.patient,
getPatientCount: (state) => state.patientCount,
getPatientPageInfo: (state) => state.patientPageInfo,
},
actions: {
// identifications
async fetchIdentifications(){
await withClientQuery(IDENTIFICATION_TYPES, {}, "identificationAll")
.then(payload => {
this.identifications = payload;
});
},
addIdentification(payload){
this.identifications?.unshift(payload);
},
updateIdentification(payload) {
const index = this.identifications!.findIndex(pt => pt.uid === payload.uid)
this.identifications[index] = { ...this.identifications![index], ...payload }
},
2022-04-01 04:29:09 +08:00
// patiets
2022-04-01 04:29:09 +08:00
async fetchPatients(params){
2022-04-15 16:32:24 +08:00
this.fetchingPatients = true
2022-04-04 02:54:31 +08:00
await withClientQuery(GET_ALL_PATIENTS, { ...params, sortBy: ["-uid"] }, undefined)
2022-04-01 04:29:09 +08:00
.then(payload => {
2022-04-15 16:32:24 +08:00
this.fetchingPatients = false
const page = payload.patientAll
const patients = page.items;
2022-04-01 04:29:09 +08:00
if(params.filterAction){
this.patients = [];
this.patients = patients;
} else {
this.patients = addListsUnique(this.patients, patients, "uid");
}
this.patientCount = page?.totalCount;
this.patientPageInfo = page?.pageInfo;
2022-04-15 16:32:24 +08:00
}).catch(err => this.fetchingPatients = false);
2022-04-01 04:29:09 +08:00
},
addPatient(payload){
2022-04-09 22:57:06 +08:00
this.patients?.unshift(payload);
2022-04-01 04:29:09 +08:00
},
updatePatient(payload) {
const index = this.patients!.findIndex(pt => pt.uid === payload.uid)
this.patients[index] = { ...this.patients![index], ...payload }
this.patient = payload;
},
async fetchPtientByUid(uid){
2022-04-15 16:32:24 +08:00
if(!uid){ return }
this.fetchingPatient = true;
2022-04-04 02:54:31 +08:00
await withClientQuery(GET_PATIENT_BY_UID, { uid }, "patientByUid")
2022-04-15 16:32:24 +08:00
.then(payload => {
this.fetchingPatient = false;
this.patient = payload
}).catch(err => this.fetchingPatient = false)
2022-04-01 04:29:09 +08:00
},
async setPatient(payload: IPatient) {
if(!payload){ return }
this.fetchingPatient = true;
this.patient = payload
this.fetchingPatient = false;
},
async resetPatient() {
this.patient = undefined;
},
2022-04-01 04:29:09 +08:00
async searchPatients(queryString: string){
this.searchQuery = queryString;
2022-04-04 02:54:31 +08:00
await withClientQuery(SEARCH_PATIENTS, { queryString }, "patientSearch")
2022-04-01 04:29:09 +08:00
.then(payload => this.patients = payload)
},
clearSearchQuery(){
this.searchQuery = "";
2022-04-01 04:29:09 +08:00
}
}
})