fix most type errors

still have a few things outstanding
This commit is contained in:
Thomas Ruoff
2020-08-28 00:29:20 +02:00
committed by Thomas Ruoff
parent c3d8c6f3e0
commit 90ac05a907
16 changed files with 279 additions and 96 deletions

View File

@@ -1,4 +1,5 @@
import React, { useReducer, useEffect } from 'react'
import * as React from 'react'
import { useReducer, useEffect } from 'react'
import {
storeBookingData,
@@ -6,8 +7,52 @@ import {
clearBookingData,
} from '../../../helpers/storage'
// @ts-expect-error ts-migrate(2554) FIXME: Expected 1 arguments, but got 0.
export const WizardContext = React.createContext()
interface WizardFormData {
startDate: string
endDate: string
purpose: string
org: string
destination: string
name: string
email: string
street: string
zip: string
city: string
}
interface Booking {
uuid: string
}
interface WizardStoreState {
postData?: boolean
postDataError?: string
postDataSuccess?: boolean
formData: WizardFormData
formDataChanged: string[]
booking?: Booking
dataStored: boolean
dataStoredLoaded: boolean
}
interface WizardStore {
state: WizardStoreState
dispatch: React.Dispatch<WizardAction>
onChange: (data: object) => void
onChangeEvent: (event: React.ChangeEvent<React.ElementRef<'input'>>) => void
onSubmit: () => void
storeData: (value: boolean) => void
forgetData: () => void
}
interface WizardAction {
type: string
payload?: any
}
export const WizardContext: React.Context<WizardStore> = React.createContext<
WizardStore
>(null)
export const ACTIONS = {
SET_FORM_DATA: 'setFormData',
@@ -19,7 +64,7 @@ export const ACTIONS = {
DATA_STORED_FORGOTTEN: 'dataStoredForgotten',
}
function reducer(state, action) {
function reducer(state: WizardStoreState, action: WizardAction) {
switch (action.type) {
case ACTIONS.SET_FORM_DATA:
return {
@@ -84,14 +129,7 @@ function reducer(state, action) {
}
}
function debugReducer(state, action) {
console.debug('applying action', action)
const newState = reducer(state, action)
console.debug(newState)
return newState
}
const initialState = {
const initialState: WizardStoreState = {
postData: false,
postDataError: null,
postDataSuccess: null,
@@ -108,9 +146,11 @@ const initialState = {
city: '',
},
formDataChanged: [],
dataStored: undefined,
dataStoredLoaded: undefined,
}
async function createBooking(formData) {
async function createBooking(formData: WizardFormData) {
const response = await fetch('/api/booking', {
method: 'POST',
mode: 'cors',
@@ -126,7 +166,7 @@ async function createBooking(formData) {
}
export default function WizardStore({ children }) {
const [state, dispatch] = useReducer(debugReducer, initialState)
const [state, dispatch] = useReducer(reducer, initialState)
useEffect(() => {
const data = loadBookingData()
@@ -135,7 +175,9 @@ export default function WizardStore({ children }) {
}
}, [])
const onChangeEvent = (event) => {
const onChangeEvent = (
event: React.ChangeEvent<React.ElementRef<'input'>>
) => {
const { name, value } = event.target
dispatch({
@@ -144,7 +186,7 @@ export default function WizardStore({ children }) {
})
}
const onChange = (data) => {
const onChange = (data: object) => {
dispatch({
type: ACTIONS.SET_FORM_DATA,
payload: data,
@@ -163,7 +205,7 @@ export default function WizardStore({ children }) {
}
}
const storeData = (value) => {
const storeData = (value: boolean) => {
if (value) {
storeBookingData(state.booking)
}