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