useSchedule hook provides a way to fetch, display, and update the notification schedule for the current subscriber.
Hook parameters
| Property | Type | Description |
|---|---|---|
onSuccess | (data: Schedule) => void | Callback function called when the schedule is successfully fetched |
onError | (error: NovuError) => void | Callback function called when an error occurs |
Return value
| Property | Type | Description | |
|---|---|---|---|
schedule | `Schedule | undefined` | The subscriber |
error | `NovuError | undefined` | Error object if the request failed |
isLoading | boolean | True during the initial load, false otherwise (default: true) | |
isFetching | boolean | True while any request is in flight (initial load or refetch), false otherwise (default: true) | |
refetch | () => Promise<void> | Function to manually trigger a refetch of the schedule |
Schedule type
TheSchedule type from @novu/react includes these properties:
| Property | Type | Description |
|---|---|---|
isEnabled | boolean | Global flag to enable or disable the entire schedule. |
weeklySchedule | object | An object containing the schedule for each day of the week (for example, monday, tuesday). Each key holds a DaySchedule object. |
Schedule object structure.
EachDaySchedule object within weeklySchedule has the following structure:
| Property | Type | Description |
|---|---|---|
isEnabled | boolean | Flag to enable or disable the schedule for this specific day. |
hours | Array<{ start: string; end: string; }> | An array of time ranges for when notifications are allowed. Multiple ranges per day are supported (for example, 9-12 AM and 2-5 PM). |
Updating the schedule
The hook also allows updating the subscriber’s schedule viaschedule.update:
Example usage
Here’s how to use theuseSchedule hook to display and update a subscriber’s notification schedule. The schedule.update() method creates a schedule if one doesn’t exist or updates the existing one.