import { type Ref, defineComponent, inject, onMounted, ref } from 'vue'; import TransactionService from './transaction.service'; import { type ITransaction } from '@/shared/model/transaction.model'; import { useAlertService } from '@/shared/alert/alert.service'; import type AccountService from '@/account/account.service'; export default defineComponent({ compatConfig: { MODE: 3 }, name: 'Transaction', props: { userAccountId: { type: [Number, String], required: false, default: null, }, }, setup(props) { const transactionService = inject('transactionService', () => new TransactionService()); const alertService = inject('alertService', () => useAlertService(), true); const transactions: Ref = ref([]); const hasAnyAuthorityValues: Ref = ref({}); const accountService = inject('accountService'); const isFetching = ref(false); const clear = () => {}; // console.log(props.userAccountId); const retrieveTransactions = async () => { isFetching.value = true; try { // console.log(props); const res = await transactionService().retrieve(); // const res = await transactionService().retrieveForAccount(props.userAccountId); transactions.value = res.data; } catch (err) { alertService.showHttpError(err.response); } finally { isFetching.value = false; } }; const handleSyncList = () => { retrieveTransactions(); }; onMounted(async () => { await retrieveTransactions(); }); const removeId: Ref = ref(null); const removeEntity = ref(null); const prepareRemove = (instance: ITransaction) => { removeId.value = instance.id; removeEntity.value.show(); }; const closeDialog = () => { removeEntity.value.hide(); }; const removeTransaction = async () => { try { await transactionService().delete(removeId.value); const message = `A Transaction is deleted with identifier ${removeId.value}`; alertService.showInfo(message, { variant: 'danger' }); removeId.value = null; retrieveTransactions(); closeDialog(); } catch (error) { alertService.showHttpError(error.response); } }; return { transactions, handleSyncList, isFetching, retrieveTransactions, clear, removeId, accountService, hasAnyAuthorityValues, removeEntity, prepareRemove, closeDialog, removeTransaction, }; }, methods: { hasAnyAuthority(authorities: any): boolean { this.accountService.hasAnyAuthorityAndCheckAuth(authorities).then(value => { if (this.hasAnyAuthorityValues[authorities] !== value) { this.hasAnyAuthorityValues = { ...this.hasAnyAuthorityValues, [authorities]: value }; } }); return this.hasAnyAuthorityValues[authorities] ?? false; }, }, });