Skip to content

useSignInGuestsStore

This store is responsible for handling the guest registration. It provides methods to update, and delete guest registrations, as well as the active guest 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
guestSignInsrefHolds the guest registrations
activeGuestsrefHolds the list with all available guests not archived
guestLoadedrefLoading state for the activeGuests
signInsLoadedrefLoading state for the guestSignIns

Initialization

initializeGuestSignInStore 🚀

Initializes the guest registration store. Tries to load the state from local storage, processes any pending sync tasks, and if skipFetch is not set to true, fetches the guest sign-in data from the backend and updates the store.

Actions

persist

Persist the state of the store to local storage. This function is a wrapper around the persistState function from usePersistentStore. It should be called after making changes to the store state.

fetchGuestSignIns

Fetches the guest registrations from the /guests-sign-ins endpoint for the given run date and stores them in the guestSignIns state. It also updates the signInsLoaded state to indicate whether the data has been loaded successfully. If the data is loaded successfully, it calls persist to save the state to local storage. If the data cannot be loaded, it logs an error and sets signInsLoaded to false.

fetchGuestData

Fetches the active guest data from the /guests endpoint and stores it in the activeGuests state. It also updates the guestLoaded state to indicate whether the data has been loaded successfully. If the data is loaded successfully, it calls persist to save the state to local storage. If the data cannot be loaded, it logs an error and sets guestLoaded to false.

mobileDigits

Nested in: createGuest ->enqueueGuestFallback

Returns the number of digits in a mobile phone number, ignoring spaces, plus sign, and non-digit characters.

enqueueDeleteFallback

Nested in: deleteSignInEntry

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

enqueueUpdateFallback

Nested in: updateSignInEntry

Enqueues a sync task to update a guest registration, and updates the local state of the registration. This function is called when the device is offline or a network error occurs during the updateSignInEntry function. The function enqueues the guest registration with the sync store and then updates the local state of registration with the given type and data.

createGuest

Adds a new guest by sending a POST request to the /guests endpoint with the provided newGuest data. If the request is successful, it registers the guest and updates the registration data. If the request fails due to a network issue (e.g. no internet connection), it enqueues the guest creation for later using the enqueueGuestFallback function.

normalizeMobile

Nested_in: createGuest

Utility function to normalize a mobile phone number. It removes spaces, plus sign, and non-digit characters, then slices the last 10 digits. If the result has less than 10 digits, it pads with leading zeros.

enqueueGuestFallback

Nested in: createGuest

Utility function to enqueue an add Guest task in the sync queue. This function is called when the guest creation fails due to a network issue. It adds a new guest in the activeGuests array with a unique ID and enqueues a sync task to add the guest entry on the server. It also creates a register Guest task for the guest.

createSignInEntry

Create a registration for a given guest. If the user is offline, enqueue a sync task to sync the registration when the user comes back online.

enqueueGuestSignInFallback

Nested in: createSignInEntry

Enqueues a sync task to create a guest registration for a given guest, and adds the registration to the local state of registration. 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 registration id, and enqueues the registration with the sync store. It then adds the registration to the local state with the document id set to the generated registration id.

toggleCheckIn

Toggles the sign-in status of a guest registration. If the user is offline, this function will enqueue a sync task to toggle the sign-in/sign-out status when the user comes online later. If a network error occurs, this function will also enqueue a sync task to toggle the sign-in status when the user comes online later.

enqueueToggleFallback

Nested in: toggleCheckIn

Enqueues a sync task to toggle the sign-in/sign-out status of a guest registration and updates the local state of registration. This function is called when the device is offline or a network error occurs during the toggleCheckIn function. The function enqueues the registration with the sync store and then updates the state of registrations with the given newState.

deleteSignInEntry

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

updateSignInEntry

Updates a guest registration. This asynchronous function attempts to update a specific guest registration 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 registration data.

processQueue

Processes the sync queue for guest-related data, 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 guest entries in the store.

abortOnError

Nested in: abortOnError

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

signedInGuests (computed)

Getter to transform the raw guests entries to the format we need for the SignInTableGuest

Released under the MIT License.