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

@@ -7,7 +7,7 @@ import Col from 'react-bootstrap/Col'
export default function Contact() { export default function Contact() {
const { state } = useContext(WizardContext) const { state } = useContext(WizardContext)
const { name, email, street, zip, city } = state const { name, email, street, zip, city } = state.formData
return ( return (
<> <>

View File

@@ -1,4 +1,4 @@
import { useContext } from 'react' import { useContext, useState } from 'react'
import useSWR from 'swr' import useSWR from 'swr'
import { WizardContext, ACTIONS } from '../context/wizardStore' import { WizardContext, ACTIONS } from '../context/wizardStore'
@@ -12,13 +12,14 @@ import { DateRangePicker, SingleDatePicker } from 'react-dates'
const fetcher = (path) => fetch(path).then((r) => r.json()) const fetcher = (path) => fetch(path).then((r) => r.json())
export default function DateSelect() { export default function DateSelect() {
const [focusedInput, setFocusedInput] = useState(null)
const { state, dispatch } = useContext(WizardContext) const { state, dispatch } = useContext(WizardContext)
const { data: daysBooked, error: fetchBookedOnError } = useSWR( const { data: daysBooked, error: fetchBookedOnError } = useSWR(
'/api/daysbooked', '/api/daysbooked',
fetcher fetcher
) )
const { multipleDays, startDate, endDate, focusedInput } = state const { multipleDays, startDate, endDate } = state.formData
function isDayBlocked(momentDay) { function isDayBlocked(momentDay) {
return ( return (
@@ -45,10 +46,18 @@ export default function DateSelect() {
label="Einen Tag" label="Einen Tag"
name="multipleDays" name="multipleDays"
value="single" value="single"
checked={state.multipleDays === false} checked={multipleDays === false}
onChange={() => onChange={() => {
dispatch({ type: ACTIONS.SET_MULTIPLE_DAYS, payload: false }) setFocusedInput(null)
} dispatch({
type: ACTIONS.SET_FORM_DATA,
payload: {
multipleDays: false,
startDate: null,
endDate: null,
},
})
}}
/> />
<Form.Check <Form.Check
type="radio" type="radio"
@@ -56,10 +65,18 @@ export default function DateSelect() {
label="Mehrere Tage" label="Mehrere Tage"
name="multipleDays" name="multipleDays"
value="multiple" value="multiple"
checked={state.multipleDays === true} checked={multipleDays === true}
onChange={() => onChange={() => {
dispatch({ type: ACTIONS.SET_MULTIPLE_DAYS, payload: true }) setFocusedInput(null)
} dispatch({
type: ACTIONS.SET_FORM_DATA,
payload: {
multipleDays: true,
startDate: null,
endDate: null,
},
})
}}
/> />
</Form.Group> </Form.Group>
{multipleDays !== null && ( {multipleDays !== null && (
@@ -67,7 +84,7 @@ export default function DateSelect() {
<Form.Label component="legend" style={{ display: 'block' }}> <Form.Label component="legend" style={{ display: 'block' }}>
Datum Datum
</Form.Label> </Form.Label>
{state.multipleDays === false && ( {multipleDays === false && (
<SingleDatePicker <SingleDatePicker
small={true} small={true}
date={startDate && moment(startDate)} date={startDate && moment(startDate)}
@@ -75,22 +92,19 @@ export default function DateSelect() {
numberOfMonths={1} numberOfMonths={1}
onDateChange={(date) => onDateChange={(date) =>
dispatch({ dispatch({
type: ACTIONS.SET_DATE, type: ACTIONS.SET_FORM_DATA,
payload: { startDate: date.toISOString() }, payload: {
startDate: date && date.toISOString(),
},
}) })
} }
focused={typeof focusedInput === 'boolean' && focusedInput} focused={typeof focusedInput === 'boolean' && focusedInput}
onFocusChange={({ focused }) => onFocusChange={({ focused }) => setFocusedInput(focused)}
dispatch({
type: ACTIONS.SET_FOCUSED_INPUT,
payload: focused,
})
}
isDayBlocked={isDayBlocked} isDayBlocked={isDayBlocked}
id="startDate" id="startDate"
/> />
)} )}
{state.multipleDays === true && ( {multipleDays === true && (
<DateRangePicker <DateRangePicker
small={true} small={true}
startDate={startDate && moment(startDate)} startDate={startDate && moment(startDate)}
@@ -102,7 +116,7 @@ export default function DateSelect() {
endDateId="endDate" endDateId="endDate"
onDatesChange={({ startDate, endDate }) => { onDatesChange={({ startDate, endDate }) => {
dispatch({ dispatch({
type: ACTIONS.SET_DATE, type: ACTIONS.SET_FORM_DATA,
payload: { payload: {
startDate: startDate && startDate.toISOString(), startDate: startDate && startDate.toISOString(),
endDate: endDate && endDate.toISOString(), endDate: endDate && endDate.toISOString(),
@@ -110,12 +124,7 @@ export default function DateSelect() {
}) })
}} }}
focusedInput={focusedInput} focusedInput={focusedInput}
onFocusChange={(focusedInput) => onFocusChange={(focusedInput) => setFocusedInput(focusedInput)}
dispatch({
type: ACTIONS.SET_FOCUSED_INPUT,
payload: focusedInput,
})
}
isDayBlocked={isDayBlocked} isDayBlocked={isDayBlocked}
minDate={moment()} minDate={moment()}
/> />

View File

@@ -6,7 +6,7 @@ import Form from 'react-bootstrap/Form'
export default function Contact() { export default function Contact() {
const { state } = useContext(WizardContext) const { state } = useContext(WizardContext)
const { purpose, destination, org } = state const { purpose, destination, org } = state.formData
return ( return (
<> <>

View File

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