refactor: conditional type based on args
This commit is contained in:
parent
65df8984d6
commit
b69146ff8a
2 changed files with 38 additions and 17 deletions
|
|
@ -1,14 +1,29 @@
|
|||
import { ref } from 'vue';
|
||||
import { ref, watch } from 'vue';
|
||||
import { defineStore } from 'pinia';
|
||||
import { Pagination } from '../types';
|
||||
import { api } from 'src/boot/axios';
|
||||
import { Branch, BranchCreate } from './types';
|
||||
import { BranchContact } from '../branch-contact/types';
|
||||
|
||||
type BranchId = string;
|
||||
|
||||
const useBranchStore = defineStore('api-branch', () => {
|
||||
const data = ref<Pagination<Branch[]>>();
|
||||
const data = ref<Pagination<Branch[]>>({
|
||||
result: [],
|
||||
page: 0,
|
||||
pageSize: 0,
|
||||
total: 0,
|
||||
});
|
||||
const map = ref<Record<BranchId, Branch & { contact?: BranchContact[] }>>({});
|
||||
|
||||
async function fetchList<T extends Branch>(
|
||||
opts?: {
|
||||
watch(data, () => {
|
||||
data.value.result.forEach((v) => {
|
||||
map.value[v.id] = v;
|
||||
});
|
||||
});
|
||||
|
||||
async function fetchList<
|
||||
Options extends {
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
zipCode?: string;
|
||||
|
|
@ -16,12 +31,15 @@ const useBranchStore = defineStore('api-branch', () => {
|
|||
tree?: boolean;
|
||||
filter?: 'head' | 'sub';
|
||||
},
|
||||
Data extends Pagination<Branch[]>,
|
||||
>(
|
||||
opts?: Options,
|
||||
flow?: {
|
||||
sessionId: string;
|
||||
refTransactionId: string;
|
||||
transactionId: string;
|
||||
},
|
||||
) {
|
||||
): Promise<Data | false> {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
if (opts?.pageSize && opts?.pageSize > 0) {
|
||||
|
|
@ -35,7 +53,7 @@ const useBranchStore = defineStore('api-branch', () => {
|
|||
|
||||
const query = params.toString();
|
||||
|
||||
const res = await api.get<Pagination<T[]>>(
|
||||
const res = await api.get<Data>(
|
||||
`/branch${(params && '?'.concat(query)) || ''}`,
|
||||
{
|
||||
headers: {
|
||||
|
|
@ -48,27 +66,31 @@ const useBranchStore = defineStore('api-branch', () => {
|
|||
|
||||
if (res && res.status === 200) {
|
||||
data.value = res.data;
|
||||
return data.value as Pagination<T[]>;
|
||||
return res.data;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async function fetchById<T extends Branch>(
|
||||
async function fetchById<
|
||||
Options extends { includeSubBranch?: boolean },
|
||||
Data extends Branch &
|
||||
(Options['includeSubBranch'] extends true ? { branch: [] } : unknown),
|
||||
>(
|
||||
id: string,
|
||||
opts?: { includeSubBranch?: boolean },
|
||||
opts?: Options,
|
||||
flow?: {
|
||||
sessionId: string;
|
||||
refTransactionId: string;
|
||||
transactionId: string;
|
||||
},
|
||||
) {
|
||||
): Promise<Data | false> {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
if (opts?.includeSubBranch) params.append('includeSubBranch', 'true');
|
||||
|
||||
const query = params.toString();
|
||||
|
||||
const res = await api.get<T>(
|
||||
const res = await api.get<Data>(
|
||||
`/branch/${id}${(params && '?'.concat(query)) || ''}`,
|
||||
{
|
||||
headers: {
|
||||
|
|
@ -81,7 +103,6 @@ const useBranchStore = defineStore('api-branch', () => {
|
|||
|
||||
if (!res) return false;
|
||||
if (res.status === 200) return res.data;
|
||||
if (res.status === 204) return null;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
@ -275,6 +296,7 @@ const useBranchStore = defineStore('api-branch', () => {
|
|||
|
||||
return {
|
||||
data,
|
||||
map,
|
||||
fetchList,
|
||||
fetchById,
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ export type Branch = {
|
|||
taxNo: string;
|
||||
id: string;
|
||||
code: string;
|
||||
branch: Branch[];
|
||||
};
|
||||
|
||||
export type BranchWithChildren = Branch & { branch: Branch[] };
|
||||
|
|
@ -50,8 +49,8 @@ export type BranchCreate = {
|
|||
};
|
||||
|
||||
export type BranchUserStats = {
|
||||
id: string,
|
||||
nameEN: string,
|
||||
id: string;
|
||||
nameEN: string;
|
||||
name: string;
|
||||
count: 0
|
||||
}
|
||||
count: 0;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue