more hacking

This commit is contained in:
Thomas Ruoff
2020-07-26 00:12:51 +02:00
parent 90a9288e84
commit 518b437d14
13 changed files with 166 additions and 54 deletions

View File

@@ -1,7 +1,5 @@
import React, { useReducer } from 'react'
import { getDays } from '../lib/dateHelper'
export const WizardContext = React.createContext()
export const ACTIONS = {
@@ -12,7 +10,6 @@ export const ACTIONS = {
SET_FOCUSED_INPUT: 'setFocusedInput',
SET_PICKUP_TIME: 'setPickupTime',
SET_DROPOFF_TIME: 'setDropoffTime',
SUBMIT: 'submit',
}
function reducer(state, action) {
@@ -41,11 +38,6 @@ function reducer(state, action) {
...state,
startDate: action.payload.startDate,
endDate: action.payload.endDate,
days: getDays({
startDate: action.payload.startDate || state.startDate,
endDate: action.payload.endDate || state.endDate,
multipleDays: state.multipleDays,
}),
}
case ACTIONS.SET_FOCUSED_INPUT:
return { ...state, focusedInput: action.payload }
@@ -53,10 +45,6 @@ function reducer(state, action) {
return { ...state, pickupTime: action.payload }
case ACTIONS.SET_DROPOFF_TIME:
return { ...state, dropoffTime: action.payload }
case ACTIONS.SUBMIT:
// TODO: should probably not kick this off here, that sucks
createBooking(state)
return { ...state }
default:
throw new Error(`Unkown Action type ${action.type}`)
}
@@ -70,7 +58,7 @@ function debugReducer(state, action) {
}
async function createBooking(state) {
const { name, email, days } = state
const { name, email, startDate, endDate } = state
const response = await fetch('/api/booking', {
method: 'POST',
mode: 'cors',
@@ -80,7 +68,7 @@ async function createBooking(state) {
'Content-Type': 'application/json',
},
referrerPolicy: 'no-referrer',
body: JSON.stringify({ name, email, days }),
body: JSON.stringify({ name, email, startDate, endDate }),
})
return response.json()
@@ -103,8 +91,10 @@ const initialState = {
export default function WizardStore({ children }) {
const [state, dispatch] = useReducer(debugReducer, initialState)
const onSubmit = () => createBooking(state)
return (
<WizardContext.Provider value={{ state, dispatch }}>
<WizardContext.Provider value={{ state, dispatch, onSubmit }}>
{children}
</WizardContext.Provider>
)