Skip to content

Commit

Permalink
fixed reactive state for listDistricts
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunlumbcgov committed Jul 18, 2023
1 parent f09b2b0 commit 8c017c4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 37 deletions.
38 changes: 4 additions & 34 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,49 +1,19 @@
<script>
import { RouterLink, RouterView } from 'vue-router'
import { ref, onMounted } from 'vue'
import InstituteService from './services/InstituteService'
import { ref, onBeforeMount, onMounted } from 'vue'
import { useAppStore } from './stores/app'
export default {
setup() {
const data = ref([]) // Placeholder for the received data
const appStore = useAppStore()
const getDistricts = () => {
InstituteService.getDistricts()
.then((response) => {
// Handle the response data
appStore.setDistricts(response.data)
})
.catch((error) => {
// Handle the error
console.error(error)
})
}
const getSchools = () => {
InstituteService.getSchools()
.then((response) => {
console.log('getting schools')
// Handle the response data
appStore.setSchools(response.data)
})
.catch((error) => {
// Handle the error
console.error(error)
})
}
getDistricts()
// Call the getDistricts function when the component is mounted
onMounted(() => {
//getDistricts()
//getSchools()
onBeforeMount(async () => {
await appStore.setDistricts()
})
return {
data,
getDistricts,
getSchools
data
}
},
mounted() {
Expand Down
19 changes: 17 additions & 2 deletions frontend/src/stores/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,31 @@ interface School {

}

import InstituteService from '../services/InstituteService'

export const useAppStore = defineStore('app', {
state: () => ({
districts: [] as District[],
schools: [] as School[]
}),
actions: {
setDistricts(districts: District[]): void {
this.districts = districts;
setDistricts(): void {

InstituteService.getDistricts()
.then((response) => {
// Handle the response data
this.districts = response.data
})
.catch((error) => {
// Handle the error
console.error(error)
})



},
setSchools(schools: School[]): void {

this.schools = schools;
}
},
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/views/ListDistricts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
</template>
<script>
import { useAppStore } from '@/stores/app'
import { toRefs, watchEffect } from 'vue';
export default {
setup() {
const appStore = useAppStore()
const districts = appStore.getDistricts
const { districts } = toRefs(appStore);
return { districts }
}
}
Expand Down

0 comments on commit 8c017c4

Please sign in to comment.