Skip to content

useCashExpensesStore

This store is responsible for managing cash expenses. It provides methods to update, and delete cash expenses. It manages online and offline work by persisting data when offline and syncing it when back online again.

📄 View source on GitHub

Used by

Used Stores

State

NameTypeDescription
cashExpensesref
cashExpenseTypesref
cashExpensesLoadedref
cashExpenseTypesLoadedref

Initialization

initializeCashExpensesStore 🚀

Initializes the cash expenses store. Tries to load the state from local storage, and if skipFetch is not set to true, fetches the cash expenses data from the backend and updates the store.

Actions

processQueue

Processes the sync queue for cash expenses, which are created, updated, or deleted offline. The function processes the queue by sending each task to the backend server. If a network error occurs or the device is offline, the function aborts and sets the application as offline. Otherwise, the function processes the queue and updates the sales entries in the store.

persist

Persist the current state of the store to the browser's local storage. This function should be called when every time data was loaded / store state was changed.

fetchCashExpenses

Fetches cash expenses for a specified run date from the backend /cash-expenses endpoint. The data is populated with all fields related to cash expenses and filtered by the given run date. Upon successful retrieval, the data is stored in the cashExpenses state and the cashExpensesLoaded flag is set to true. The state is persisted to local storage to ensure data consistency. If the fetch fails, an error is logged and the cashExpensesLoaded flag is set to false.

getExpenseTypes

Fetches the list of expense types from the backend /content-type-builder/content-types/api::cash-expense.cash-expense endpoint. The data is populated with the expense types and stored in the cashExpenseTypes state. If the fetch fails, an error is logged.

createCashExpense

Creates a new cash expense for the specified run date. This asynchronous function attempts to send the cash expense data to the backend server. If the device is offline or a network error occurs, it enqueues the cash expense for later synchronization. The function constructs the payload from the provided expense object and uses an idempotency key to ensure unique requests.

enqueueCashExpenseFallback

Nested in:: createCashExpense

Enqueues a sync task to create a cash expense when the user comes online later, and adds the expense to the local state. This function is called when the device is offline or a network error occurs during the createSignInEntry function. The function generates a unique idempotency key, a unique expense id, and enqueues the expense with the sync store. It then adds the expense to the local state with the document id set to the generated expense id.

updateCashExpense

This function is currently not in use! Updates a cash expense and its details. This asynchronous function attempts to update the cash expense identified by entryId with the provided data. If the device is offline or a network error occurs, it enqueues the update for later synchronization. On successful update, it fetches the latest cash expenses data.

enqueueCashExpenseUpdateFallback

Nested in: updateCashExpense

Enqueues the cash expense update for later synchronization and updates the local cash expense state, but only if the device is offline or a network error occurs. The function generates a unique idempotency key and a unique cash expense id, and then enqueues the cash expense update with the sync store. It then pushes the cash expense update to the local cash expense state with the document id set to the generated cash expense id.

deleteCashExpense

Deletes a cash expense entry. If the device is offline, this function will enqueue a sync task to delete the expense when the user comes online later. If a network error occurs, this function will also enqueue a sync task to delete the expense when the user comes online later.

enqueueCashExpenseDeleteFallback

Nested in: deleteCashExpense

Enqueues the cash expense entry for later deletion and removes it from the local cash expense state, but only if the device is offline or a network error occurs. The function enqueues the cash expense entry with the sync store and then filters the local cash expense state to remove the entry with the given document id.

abortOnError

Nested in: processQueue

Checks if the given error is a network error (i.e. 'network', 'timeout', or 'aborted'), and if so, logs a warning message, sets the app as offline, and returns true. Otherwise, returns false.

Getters/Setters

expensesForTable (computed)

This computed property transforms the cashExpenses state into a format suitable for displaying in the expenses table.

Released under the MIT License.