From b69146ff8a85e8e68d878becd76196df9453369b Mon Sep 17 00:00:00 2001 From: Methapon-Frappet Date: Fri, 12 Apr 2024 21:52:16 +0700 Subject: [PATCH] refactor: conditional type based on args --- src/stores/branch/index.ts | 46 ++++++++++++++++++++++++++++---------- src/stores/branch/types.ts | 9 ++++---- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/stores/branch/index.ts b/src/stores/branch/index.ts index 8d80f3ec..9ce6513e 100644 --- a/src/stores/branch/index.ts +++ b/src/stores/branch/index.ts @@ -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>(); + const data = ref>({ + result: [], + page: 0, + pageSize: 0, + total: 0, + }); + const map = ref>({}); - async function fetchList( - 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, + >( + opts?: Options, flow?: { sessionId: string; refTransactionId: string; transactionId: string; }, - ) { + ): Promise { 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>( + const res = await api.get( `/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; + return res.data; } return false; } - async function fetchById( + 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 { const params = new URLSearchParams(); if (opts?.includeSubBranch) params.append('includeSubBranch', 'true'); const query = params.toString(); - const res = await api.get( + const res = await api.get( `/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, diff --git a/src/stores/branch/types.ts b/src/stores/branch/types.ts index abdc182c..00de4e2e 100644 --- a/src/stores/branch/types.ts +++ b/src/stores/branch/types.ts @@ -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; +};