Files
BarangaySystem/resources/js/stores/globalTransaction.js
2026-06-06 18:43:00 +08:00

59 lines
1.5 KiB
JavaScript

import { defineStore } from 'pinia';
import axios from 'axios';
export const useGlobalTransactionStore = defineStore('globalTransaction', {
state: () => ({
transactions: [],
isLoading: false,
error: null,
lastFetched: null,
}),
getters: {
getTransactionsByProduct: (state) => (productHash) => {
return state.transactions.filter(tx => tx.product_hash === productHash);
},
getTransactionsByStore: (state) => (storeHash) => {
return state.transactions.filter(tx => tx.store_hash === storeHash);
}
},
actions: {
async fetchTransactions(filters = {}) {
try {
this.isLoading = true;
this.error = null;
const response = await axios.post('/admin/transactions/list', filters);
if (Array.isArray(response.data)) {
this.transactions = response.data;
this.lastFetched = Date.now();
}
return this.transactions;
} catch (err) {
this.error = 'Failed to load transactions';
console.error('Error fetching transactions:', err);
throw err;
} finally {
this.isLoading = false;
}
},
async precache() {
// Avoid refetching if data is fresh (less than 5 minutes old)
if (this.transactions.length > 0 && this.lastFetched && (Date.now() - this.lastFetched < 300000)) {
return;
}
await this.fetchTransactions();
},
clearCache() {
this.transactions = [];
this.lastFetched = null;
}
}
});