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