mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
improve client a bit
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { useContext } from 'react'
|
import { useContext } from 'react'
|
||||||
import { WizardContext, ACTIONS } from '../context/wizardStore'
|
import { WizardContext, ACTIONS } from '../context/wizardStore'
|
||||||
|
import Required from './required'
|
||||||
|
|
||||||
import Form from 'react-bootstrap/Form'
|
import Form from 'react-bootstrap/Form'
|
||||||
import Col from 'react-bootstrap/Col'
|
import Col from 'react-bootstrap/Col'
|
||||||
@@ -12,54 +13,69 @@ export default function Contact() {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Form.Group>
|
<Form.Group>
|
||||||
<Form.Label>Name</Form.Label>
|
<Form.Label>
|
||||||
|
Name <Required />
|
||||||
|
</Form.Label>
|
||||||
<Form.Control
|
<Form.Control
|
||||||
type="text"
|
type="text"
|
||||||
name="name"
|
name="name"
|
||||||
placeholder="Name"
|
placeholder="Name"
|
||||||
value={name}
|
value={name}
|
||||||
onChange={onChangeEvent}
|
onChange={onChangeEvent}
|
||||||
|
required
|
||||||
/>
|
/>
|
||||||
</Form.Group>
|
</Form.Group>
|
||||||
<Form.Group>
|
<Form.Group>
|
||||||
<Form.Label>E-Mail</Form.Label>
|
<Form.Label>
|
||||||
|
E-Mail <Required />
|
||||||
|
</Form.Label>
|
||||||
<Form.Control
|
<Form.Control
|
||||||
type="email"
|
type="email"
|
||||||
name="email"
|
name="email"
|
||||||
placeholder="E-Mail"
|
placeholder="E-Mail"
|
||||||
value={email}
|
value={email}
|
||||||
onChange={onChangeEvent}
|
onChange={onChangeEvent}
|
||||||
|
required
|
||||||
/>
|
/>
|
||||||
</Form.Group>
|
</Form.Group>
|
||||||
<Form.Group>
|
<Form.Group>
|
||||||
<Form.Label>Straße</Form.Label>
|
<Form.Label>
|
||||||
|
Straße <Required />
|
||||||
|
</Form.Label>
|
||||||
<Form.Control
|
<Form.Control
|
||||||
type="text"
|
type="text"
|
||||||
name="street"
|
name="street"
|
||||||
placeholder="Straße"
|
placeholder="Straße"
|
||||||
value={street}
|
value={street}
|
||||||
onChange={onChangeEvent}
|
onChange={onChangeEvent}
|
||||||
|
required
|
||||||
/>
|
/>
|
||||||
</Form.Group>
|
</Form.Group>
|
||||||
<Form.Row>
|
<Form.Row>
|
||||||
<Form.Group as={Col} xs={4}>
|
<Form.Group as={Col} xs={4}>
|
||||||
<Form.Label>PLZ</Form.Label>
|
<Form.Label>
|
||||||
|
PLZ <Required />
|
||||||
|
</Form.Label>
|
||||||
<Form.Control
|
<Form.Control
|
||||||
type="text"
|
type="text"
|
||||||
name="zip"
|
name="zip"
|
||||||
placeholder="PLZ"
|
placeholder="PLZ"
|
||||||
value={zip}
|
value={zip}
|
||||||
onChange={onChangeEvent}
|
onChange={onChangeEvent}
|
||||||
|
required
|
||||||
/>
|
/>
|
||||||
</Form.Group>
|
</Form.Group>
|
||||||
<Form.Group as={Col}>
|
<Form.Group as={Col}>
|
||||||
<Form.Label>Stadt</Form.Label>
|
<Form.Label>
|
||||||
|
Stadt <Required />
|
||||||
|
</Form.Label>
|
||||||
<Form.Control
|
<Form.Control
|
||||||
type="text"
|
type="text"
|
||||||
name="city"
|
name="city"
|
||||||
placeholder="Stadt"
|
placeholder="Stadt"
|
||||||
value={city}
|
value={city}
|
||||||
onChange={onChangeEvent}
|
onChange={onChangeEvent}
|
||||||
|
required
|
||||||
/>
|
/>
|
||||||
</Form.Group>
|
</Form.Group>
|
||||||
</Form.Row>
|
</Form.Row>
|
||||||
|
|||||||
@@ -9,13 +9,14 @@ import moment from 'moment'
|
|||||||
import 'react-dates/initialize'
|
import 'react-dates/initialize'
|
||||||
import { DateRangePicker, SingleDatePicker } from 'react-dates'
|
import { DateRangePicker, SingleDatePicker } from 'react-dates'
|
||||||
|
|
||||||
|
import Required from './required'
|
||||||
import { dateFormat } from '../lib/dateHelper'
|
import { dateFormat } from '../lib/dateHelper'
|
||||||
|
|
||||||
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 [focusedInput, setFocusedInput] = useState(null)
|
||||||
const { state, onChange, onChangeEvent } = useContext(WizardContext)
|
const { state, onChange } = useContext(WizardContext)
|
||||||
const { data: daysBooked, error: fetchBookedOnError } = useSWR(
|
const { data: daysBooked, error: fetchBookedOnError } = useSWR(
|
||||||
'/api/daysbooked',
|
'/api/daysbooked',
|
||||||
fetcher
|
fetcher
|
||||||
@@ -41,7 +42,9 @@ export default function DateSelect() {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Form.Group controlId="dateSelect">
|
<Form.Group controlId="dateSelect">
|
||||||
<Form.Label>Willst du einen odere mehrere Tage buchen?</Form.Label>
|
<Form.Label>
|
||||||
|
Willst du einen odere mehrere Tage buchen? <Required />
|
||||||
|
</Form.Label>
|
||||||
<Form.Check
|
<Form.Check
|
||||||
type="radio"
|
type="radio"
|
||||||
id={'multipleDays-single'}
|
id={'multipleDays-single'}
|
||||||
@@ -78,7 +81,7 @@ export default function DateSelect() {
|
|||||||
{multipleDays !== null && (
|
{multipleDays !== null && (
|
||||||
<Form.Group>
|
<Form.Group>
|
||||||
<Form.Label component="legend" style={{ display: 'block' }}>
|
<Form.Label component="legend" style={{ display: 'block' }}>
|
||||||
Datum
|
Datum <Required />
|
||||||
</Form.Label>
|
</Form.Label>
|
||||||
{multipleDays === false && (
|
{multipleDays === false && (
|
||||||
<SingleDatePicker
|
<SingleDatePicker
|
||||||
@@ -86,6 +89,7 @@ export default function DateSelect() {
|
|||||||
date={startDate && moment(startDate)}
|
date={startDate && moment(startDate)}
|
||||||
placeholder="Datum"
|
placeholder="Datum"
|
||||||
numberOfMonths={1}
|
numberOfMonths={1}
|
||||||
|
required
|
||||||
onDateChange={(date) =>
|
onDateChange={(date) =>
|
||||||
onChange({
|
onChange({
|
||||||
startDate: date && dateFormat(date),
|
startDate: date && dateFormat(date),
|
||||||
@@ -104,6 +108,7 @@ export default function DateSelect() {
|
|||||||
startDateId="startDate"
|
startDateId="startDate"
|
||||||
startDatePlaceholderText="Start"
|
startDatePlaceholderText="Start"
|
||||||
endDatePlaceholderText="Ende"
|
endDatePlaceholderText="Ende"
|
||||||
|
required
|
||||||
numberOfMonths={1}
|
numberOfMonths={1}
|
||||||
endDate={endDate && moment(endDate)}
|
endDate={endDate && moment(endDate)}
|
||||||
endDateId="endDate"
|
endDateId="endDate"
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { useContext } from 'react'
|
import { useContext } from 'react'
|
||||||
import { WizardContext, ACTIONS } from '../context/wizardStore'
|
import { WizardContext, ACTIONS } from '../context/wizardStore'
|
||||||
|
import Required from './required'
|
||||||
|
|
||||||
import Form from 'react-bootstrap/Form'
|
import Form from 'react-bootstrap/Form'
|
||||||
|
|
||||||
@@ -11,13 +12,16 @@ export default function Contact() {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Form.Group>
|
<Form.Group>
|
||||||
<Form.Label>Zweck der Fahrt</Form.Label>
|
<Form.Label>
|
||||||
|
Zweck der Fahrt <Required />
|
||||||
|
</Form.Label>
|
||||||
<Form.Control
|
<Form.Control
|
||||||
type="text"
|
type="text"
|
||||||
name="purpose"
|
name="purpose"
|
||||||
placeholder="Zweck der Fahrt"
|
placeholder="Zweck der Fahrt"
|
||||||
value={purpose}
|
value={purpose}
|
||||||
onChange={onChangeEvent}
|
onChange={onChangeEvent}
|
||||||
|
required
|
||||||
/>
|
/>
|
||||||
</Form.Group>
|
</Form.Group>
|
||||||
<Form.Group>
|
<Form.Group>
|
||||||
@@ -31,13 +35,16 @@ export default function Contact() {
|
|||||||
/>
|
/>
|
||||||
</Form.Group>
|
</Form.Group>
|
||||||
<Form.Group>
|
<Form.Group>
|
||||||
<Form.Label>Ziel der Fahrt</Form.Label>
|
<Form.Label>
|
||||||
|
Ziel der Fahrt <Required />
|
||||||
|
</Form.Label>
|
||||||
<Form.Control
|
<Form.Control
|
||||||
type="text"
|
type="text"
|
||||||
name="destination"
|
name="destination"
|
||||||
placeholder="Fahrtziel"
|
placeholder="Fahrtziel"
|
||||||
value={destination}
|
value={destination}
|
||||||
onChange={onChangeEvent}
|
onChange={onChangeEvent}
|
||||||
|
required
|
||||||
/>
|
/>
|
||||||
</Form.Group>
|
</Form.Group>
|
||||||
</>
|
</>
|
||||||
|
|||||||
3
components/required.js
Normal file
3
components/required.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import react from 'react'
|
||||||
|
|
||||||
|
export default () => <span>*</span>
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useContext, useRef } from 'react'
|
import { useContext } from 'react'
|
||||||
|
|
||||||
import Button from 'react-bootstrap/Button'
|
import Button from 'react-bootstrap/Button'
|
||||||
import Form from 'react-bootstrap/Form'
|
import Form from 'react-bootstrap/Form'
|
||||||
@@ -11,7 +11,23 @@ import Contact from './contact'
|
|||||||
//import Driver from './driver'
|
//import Driver from './driver'
|
||||||
|
|
||||||
function WizardInternal() {
|
function WizardInternal() {
|
||||||
const { state, storeBooking } = useContext(WizardContext)
|
const { onSubmit, state } = useContext(WizardContext)
|
||||||
|
const { postData, postDataSuccess, postDataError } = state
|
||||||
|
|
||||||
|
if (postDataSuccess) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<h3>Danke, die Buchung ist in Bearbeitung!</h3>
|
||||||
|
<p>Wir melden uns per E-Mail sobald die Buchung bestätigt ist.</p>
|
||||||
|
<p>
|
||||||
|
Sollen die eingegebenen Daten in Deinem Browser für die nächste
|
||||||
|
Buchung gespeichert werden?
|
||||||
|
</p>
|
||||||
|
<Button>Ja, bitte speichern</Button>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const onChange = (payload) =>
|
const onChange = (payload) =>
|
||||||
dispatch({ action: ACTIONS.SET_FORM_DATA, payload })
|
dispatch({ action: ACTIONS.SET_FORM_DATA, payload })
|
||||||
|
|
||||||
@@ -19,14 +35,16 @@ function WizardInternal() {
|
|||||||
<Form
|
<Form
|
||||||
onSubmit={(event) => {
|
onSubmit={(event) => {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
onSubmit()
|
||||||
storeBooking()
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<DateSelect />
|
<DateSelect />
|
||||||
<Reason />
|
<Reason />
|
||||||
<Contact />
|
<Contact />
|
||||||
<Button type="submit">Absenden</Button>
|
<div>{postDataError}</div>
|
||||||
|
<Button type="submit" disabled={postData}>
|
||||||
|
{postData ? 'Speichern...' : 'Absenden'}
|
||||||
|
</Button>
|
||||||
</Form>
|
</Form>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ export const WizardContext = React.createContext()
|
|||||||
|
|
||||||
export const ACTIONS = {
|
export const ACTIONS = {
|
||||||
SET_FORM_DATA: 'setFormData',
|
SET_FORM_DATA: 'setFormData',
|
||||||
|
POST_DATA: 'postData',
|
||||||
|
POST_DATA_ERROR: 'postDataError',
|
||||||
|
POST_DATA_SUCCESS: 'postDataSuccess',
|
||||||
}
|
}
|
||||||
|
|
||||||
function reducer(state, action) {
|
function reducer(state, action) {
|
||||||
@@ -16,6 +19,31 @@ function reducer(state, action) {
|
|||||||
...action.payload,
|
...action.payload,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
case ACTIONS.POST_DATA:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
postData: true,
|
||||||
|
postDataError: null,
|
||||||
|
postDataSuccess: null,
|
||||||
|
}
|
||||||
|
case ACTIONS.POST_DATA_ERROR:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
postData: false,
|
||||||
|
postDataError: action.payload,
|
||||||
|
postDataSuccess: null,
|
||||||
|
}
|
||||||
|
case ACTIONS.POST_DATA_SUCCESS:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
formData: {
|
||||||
|
...state.formData,
|
||||||
|
...action.payload,
|
||||||
|
},
|
||||||
|
postData: false,
|
||||||
|
postDataError: null,
|
||||||
|
postDataSuccess: true,
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unkown Action type ${action.type}`)
|
throw new Error(`Unkown Action type ${action.type}`)
|
||||||
}
|
}
|
||||||
@@ -28,8 +56,26 @@ function debugReducer(state, action) {
|
|||||||
return newState
|
return newState
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createBooking(state) {
|
const initialState = {
|
||||||
const { name, email, startDate, endDate } = state.formData
|
postData: false,
|
||||||
|
postDataError: null,
|
||||||
|
postDataSuccess: null,
|
||||||
|
formData: {
|
||||||
|
multipleDays: true,
|
||||||
|
//startDate: '2020-08-10',
|
||||||
|
//endDate: '2020-08-17',
|
||||||
|
//purpose: 'Sommerlager 2021',
|
||||||
|
//org: 'VCP Rosenfeld',
|
||||||
|
//destination: 'Lüneburger Heide',
|
||||||
|
//name: 'Thomas Ruoff',
|
||||||
|
//email: 'thomasruoff@gmail.com',
|
||||||
|
//street: 'Mömpelgardgasse 25',
|
||||||
|
//zip: '72348',
|
||||||
|
//city: 'Rosenfeld',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createBooking(formData) {
|
||||||
const response = await fetch('/api/booking', {
|
const response = await fetch('/api/booking', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
mode: 'cors',
|
mode: 'cors',
|
||||||
@@ -39,23 +85,14 @@ async function createBooking(state) {
|
|||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
referrerPolicy: 'no-referrer',
|
referrerPolicy: 'no-referrer',
|
||||||
body: JSON.stringify({ name, email, startDate, endDate }),
|
body: JSON.stringify(formData),
|
||||||
})
|
})
|
||||||
|
|
||||||
return response.json()
|
return response.json()
|
||||||
}
|
}
|
||||||
|
|
||||||
const initialState = {
|
|
||||||
formData: {
|
|
||||||
multipleDays: null,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function WizardStore({ children }) {
|
export default function WizardStore({ children }) {
|
||||||
const [state, dispatch] = useReducer(debugReducer, initialState)
|
const [state, dispatch] = useReducer(debugReducer, initialState)
|
||||||
|
|
||||||
const storeBooking = () => createBooking(state)
|
|
||||||
|
|
||||||
const onChangeEvent = (event) => {
|
const onChangeEvent = (event) => {
|
||||||
const { name, value } = event.target
|
const { name, value } = event.target
|
||||||
|
|
||||||
@@ -72,9 +109,21 @@ export default function WizardStore({ children }) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const onSubmit = async () => {
|
||||||
|
dispatch({ type: ACTIONS.POST_DATA })
|
||||||
|
|
||||||
|
try {
|
||||||
|
const bookingData = await createBooking(state.formData)
|
||||||
|
dispatch({ type: ACTIONS.POST_DATA_SUCCESS, payload: bookingData })
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
dispatch({ type: ACTIONS.POST_DATA_ERROR, payload: error.message })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<WizardContext.Provider
|
<WizardContext.Provider
|
||||||
value={{ state, dispatch, onChangeEvent, onChange, storeBooking }}
|
value={{ state, dispatch, onChangeEvent, onChange, onSubmit }}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</WizardContext.Provider>
|
</WizardContext.Provider>
|
||||||
|
|||||||
Reference in New Issue
Block a user