mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
add a wizard
This commit is contained in:
@@ -1,18 +1,14 @@
|
||||
import { useContext } from 'react'
|
||||
import { AppContext, ACTIONS } from '../context/appStore'
|
||||
import { WizardContext, ACTIONS } from '../context/wizardStore'
|
||||
|
||||
import Form from 'react-bootstrap/Form'
|
||||
import Col from 'react-bootstrap/Col'
|
||||
|
||||
export default function Contact() {
|
||||
const { state, dispatch } = useContext(AppContext)
|
||||
const { state, dispatch } = useContext(WizardContext)
|
||||
|
||||
const { multipleDays, startDate, endDate } = state
|
||||
|
||||
if (!startDate || (multipleDays && !endDate)) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Form.Group>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useContext } from 'react'
|
||||
import { AppContext, ACTIONS } from '../context/appStore'
|
||||
import { WizardContext, ACTIONS } from '../context/wizardStore'
|
||||
|
||||
import Form from 'react-bootstrap/Form'
|
||||
|
||||
@@ -8,7 +8,7 @@ import 'react-dates/initialize'
|
||||
import { DateRangePicker, SingleDatePicker } from 'react-dates'
|
||||
|
||||
export default function DateSelect() {
|
||||
const { state, dispatch } = useContext(AppContext)
|
||||
const { state, dispatch } = useContext(WizardContext)
|
||||
|
||||
const {
|
||||
multipleDays,
|
||||
@@ -24,65 +24,88 @@ export default function DateSelect() {
|
||||
return bookedOn.some((rawDay) => momentDay.isSame(rawDay))
|
||||
}
|
||||
|
||||
if (multipleDays === null || multipleDays === undefined) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Form.Group>
|
||||
<Form.Label component="legend" style={{ display: 'block' }}>
|
||||
Datum
|
||||
</Form.Label>
|
||||
{state.multipleDays === false && (
|
||||
<SingleDatePicker
|
||||
small={true}
|
||||
date={startDate && moment(startDate)}
|
||||
onDateChange={(date) =>
|
||||
dispatch({
|
||||
type: ACTIONS.SET_DATE,
|
||||
payload: { startDate: date.toISOString() },
|
||||
})
|
||||
}
|
||||
focused={typeof focusedInput === 'boolean' && focusedInput}
|
||||
onFocusChange={({ focused }) =>
|
||||
dispatch({
|
||||
type: ACTIONS.SET_FOCUSED_INPUT,
|
||||
payload: focused,
|
||||
})
|
||||
}
|
||||
isDayBlocked={isDayBlocked}
|
||||
id="your_unique_id"
|
||||
/>
|
||||
)}
|
||||
{state.multipleDays === true && (
|
||||
<DateRangePicker
|
||||
small={true}
|
||||
startDate={startDate && moment(startDate)}
|
||||
startDateId="bussle_start_date_id"
|
||||
endDate={endDate && moment(endDate)}
|
||||
endDateId="bussle_end_date_id"
|
||||
onDatesChange={({ startDate, endDate }) => {
|
||||
dispatch({
|
||||
type: ACTIONS.SET_DATE,
|
||||
payload: {
|
||||
startDate: startDate && startDate.toISOString(),
|
||||
endDate: endDate && endDate.toISOString(),
|
||||
},
|
||||
})
|
||||
}}
|
||||
focusedInput={focusedInput}
|
||||
onFocusChange={(focusedInput) =>
|
||||
dispatch({
|
||||
type: ACTIONS.SET_FOCUSED_INPUT,
|
||||
payload: focusedInput,
|
||||
})
|
||||
}
|
||||
isDayBlocked={isDayBlocked}
|
||||
minDate={moment()}
|
||||
/>
|
||||
)}
|
||||
<Form.Group controlId="dateSelect">
|
||||
<Form.Label>Willst du einen odere mehrere Tage buchen?</Form.Label>
|
||||
<Form.Check
|
||||
type="radio"
|
||||
id={'multipleDays-single'}
|
||||
label="Einen Tag"
|
||||
name="multipleDays"
|
||||
value="single"
|
||||
checked={state.multipleDays === false}
|
||||
onChange={() =>
|
||||
dispatch({ type: ACTIONS.SET_MULTIPLE_DAYS, payload: false })
|
||||
}
|
||||
/>
|
||||
<Form.Check
|
||||
type="radio"
|
||||
id={'multipleDays-multiple'}
|
||||
label="Mehrere Tage"
|
||||
name="multipleDays"
|
||||
value="multiple"
|
||||
checked={state.multipleDays === true}
|
||||
onChange={() =>
|
||||
dispatch({ type: ACTIONS.SET_MULTIPLE_DAYS, payload: true })
|
||||
}
|
||||
/>
|
||||
</Form.Group>
|
||||
{multipleDays !== null && (
|
||||
<Form.Group>
|
||||
<Form.Label component="legend" style={{ display: 'block' }}>
|
||||
Datum
|
||||
</Form.Label>
|
||||
{state.multipleDays === false && (
|
||||
<SingleDatePicker
|
||||
small={true}
|
||||
date={startDate && moment(startDate)}
|
||||
onDateChange={(date) =>
|
||||
dispatch({
|
||||
type: ACTIONS.SET_DATE,
|
||||
payload: { startDate: date.toISOString() },
|
||||
})
|
||||
}
|
||||
focused={typeof focusedInput === 'boolean' && focusedInput}
|
||||
onFocusChange={({ focused }) =>
|
||||
dispatch({
|
||||
type: ACTIONS.SET_FOCUSED_INPUT,
|
||||
payload: focused,
|
||||
})
|
||||
}
|
||||
isDayBlocked={isDayBlocked}
|
||||
id="your_unique_id"
|
||||
/>
|
||||
)}
|
||||
{state.multipleDays === true && (
|
||||
<DateRangePicker
|
||||
small={true}
|
||||
startDate={startDate && moment(startDate)}
|
||||
startDateId="bussle_start_date_id"
|
||||
endDate={endDate && moment(endDate)}
|
||||
endDateId="bussle_end_date_id"
|
||||
onDatesChange={({ startDate, endDate }) => {
|
||||
dispatch({
|
||||
type: ACTIONS.SET_DATE,
|
||||
payload: {
|
||||
startDate: startDate && startDate.toISOString(),
|
||||
endDate: endDate && endDate.toISOString(),
|
||||
},
|
||||
})
|
||||
}}
|
||||
focusedInput={focusedInput}
|
||||
onFocusChange={(focusedInput) =>
|
||||
dispatch({
|
||||
type: ACTIONS.SET_FOCUSED_INPUT,
|
||||
payload: focusedInput,
|
||||
})
|
||||
}
|
||||
isDayBlocked={isDayBlocked}
|
||||
minDate={moment()}
|
||||
/>
|
||||
)}
|
||||
</Form.Group>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
import { useContext } from 'react'
|
||||
import { AppContext, ACTIONS } from '../context/appStore'
|
||||
|
||||
import Form from 'react-bootstrap/Form'
|
||||
|
||||
export default function RangeSelect() {
|
||||
const { state, dispatch } = useContext(AppContext)
|
||||
|
||||
function getValue() {
|
||||
const { multipleDays } = state
|
||||
|
||||
if (multipleDays === null || multipleDays === undefined) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (multipleDays === true) {
|
||||
return 'multiple'
|
||||
}
|
||||
|
||||
return 'single'
|
||||
}
|
||||
|
||||
return (
|
||||
<Form.Group controlId="dateSelect">
|
||||
<Form.Label>Willst du einen odere mehrere Tage buchen?</Form.Label>
|
||||
<Form.Check
|
||||
type="radio"
|
||||
id={'multipleDays-single'}
|
||||
label="Einen Tag"
|
||||
name="multipleDays"
|
||||
value="single"
|
||||
checked={state.multipleDays === false}
|
||||
onChange={() =>
|
||||
dispatch({ type: ACTIONS.SET_MULTIPLE_DAYS, payload: false })
|
||||
}
|
||||
/>
|
||||
<Form.Check
|
||||
type="radio"
|
||||
id={'multipleDays-multiple'}
|
||||
label="Mehrere Tage"
|
||||
name="multipleDays"
|
||||
value="multiple"
|
||||
checked={state.multipleDays === true}
|
||||
onChange={() =>
|
||||
dispatch({ type: ACTIONS.SET_MULTIPLE_DAYS, payload: true })
|
||||
}
|
||||
/>
|
||||
</Form.Group>
|
||||
)
|
||||
}
|
||||
55
components/wizard.js
Normal file
55
components/wizard.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import { useContext } from 'react'
|
||||
|
||||
import Button from 'react-bootstrap/Button'
|
||||
import Form from 'react-bootstrap/Form'
|
||||
|
||||
import WizardStore, { WizardContext, ACTIONS } from '../context/wizardStore'
|
||||
|
||||
import DateSelect from './dateSelect'
|
||||
import Contact from './contact'
|
||||
import Driver from './driver'
|
||||
|
||||
const STEPS = [
|
||||
{ id: 'DATE_SELECT', component: DateSelect },
|
||||
{ id: 'CONTACT', component: Contact },
|
||||
]
|
||||
|
||||
function WizardInternal() {
|
||||
const { state, dispatch } = useContext(WizardContext)
|
||||
const { currentStep } = state
|
||||
|
||||
const isFirstStep = currentStep === 0
|
||||
const isLastStep = currentStep === STEPS.length - 1
|
||||
|
||||
return (
|
||||
<Form>
|
||||
{STEPS.map(({ id, component: Component }, index) => (
|
||||
<>{currentStep === index && <Component />}</>
|
||||
))}
|
||||
{!isFirstStep && (
|
||||
<Button
|
||||
variant="secondary"
|
||||
onClick={() => dispatch({ type: ACTIONS.PREV_STEP })}
|
||||
>
|
||||
Zurück
|
||||
</Button>
|
||||
)}
|
||||
{!isLastStep && (
|
||||
<Button onClick={() => dispatch({ type: ACTIONS.NEXT_STEP })}>
|
||||
Weiter
|
||||
</Button>
|
||||
)}
|
||||
{isLastStep && (
|
||||
<Button onClick={() => console.log('SEND OFF', state)}>Absenden</Button>
|
||||
)}
|
||||
</Form>
|
||||
)
|
||||
}
|
||||
|
||||
export default function Wizard() {
|
||||
return (
|
||||
<WizardStore>
|
||||
<WizardInternal />
|
||||
</WizardStore>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user