start simplifying form data handling

This commit is contained in:
Thomas Ruoff
2020-07-27 00:42:08 +02:00
parent a099d50097
commit 33900b133d
4 changed files with 50 additions and 56 deletions

View File

@@ -5,11 +5,7 @@ export const WizardContext = React.createContext()
export const ACTIONS = {
NEXT_STEP: 'nextStep',
PREV_STEP: 'prevStep',
SET_MULTIPLE_DAYS: 'setMultipleDays',
SET_DATE: 'setDate',
SET_FOCUSED_INPUT: 'setFocusedInput',
SET_PICKUP_TIME: 'setPickupTime',
SET_DROPOFF_TIME: 'setDropoffTime',
SET_FORM_DATA: 'setFormData',
}
function reducer(state, action) {
@@ -24,27 +20,14 @@ function reducer(state, action) {
...state,
currentStep: Math.max(0, state.currentStep - 1),
}
case ACTIONS.SET_MULTIPLE_DAYS:
case ACTIONS.SET_FORM_DATA:
return {
...state,
multipleDays: action.payload,
focusedInput: undefined,
startDate: undefined,
endDate: undefined,
days: [],
formData: {
...state.formData,
...action.payload,
},
}
case ACTIONS.SET_DATE:
return {
...state,
startDate: action.payload.startDate,
endDate: action.payload.endDate,
}
case ACTIONS.SET_FOCUSED_INPUT:
return { ...state, focusedInput: action.payload }
case ACTIONS.SET_PICKUP_TIME:
return { ...state, pickupTime: action.payload }
case ACTIONS.SET_DROPOFF_TIME:
return { ...state, dropoffTime: action.payload }
default:
throw new Error(`Unkown Action type ${action.type}`)
}
@@ -58,7 +41,7 @@ function debugReducer(state, action) {
}
async function createBooking(state) {
const { name, email, startDate, endDate } = state
const { name, email, startDate, endDate } = state.formData
const response = await fetch('/api/booking', {
method: 'POST',
mode: 'cors',
@@ -75,10 +58,12 @@ async function createBooking(state) {
}
const initialState = {
name: 'Thomas Ruoff',
email: 'thomasruoff@gmail.com',
formData: {
name: 'Thomas Ruoff',
email: 'thomasruoff@gmail.com',
multipleDays: null,
},
currentStep: 0,
multipleDays: null,
bookedOn: [
'2020-07-10',
'2020-07-11',