Skip to content

Commit

Permalink
Merge pull request #5 from IntelliSOFT-Consulting/chore/locations-rou…
Browse files Browse the repository at this point in the history
…te-update

chore: changed the route for facilities and updated the interface to …
  • Loading branch information
PiusKariuki authored Dec 11, 2024
2 parents a5ea4ae + 6678a6b commit fc2aeb9
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 62 deletions.
30 changes: 30 additions & 0 deletions src/modules/facilities/data/facilityTableHeaders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const facilityTableHeaders = [
{
text: "HEALTH FACILITY NAME",
value: "resource.name",
},
// {
// text: "LEVEL",
// value: "",
// },
{
text: "FACILITY CODE",
value: "resource.id",
},
// {
// text: "COUNTY OF ORIGIN",
// value: "country",
// },
// {
// text: "REGION",
// value: "region",
// },
{
text: "WARD",
value: "district",
},
{
text: "ACTION",
value: "id",
},
]
204 changes: 163 additions & 41 deletions src/modules/facilities/hooks/useAllFacilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import {useToast} from "maz-ui";
import {useLocationStore} from "../../../shared/store/locationStore.js";

export const useAllFacilities = () => {
const locations = ref([])
const locations = ref([]);
const countries = ref([]);
const counties = ref([]);
const subCounties = ref([]);
const wards = ref([]);
const selectedCountryID = ref("");
const selectedCountyID = ref("");
const selectedSubCountyID = ref("");
const selectedWardID = ref("");

const data = ref([])

Expand All @@ -22,58 +30,161 @@ export const useAllFacilities = () => {

const toast = useToast()

const headers = [
{
text: "HEALTH FACILITY NAME",
value: "resource.name",
},
{
text: "LEVEL",
value: "",
},
{
text: "FACILITY CODE",
value: "",
},
{
text: "COUNTY OF ORIGIN",
value: "country",
},
{
text: "REGION",
value: "region",
},
{
text: "DISTRICT",
value: "district",
},
{
text: "ACTION",
value: "id",
},
]

const add = () => router.push("/facility/register")

const getCountries = async () => {
try {
loading.value = true;
const response = await makeFHIRRequest({
url: "Location?type=COUNTRY"
});

if (response.entry) {
/**
* flatten the entries
*/
countries.value = response.entry.map(entry => ({
...entry,
value: entry.resource.id,
label: entry.resource.name
}));
/**
* Clear all address hierarchies down the list
*/
counties.value = [];
subCounties.value = [];
wards.value = [];
} else {
countries.value = []
}
} catch (e) {
toast.error(`Error getting countries`)
}
}

const getAllFacilities = async ({filter = ""}) => {
const getCounties = async ({countryID}) => {
try {
loading.value = true;
const response = await makeFHIRRequest({
url: `Location?partof=${countryID}&type=COUNTY&_count=5000`
});

await locationStore.fetchLocations()
if (response.entry) {
/**
* flatten the entries
*/
counties.value = response.entry.map(entry => ({
...entry,
value: entry.resource.id,
label: entry.resource.name
}))
/**
* Clear all address hierarchies down the list
*/
subCounties.value = [];
selectedSubCountyID.value=""
wards.value = [];
selectedWardID.value="";
data.value=[];
} else {
counties.value = []
}
} catch (e) {
toast.error(`Error getting counties`)
}finally {
loading.value = false;
}
}

const getSubCounties = async ({countyID}) => {
try {
loading.value = true;
const response = await makeFHIRRequest({
url: `/Location?type=FACILITY&${filter}&_count=5000`,
})
url: `Location?partof=${countyID}&type=SUB-COUNTY&_count=5000`
});

if (response.entry) {
/**
* flatten the entries
*/
subCounties.value = response.entry.map(entry => ({
...entry,
value: entry.resource.id,
label: entry.resource.name
}))
/**
* Clear all address hierarchies down the list
*/
wards.value = [];
data.value=[];
} else {
subCounties.value = []
}
} catch (e) {
toast.error(`Error getting counties`)
}finally {
loading.value = false;
}
}

const getWards = async ({subCountyID}) => {
try {
loading.value = true;
const response = await makeFHIRRequest({
url: `Location?partof=${subCountyID}&type=WARD&_count=5000`
});

if (response.entry) {
/**
* flatten the entries
*/
wards.value = response.entry.map(entry => ({
...entry,
value: entry.resource.id,
label: entry.resource.name
}))
} else {
wards.value = []
}
} catch (e) {
toast.error(`Error getting counties`)
}finally {
loading.value = false;
}
}

const getAllFacilities = async ({filter = "", wardID = ""}) => {
try {
loading.value = true;

await locationStore.fetchLocations();

let path = "/Location";
let queryParams = [];

queryParams.push("type=FACILITY", "_count=1000");

if (filter) {
queryParams.push(filter);
}

if (wardID) {
queryParams.push(`partof=${wardID}`);
}

path += `?${queryParams.join("&")}`;

const response = await makeFHIRRequest({
url: path
})
if (response.entry) {
data.value = []
for (const entry of response.entry) {
data.value = [...data.value, {
...entry,
district: entry.resource.partOf?.reference?.split('/')[1],
region: locationStore.getParentLocation(entry.resource.partOf?.reference?.split('/')[1]),
country: locationStore.getParentLocation(locationStore.getParentLocation(entry.resource.partOf?.reference?.split('/')[1])),
district: entry?.resource?.partOf?.reference?.split('/')[1],
region: locationStore.getParentLocation(entry?.resource?.partOf?.reference?.split('/')[1]),
country: locationStore.getParentLocation(locationStore.getParentLocation(entry?.resource?.partOf?.reference?.split('/')[1])),
}]
}
} else data.value = []
Expand All @@ -87,7 +198,7 @@ export const useAllFacilities = () => {

const handleSearch = async (searchKey) => {
searchString.value = searchKey
if (searchKey.length > 0)
if (searchKey.length > 2)
await getAllFacilities({filter: `name=${searchKey}`})
else if (searchKey === "")
await getAllFacilities({})
Expand All @@ -99,9 +210,20 @@ export const useAllFacilities = () => {
getAllFacilities,
data,
searchString,
headers,
add,
loading,
locations,
getCountries,
countries,
selectedCountryID,
getCounties,
counties,
selectedCountyID,
getSubCounties,
subCounties,
selectedSubCountyID,
getWards,
wards,
selectedWardID,
}
}
15 changes: 8 additions & 7 deletions src/modules/facilities/hooks/useFacilityDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export const useFacilityDetails = () => {

const state = reactive({
"name": "",
"country": "",
"level": "",
"region": "",
// "country": "",
// "level": "",
// "region": "",
"code": "",
"district": "",
"ward": "",
})


Expand All @@ -30,9 +30,10 @@ export const useFacilityDetails = () => {
url: `/Location/${resourceID}`
})
state['name'] = response?.name
state['district'] = response?.partOf?.reference?.split('/')[1]
state['region'] = locationStore.getParentLocation(response?.partOf?.reference?.split('/')[1])
state['country'] = locationStore.getParentLocation(locationStore.getParentLocation(response?.partOf?.reference?.split('/')[1]))
state['ward'] = response?.partOf?.reference?.split('/')[1]
state['code'] = response?.id
// state['region'] = locationStore.getParentLocation(response?.partOf?.reference?.split('/')[1])
// state['country'] = locationStore.getParentLocation(locationStore.getParentLocation(response?.partOf?.reference?.split('/')[1]))
facility.value = response
} catch (e) {
toast.error('Error getting facility', e)
Expand Down
Loading

0 comments on commit fc2aeb9

Please sign in to comment.