mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
add appStore and use router for booking
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import React, { useReducer, useEffect } from 'react'
|
||||
|
||||
import { useRouter } from 'next/router'
|
||||
|
||||
import { storeFormData, loadFormData, clearFormData } from '../helpers/storage'
|
||||
|
||||
export const WizardContext = React.createContext()
|
||||
export const AppContext = React.createContext()
|
||||
|
||||
export const ACTIONS = {
|
||||
SET_FORM_DATA: 'setFormData',
|
||||
@@ -47,8 +49,8 @@ function reducer(state, action) {
|
||||
...state,
|
||||
formData: {
|
||||
...state.formData,
|
||||
...action.payload,
|
||||
},
|
||||
booking: { ...action.payload },
|
||||
postData: false,
|
||||
postDataError: null,
|
||||
postDataSuccess: true,
|
||||
@@ -120,9 +122,11 @@ async function createBooking(formData) {
|
||||
return response.json()
|
||||
}
|
||||
|
||||
export default function WizardStore({ children }) {
|
||||
export default function AppStore({ children }) {
|
||||
const [state, dispatch] = useReducer(debugReducer, initialState)
|
||||
|
||||
const router = useRouter()
|
||||
|
||||
useEffect(() => {
|
||||
const data = loadFormData()
|
||||
if (data !== null) {
|
||||
@@ -152,6 +156,8 @@ export default function WizardStore({ children }) {
|
||||
try {
|
||||
const bookingData = await createBooking(state.formData)
|
||||
dispatch({ type: ACTIONS.POST_DATA_SUCCESS, payload: bookingData })
|
||||
|
||||
router.push('/booking/[id]', `/booking/${bookingData._id}`)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
dispatch({ type: ACTIONS.POST_DATA_ERROR, payload: error.message })
|
||||
@@ -172,7 +178,7 @@ export default function WizardStore({ children }) {
|
||||
}
|
||||
|
||||
return (
|
||||
<WizardContext.Provider
|
||||
<AppContext.Provider
|
||||
value={{
|
||||
state,
|
||||
dispatch,
|
||||
@@ -184,6 +190,6 @@ export default function WizardStore({ children }) {
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</WizardContext.Provider>
|
||||
</AppContext.Provider>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user