appStore was a stupid idea...

This commit is contained in:
Thomas Ruoff
2020-08-22 00:04:47 +02:00
parent 06f11e4e6d
commit 826ea43363
6 changed files with 74 additions and 35 deletions

View File

@@ -1,10 +1,12 @@
import React, { useReducer, useEffect } from 'react'
import { useRouter } from 'next/router'
import {
storeBookingData,
loadBookingData,
clearBookingData,
} from '../helpers/storage'
import { storeFormData, loadFormData, clearFormData } from '../helpers/storage'
export const AppContext = React.createContext()
export const WizardContext = React.createContext()
export const ACTIONS = {
SET_FORM_DATA: 'setFormData',
@@ -122,13 +124,11 @@ async function createBooking(formData) {
return response.json()
}
export default function AppStore({ children }) {
export default function WizardStore({ children }) {
const [state, dispatch] = useReducer(debugReducer, initialState)
const router = useRouter()
useEffect(() => {
const data = loadFormData()
const data = loadBookingData()
if (data !== null) {
dispatch({ type: ACTIONS.DATA_STORED_LOADED, payload: data })
}
@@ -154,10 +154,8 @@ export default function AppStore({ children }) {
dispatch({ type: ACTIONS.POST_DATA })
try {
const bookingData = await createBooking(state.formData)
dispatch({ type: ACTIONS.POST_DATA_SUCCESS, payload: bookingData })
router.push('/booking/[id]', `/booking/${bookingData.uuid}`)
const booking = await createBooking(state.formData)
dispatch({ type: ACTIONS.POST_DATA_SUCCESS, payload: booking })
} catch (error) {
console.error(error)
dispatch({ type: ACTIONS.POST_DATA_ERROR, payload: error.message })
@@ -166,19 +164,19 @@ export default function AppStore({ children }) {
const storeData = (value) => {
if (value) {
storeFormData(state.formData)
storeBookingData(state.booking)
}
dispatch({ type: ACTIONS.DATA_STORED, payload: value })
}
const forgetData = () => {
clearFormData()
clearBookingData()
dispatch({ type: ACTIONS.DATA_STORED_FORGOTTEN })
}
return (
<AppContext.Provider
<WizardContext.Provider
value={{
state,
dispatch,
@@ -190,6 +188,6 @@ export default function AppStore({ children }) {
}}
>
{children}
</AppContext.Provider>
</WizardContext.Provider>
)
}