From 7f623e2e8565f4e840b2706add6f22767bf7f510 Mon Sep 17 00:00:00 2001 From: pius Date: Tue, 1 Oct 2024 13:55:27 +0300 Subject: [PATCH] feat: submit facility details --- .../facilities/hooks/useRegistration.js | 137 ++++-------------- .../facilities/views/RegisterFacility.vue | 68 +++++---- src/shared/utils/csvOperations.js | 15 ++ src/shared/utils/fetchMOHFacilites.js | 2 - vite.config.js | 3 +- 5 files changed, 83 insertions(+), 142 deletions(-) create mode 100644 src/shared/utils/csvOperations.js diff --git a/src/modules/facilities/hooks/useRegistration.js b/src/modules/facilities/hooks/useRegistration.js index fddd004..dde16a9 100644 --- a/src/modules/facilities/hooks/useRegistration.js +++ b/src/modules/facilities/hooks/useRegistration.js @@ -2,37 +2,25 @@ import {ref} from "vue"; import {useRoute, useRouter} from "vue-router"; import {useToast} from "maz-ui"; import {useAxios} from "../../../shared/hooks/useAxios.js"; -import {useLocationStore} from "../../../shared/store/locationStore.js"; -import axios from "axios"; -import {fetchMOHFacilities} from "../../../shared/utils/fetchMOHFacilites.js"; +import facilitiesCSV from "../../../shared/assets/facilities.csv" +import {csvToArrayOfObjects} from "../../../shared/utils/csvOperations.js"; export const useRegistration = () => { const isOpen = ref(false) const loading = ref(false) const name = ref("") - const country = ref("") const level = ref("") - const region = ref("") const district = ref("") - const code = ref("") - const districtOptions = ref([]) - const regionOptions = ref([]) - const countryOptions = ref([]) - - const levelOptions = ref([ - "Hospital", - "Health Centre_IV", - "Health Centre_III", - "Health Centre_II", - "Health Post", - "Comprehensive Health Post", - "Surveillance office", - "Primary Clinic", - "Drug Vendor" - ]) - - const locationStore = useLocationStore() + const listOfFacilities = ref([]) + const searchString = ref("") + const selectedFacility = ref({ + name: "", + code: "", + level: "", + county: "" + }); + const route = useRoute() @@ -44,70 +32,6 @@ export const useRegistration = () => { const toast = useToast() - const getCountries = async () => { - try { - loading.value = true; - const response = await makeFHIRRequest({url: `Location?type=COUNTRY`}) - if (!response?.entry) - return - countryOptions.value = response.entry.map(entry => entry.resource.name) - } catch (error) { - toast.error('Error getting countries') - } finally { - loading.value = false; - } - } - - const getRegions = async (country) => { - try { - loading.value = true; - const response = await makeFHIRRequest({url: `Location?type=REGION&partof=${country}`}) - if (!response?.entry) - return - regionOptions.value = response.entry.map(entry => entry.resource.name) - } catch (error) { - toast.error('Error getting regions') - } finally { - loading.value = false; - } - } - - const getDistricts = async (region) => { - try { - loading.value = true; - const response = await makeFHIRRequest({url: `Location?type=DISTRICT&partof=${region}`}); - if (!response?.entry) - return - districtOptions.value = response.entry.map(entry => entry.resource.name) - } catch (e) { - toast.error('Error getting districts') - } finally { - loading.value = false; - } - } - - const populateFields = async () => { - try { - loading.value = true; - - const response = await makeFHIRRequest({ - url: `/Location/${resourceID}` - }) - - await getRegions(locationStore.getParentLocation(response.partOf.reference.split("/")[1])) - - await getCountries(locationStore.getParentLocation(locationStore.getParentLocation(response.partOf.reference.split("/")[1]))) - - name.value = response.name - district.value = response.partOf.reference.split("/")[1] - region.value = locationStore.getParentLocation(response.partOf.reference.split("/")[1]) - country.value = locationStore.getParentLocation(locationStore.getParentLocation(response.partOf.reference.split("/")[1])) - } catch (error) { - toast.error('Error populating fields') - } finally { - loading.value = false; - } - } const submit = async (evt) => { evt.preventDefault() @@ -118,11 +42,11 @@ export const useRegistration = () => { url: resourceID ? `Location/${resourceID}` : "Location", data: { resourceType: "Location", - id: resourceID ? resourceID : name.value, - name: name.value, - level: level.value, + id: resourceID ? resourceID : selectedFacility.value.name, + name: selectedFacility.value.name, + level: selectedFacility.value.level, partOf: { - reference: `Location/${district.value}` + reference: `Location/${selectedFacility.value.county}` }, search: { mode: "match" @@ -158,27 +82,26 @@ export const useRegistration = () => { router.push('/facility/registered-facilities') } - fetchMOHFacilities({}) + const getListOfFacilities = () => { + fetch(facilitiesCSV) + .then(res => res.text()) + .then(data => { + const arrayOfFacilities = csvToArrayOfObjects(data) + listOfFacilities.value = arrayOfFacilities; + return arrayOfFacilities + }) + .catch(err => err) + } + return { isOpen, loading, - name, - country, - level, - region, - district, - code, - regionOptions, - countryOptions, - getRegions, - getCountries, submit, close, - resourceID, - levelOptions, - populateFields, - getDistricts, - districtOptions + getListOfFacilities, + listOfFacilities, + searchString, + selectedFacility } } \ No newline at end of file diff --git a/src/modules/facilities/views/RegisterFacility.vue b/src/modules/facilities/views/RegisterFacility.vue index 3f5bd0e..22cda2b 100644 --- a/src/modules/facilities/views/RegisterFacility.vue +++ b/src/modules/facilities/views/RegisterFacility.vue @@ -10,12 +10,32 @@ autocomplete="off" @submit="submit" class="grid grid-cols-1 md:grid-cols-2 px-4 pb-24 lg:px-11 pt-11 gap-8 lg:gap-x-[100px] gap-y-10"> - - - - - - + + +
+ + + + + + + + + + + + +
@@ -24,6 +44,7 @@ Submit
+ @@ -44,7 +65,7 @@ import MazIcon from 'maz-ui/components/MazIcon' import MazDialog from 'maz-ui/components/MazDialog' import MazSpinner from 'maz-ui/components/MazSpinner' import MazBtn from 'maz-ui/components/MazBtn' -import {onMounted, watch,} from "vue"; +import {computed, onMounted, watch,} from "vue"; import {useRegistration} from "../hooks/useRegistration.js"; import {useRouter} from "vue-router"; @@ -54,39 +75,22 @@ const router = useRouter() const { isOpen, loading, - name, - country, - region, submit, close, - resourceID, - getRegions, - getCountries, - countryOptions, - regionOptions, - levelOptions, - level, - code, - populateFields, - district, - getDistricts, - districtOptions, + getListOfFacilities, + listOfFacilities, + searchString, + selectedFacility } = useRegistration() -onMounted(async() => { - getCountries() - if(resourceID) - populateFields() +onMounted(() => { + getListOfFacilities() }) -watch(country, value => { - getRegions(value) -}) - -watch(region, value => { - getDistricts(value) +watch(searchString, value => { + selectedFacility.value = listOfFacilities.value.find(facility => facility.code === value) }) diff --git a/src/shared/utils/csvOperations.js b/src/shared/utils/csvOperations.js new file mode 100644 index 0000000..743ac3d --- /dev/null +++ b/src/shared/utils/csvOperations.js @@ -0,0 +1,15 @@ +export const csvToArrayOfObjects = (csvString) => { + const lines = csvString.split('\n'); + + const headers = lines[0].split(','); + + const result = lines.slice(1).map(line =>{ + const values = line.split(","); + const obj = {}; + headers.forEach((header, index) => { + obj[header.trim()] = values[index].trim(); + }) + return obj; + }) + return result; +} \ No newline at end of file diff --git a/src/shared/utils/fetchMOHFacilites.js b/src/shared/utils/fetchMOHFacilites.js index 14519f2..c462529 100644 --- a/src/shared/utils/fetchMOHFacilites.js +++ b/src/shared/utils/fetchMOHFacilites.js @@ -59,7 +59,5 @@ const iterateFacilities = async() =>{ } - - iterateFacilities() diff --git a/vite.config.js b/vite.config.js index 81cbf85..00b7521 100644 --- a/vite.config.js +++ b/vite.config.js @@ -20,5 +20,6 @@ export default defineConfig({ host: true, // needed for the Docker Container port mapping to work strictPort: true, port: 5173 // you can replace this port with any port - } + }, + assetsInclude: ['**/*.csv'] })