mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 14:37:13 +01:00
move daysbooked fetch to context
This commit is contained in:
@@ -2,6 +2,9 @@ import React, { useEffect, useReducer } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
import { clearBookingData, loadBookingData } from '../../../helpers/storage'
|
||||
import { ValidationError } from './validationError'
|
||||
import useSWR from 'swr'
|
||||
|
||||
const fetcher = (path: string) => fetch(path).then((r) => r.json())
|
||||
|
||||
interface WizardFormData {
|
||||
startDate: string
|
||||
@@ -39,6 +42,8 @@ interface WizardStore {
|
||||
onChangeEvent: (event: React.ChangeEvent<React.ElementRef<'input'>>) => void
|
||||
onSubmit: () => void
|
||||
forgetData: () => void
|
||||
daysBooked: string[]
|
||||
daysBookedErrorMessage: string
|
||||
}
|
||||
|
||||
interface WizardAction {
|
||||
@@ -164,6 +169,10 @@ async function createBooking(formData: WizardFormData) {
|
||||
export default function WizardStore({ children }) {
|
||||
const router = useRouter()
|
||||
const [state, dispatch] = useReducer(reducer, initialState)
|
||||
const { data: daysBooked, error: daysBookedError } = useSWR<string[], Error>(
|
||||
'/api/daysbooked',
|
||||
fetcher
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
const data = loadBookingData()
|
||||
@@ -216,6 +225,8 @@ export default function WizardStore({ children }) {
|
||||
onChange,
|
||||
onSubmit,
|
||||
forgetData,
|
||||
daysBooked,
|
||||
daysBookedErrorMessage: daysBookedError?.message,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
|
||||
Reference in New Issue
Block a user