Skip to content

useOnsiteSalesStore

This store manages the onsite sales data for a specific run date. It provides methods to fetch, update, and delete sales entries, as well as the active buyers list. 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
onsiteSalesrefHolds the onsite sales data
salesItemsrefHolds the list with available sales items
onsiteSalesLoadedrefLoading state for the onsiteSales
salesItemsLoadedrefLoading state for the salesItems

Initialization

initializeOnsiteSalesStore 🚀

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

Actions

persist

Persists the current state of the onsite sales store to ensure data integrity and consistency. This function leverages the persistent storage mechanism to save the store's state, allowing data to be retained across sessions and synchronized when back online.

fetchActiveBuyers

Fetches the active members and guests who have signed in for the current run date, ensuring that the data is up-to-date before retrieving the active buyers.

fetchOnsiteSales

Fetches the onsite sales data for the given run date from the /onsite-sales endpoint. This endpoint is populated with the associated member, guest, and sales item data. The function first constructs the query parameters for the paginated data fetch, then calls the fetchPaginatedData utility. After a successful fetch, it persists the data using the persist function.

fetchSalesItems

Fetches the available, valid sales items for the provided run date from the /sales-items endpoint. The function constructs query parameters to sort items by name and filter out items whose end date is before the run date or unset. After fetching the data, it persists the state to ensure data integrity.

processQueue

Processes the sync queue for onsite sales entries, 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.

createOnsiteSalesEntry

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

enqueueSalesFallback

Nested in: createOnsiteSalesEntry

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

updateOnsiteSalesEntry

Updates the payment type of an onsite sales entry. This asynchronous function attempts to update the payment type of a specific onsite sales entry 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 onsite sales data.

enqueueSalesUpdateFallback

Nested in: updateOnsiteSalesEntry

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

deleteOnsiteSalesEntry

Deletes an onsite sales entry using the deleteOnsiteSalesEntry method of the onsiteSalesStore. Shows a confirmation dialog before deleting the entry. After deletion, shows a snackbar notification with the result.

enqueueSalesDeleteFallback

Nested in: deleteOnsiteSalesEntry

Enqueues the sales entry for later deletion and removes it from the local sales state, but only if the device is offline or a network error occurs. The function enqueues the sales entry with the sync store and then filters the local sales 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

activeBuyers (computed)

Combines all members and guests registered for the run in a single table and sorts them alphabetically. Indicates member or guest as the buyer type for later filtering.

totalSales (computed)

Transforms and combines the onsiteSales data into a format to be displayed in the SalesTable component.

Released under the MIT License.