Merge branch 'feat/api-function' into develop
This commit is contained in:
commit
1c629ce18d
3 changed files with 248 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;
|
||||
147
src/stores/branch/index.ts
Normal file
147
src/stores/branch/index.ts
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
import { ref } from 'vue';
|
||||
import { defineStore } from 'pinia';
|
||||
import { District, Province, SubDistrict } from '../address';
|
||||
import { Pagination, Status } from '../types';
|
||||
import { api } from 'src/boot/axios';
|
||||
|
||||
export type Branch = {
|
||||
subDistrict: SubDistrict | null;
|
||||
district: District | null;
|
||||
province: Province | null;
|
||||
updatedAt: string;
|
||||
updateBy: string;
|
||||
createdAt: string;
|
||||
createdBy: string;
|
||||
status: Status;
|
||||
headOfficeId: string | null;
|
||||
isHeadOffice: boolean;
|
||||
longitude: string;
|
||||
latitude: string;
|
||||
telephoneNo: string;
|
||||
email: string;
|
||||
zipCode: string;
|
||||
subDistrictId: string;
|
||||
districtId: string;
|
||||
provinceId: string;
|
||||
addressEN: string;
|
||||
addressTH: string;
|
||||
nameEN: string;
|
||||
nameTH: string;
|
||||
taxNo: string;
|
||||
code: string;
|
||||
id: string;
|
||||
};
|
||||
|
||||
const useBranchStore = defineStore('api-branch', () => {
|
||||
const data = ref<Pagination<Branch[]>>();
|
||||
|
||||
async function fetchBranch(opts?: {
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
zipCode?: string;
|
||||
query?: string;
|
||||
}) {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
if (opts?.pageSize && opts?.pageSize > 0) {
|
||||
params.append('pageSize', `${opts.pageSize}`);
|
||||
}
|
||||
if (opts?.page && opts.page > 0) params.append('page', `${opts.page}`);
|
||||
if (opts?.zipCode) params.append('zipCode', opts.zipCode);
|
||||
if (opts?.query) params.append('query', opts.query);
|
||||
|
||||
const query = params.toString();
|
||||
|
||||
const res = await api.get<Pagination<Branch[]>>(
|
||||
`/branch${(params && '?'.concat(query)) || ''}`,
|
||||
);
|
||||
|
||||
if (res && res.status === 200) {
|
||||
data.value = res.data;
|
||||
return data.value;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async function fetchBranchById(id: string) {
|
||||
const res = await api.get<Branch>(`/branch/${id}`);
|
||||
|
||||
if (!res) return false;
|
||||
if (res.status === 200) return res.data;
|
||||
if (res.status === 204) return null;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
async function createBranch(branch: {
|
||||
code: string;
|
||||
taxNo: string;
|
||||
nameEN: string;
|
||||
nameTH: string;
|
||||
addressEN: string;
|
||||
addressTH: string;
|
||||
zipCode: string;
|
||||
email: string;
|
||||
telephoneNo: string;
|
||||
longitude: string;
|
||||
latitude: string;
|
||||
subDistrictId?: string | null;
|
||||
districtId?: string | null;
|
||||
provinceId?: string | null;
|
||||
headOfficeId?: string | null;
|
||||
}) {
|
||||
const res = await api.post<Branch>('/branch', branch);
|
||||
|
||||
if (!res) return false;
|
||||
|
||||
return res.data;
|
||||
}
|
||||
|
||||
async function editBranchById(
|
||||
id: string,
|
||||
branch: {
|
||||
code?: string;
|
||||
taxNo?: string;
|
||||
nameEN?: string;
|
||||
nameTH?: string;
|
||||
addressEN?: string;
|
||||
addressTH?: string;
|
||||
zipCode?: string;
|
||||
email?: string;
|
||||
telephoneNo?: string;
|
||||
longitude?: string;
|
||||
latitude?: string;
|
||||
subDistrictId?: string | null;
|
||||
districtId?: string | null;
|
||||
provinceId?: string | null;
|
||||
headOfficeId?: string | null;
|
||||
},
|
||||
) {
|
||||
const res = await api.patch<Branch>(`/branch/${id}`, branch);
|
||||
|
||||
if (!res) return false;
|
||||
|
||||
return res.data;
|
||||
}
|
||||
|
||||
async function deleteBranchById(id: string) {
|
||||
const res = await api.delete<Branch>(`/branch/${id}`);
|
||||
|
||||
if (!res) return false;
|
||||
if (res.status === 200) return res.data;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return {
|
||||
data,
|
||||
fetchBranch,
|
||||
fetchBranchById,
|
||||
|
||||
createBranch,
|
||||
editBranchById,
|
||||
deleteBranchById,
|
||||
};
|
||||
});
|
||||
|
||||
export default useBranchStore;
|
||||
11
src/stores/types.ts
Normal file
11
src/stores/types.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
export type Pagination<T> = {
|
||||
result: T;
|
||||
page: number;
|
||||
pageSize: number;
|
||||
total: number;
|
||||
};
|
||||
|
||||
export enum Status {
|
||||
CREATED,
|
||||
USED,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue