felicity-lims/webapp/stores/patient.ts

83 lines
2.6 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 } from '../graphql/patient.queries';
import { addListsUnique } from '../utils';
import { 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 {
patients: [],
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 {
patients: IPatient[];
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,
getPatientByUid: (state) => (uid: number) => state.patients?.find(p => p.uid === uid),
getPatient: (state) => state.patient,
getPatientCount: (state) => state.patientCount,
getPatientPageInfo: (state) => state.patientPageInfo,
},
actions: {
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 searchPatients(queryString: string){
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)
}
}
})