/** * Import types from Nadal. */ import type { UserBankItem } from '#lib/types' import { ref } from 'vue' import { useNadal } from '#lib/composables' /** * Custom hook for user banks. * @returns An object containing the user banks, loading status, and functions to add and refresh banks. * @namespace */ export const useUserBank = () => { const { account } = useNadal() const userBanks = ref([]) const isLoading = ref(false) /** * Fetches the user banks from the server. * * BankResponseData type is imported from the Nadal module. * @returns An array of user banks. */ const fetchUserBanks = async () => { isLoading.value = true try { const data = await account.banks() userBanks.value = (data ?? []) .filter((bank) => !bank.is_disable) .sort( (a, b) => new Date(b.last_updated_time!).getTime() - new Date(a.last_updated_time!).getTime(), ) return userBanks.value } catch (ex) { console.error(' ~ fetchUserBanks ~ ex:', ex) } finally { isLoading.value = false } } /** * Adds a new bank account for the user. * @param userBank The bank account to be added. */ const onAddBank = async (userBank: UserBankItem): Promise => { await account.addBankAccount(userBank) } return { isLoading, userBanks, onAddBank, fetchUserBanks, } }