feat: fetch address from api
This commit is contained in:
parent
e073a993f1
commit
d335edd3ca
1 changed files with 90 additions and 0 deletions
90
src/stores/address/index.ts
Normal file
90
src/stores/address/index.ts
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
import { ref } from 'vue';
|
||||
import { defineStore } from 'pinia';
|
||||
import { api } from 'src/boot/axios';
|
||||
|
||||
export interface SubDistrict {
|
||||
updatedAt: string;
|
||||
updateBy: string;
|
||||
createdAt: string;
|
||||
createdBy: string;
|
||||
districtId: string;
|
||||
zipCode: string;
|
||||
nameEN: string;
|
||||
nameTH: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface District {
|
||||
updatedAt: string;
|
||||
updateBy: string;
|
||||
createdAt: string;
|
||||
createdBy: string;
|
||||
provinceId: string;
|
||||
nameEN: string;
|
||||
nameTH: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface Province {
|
||||
updatedAt: string;
|
||||
updateBy: string;
|
||||
createdAt: string;
|
||||
createdBy: string;
|
||||
nameEN: string;
|
||||
nameTH: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
const useAddressStore = defineStore('api-address', () => {
|
||||
const province = ref<Province[]>();
|
||||
const district = ref<Record<Province['id'], District[]>>({});
|
||||
const subDistrict = ref<Record<District['id'], SubDistrict[]>>({});
|
||||
|
||||
async function fetchProvince() {
|
||||
if (province.value) return province.value;
|
||||
|
||||
const res = await api.get<Province[]>(`/address/province`);
|
||||
|
||||
if (!res) return false;
|
||||
|
||||
province.value = res.data;
|
||||
|
||||
return province.value;
|
||||
}
|
||||
|
||||
async function fetchDistrictByProvinceId(provinceId: string) {
|
||||
if (district.value[provinceId]) return district.value[provinceId];
|
||||
|
||||
const res = await api.get<District[]>(
|
||||
`/address/province/${provinceId}/district`,
|
||||
);
|
||||
|
||||
if (!res) return false;
|
||||
|
||||
district.value[provinceId] = res.data;
|
||||
|
||||
return district.value[provinceId];
|
||||
}
|
||||
|
||||
async function fetchSubDistrictByProvinceId(districtId: string) {
|
||||
if (subDistrict.value[districtId]) return subDistrict.value[districtId];
|
||||
|
||||
const res = await api.get<SubDistrict[]>(
|
||||
`/address/district/${districtId}/sub-district`,
|
||||
);
|
||||
|
||||
if (!res) return false;
|
||||
|
||||
subDistrict.value[districtId] = res.data;
|
||||
|
||||
return subDistrict.value[districtId];
|
||||
}
|
||||
|
||||
return {
|
||||
fetchProvince,
|
||||
fetchDistrictByProvinceId,
|
||||
fetchSubDistrictByProvinceId,
|
||||
};
|
||||
});
|
||||
|
||||
export default useAddressStore;
|
||||
Loading…
Add table
Add a link
Reference in a new issue