make storing work

This commit is contained in:
Thomas Ruoff
2020-07-25 19:19:37 +02:00
parent 279210599b
commit 90a9288e84
6 changed files with 201 additions and 2 deletions

View File

@@ -1,5 +1,7 @@
import React, { useReducer } from 'react'
import { getDays } from '../lib/dateHelper'
export const WizardContext = React.createContext()
export const ACTIONS = {
@@ -10,6 +12,7 @@ export const ACTIONS = {
SET_FOCUSED_INPUT: 'setFocusedInput',
SET_PICKUP_TIME: 'setPickupTime',
SET_DROPOFF_TIME: 'setDropoffTime',
SUBMIT: 'submit',
}
function reducer(state, action) {
@@ -31,12 +34,18 @@ function reducer(state, action) {
focusedInput: undefined,
startDate: undefined,
endDate: undefined,
days: [],
}
case ACTIONS.SET_DATE:
return {
...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 }
@@ -44,6 +53,10 @@ 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}`)
}
@@ -56,7 +69,26 @@ function debugReducer(state, action) {
return newState
}
async function createBooking(state) {
const { name, email, days } = state
const response = await fetch('/api/booking', {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
},
referrerPolicy: 'no-referrer',
body: JSON.stringify({ name, email, days }),
})
return response.json()
}
const initialState = {
name: 'Thomas Ruoff',
email: 'thomasruoff@gmail.com',
currentStep: 0,
multipleDays: null,
bookedOn: [