mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
more hacking
This commit is contained in:
@@ -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>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user