use types instead of interfaces

This commit is contained in:
Thomas Ruoff
2021-06-08 23:02:52 +02:00
parent dbe3904759
commit 498f212ee0
10 changed files with 56 additions and 71 deletions

View File

@@ -2,7 +2,7 @@ import React from 'react'
import InputWrapper from './inputWrapper'
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
type InputProps = React.InputHTMLAttributes<HTMLInputElement> & {
label: string
}

View File

@@ -3,27 +3,15 @@ import { useRouter } from 'next/router'
import { clearBookingData, loadBookingData } from '../helpers/storage'
import { createBooking } from '../helpers/booking'
import { Booker } from '../db/booker'
import { Booking } from '../db/booking'
interface BookFormData {
startDate: string
endDate: string
purpose: string
org: string
destination: string
name: string
email: string
phone: string
street: string
zip: string
city: string
export type BookFormData = Omit<Booking, 'uuid'> &
Booker & {
storeData?: boolean
}
interface Booking {
uuid: string
}
interface BookingStoreState {
type BookingProviderState = {
postData?: boolean
postDataError?: string
postDataSuccess?: boolean
@@ -34,8 +22,8 @@ interface BookingStoreState {
dataStoredLoaded: boolean
}
interface BookStore {
state: BookingStoreState
type BookProvider = {
state: BookingProviderState
dispatch: React.Dispatch<BookAction>
onChange: (data: object) => void
onChangeEvent: (event: React.ChangeEvent<React.ElementRef<'input'>>) => void
@@ -43,13 +31,13 @@ interface BookStore {
forgetData: () => void
}
interface BookAction {
type BookAction = {
type: string
payload?: any
}
export const BookContext: React.Context<BookStore> = React.createContext<
BookStore
export const BookContext: React.Context<BookProvider> = React.createContext<
BookProvider
>(null)
export const ACTIONS = {
@@ -61,7 +49,7 @@ export const ACTIONS = {
DATA_STORED_FORGOTTEN: 'dataStoredForgotten',
}
function reducer(state: BookingStoreState, action: BookAction) {
function reducer(state: BookingProviderState, action: BookAction) {
switch (action.type) {
case ACTIONS.SET_FORM_DATA:
return {
@@ -115,7 +103,7 @@ function reducer(state: BookingStoreState, action: BookAction) {
}
}
const initialState: BookingStoreState = {
const initialState: BookingProviderState = {
postData: false,
postDataError: null,
postDataSuccess: null,

View File

@@ -2,12 +2,12 @@ import * as mongoose from 'mongoose'
import { BILL_STATUS, MILAGE_TARIFS } from './enums'
import { getBillTotal } from '../helpers/bill'
export interface AdditionalCost {
export type AdditionalCost = {
name: string
value: number
}
export interface Bill {
export type Bill = {
milageStart: number
milageEnd: number
milage?: number
@@ -16,12 +16,11 @@ export interface Bill {
additionalCosts: AdditionalCost[]
}
export interface BillDocument
extends Bill,
mongoose.SchemaTimestampsConfig,
mongoose.Document { }
export type BillDocument = Bill &
mongoose.SchemaTimestampsConfig &
mongoose.Document
export interface BillModel extends mongoose.Model<BillDocument> { }
export type BillModel = mongoose.Model<BillDocument>
const BillSchema = new mongoose.Schema<BillDocument>(
{

View File

@@ -1,6 +1,6 @@
import * as mongoose from 'mongoose'
export interface Booker {
export type Booker = {
name: string
email: string
phone: string
@@ -9,12 +9,11 @@ export interface Booker {
city: string
}
export interface BookerDocument
extends Booker,
mongoose.SchemaTimestampsConfig,
mongoose.Document {}
export type BookerDocument = Booker &
mongoose.SchemaTimestampsConfig &
mongoose.Document
export interface BookerModel extends mongoose.Model<BookerDocument> {}
export type BookerModel = mongoose.Model<BookerDocument>
const BookerSchema = new mongoose.Schema<BookerDocument>(
{

View File

@@ -7,25 +7,24 @@ import { Booker } from './booker'
import { BOOKING_STATUS, VALIDATION_ERRORS } from './enums'
import { getBookedDays } from './index'
export interface Booking {
export type Booking = {
uuid: string
booker: Booker
bill: Bill
booker?: Booker
bill?: Bill
startDate: string
endDate: string
status: BOOKING_STATUS
status?: BOOKING_STATUS
purpose?: string
org?: string
destination?: string
days?: string[]
}
export interface BookingDocument
extends Booking,
mongoose.Document,
mongoose.SchemaTimestampsConfig { }
export type BookingDocument = Booking &
mongoose.Document &
mongoose.SchemaTimestampsConfig
export interface BookingModel extends mongoose.Model<BookingDocument> {
export type BookingModel = mongoose.Model<BookingDocument> & {
findBookedDays(uuidsToIngore?: string[]): Promise<string[]>
}
@@ -55,7 +54,8 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
validator: function (v: Date): boolean {
return v >= nowInTz()
},
message: (props: { value: Date }): string => `${props.value} is in the past`,
message: (props: { value: Date }): string =>
`${props.value} is in the past`,
},
},
endDate: {
@@ -66,7 +66,8 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
validator: function (v: Date): boolean {
return v >= nowInTz()
},
message: (props: { value: Date }): string => `${props.value} is in the past`,
message: (props: { value: Date }): string =>
`${props.value} is in the past`,
},
},
days: {

View File

@@ -1,11 +1,13 @@
import { Booking } from '../db/booking'
const BOOKING_DATA_KEY = 'pfadiBussleBookingData'
function getStorage() {
return localStorage
}
export function storeBookingData({ booker, ...booking }) {
const { name, email, street, zip, city } = booker
export function storeBookingData(booking: Booking) {
const { name, email, street, zip, city } = booking.booker
const { org } = booking
getStorage().setItem(

View File

@@ -1,6 +1,6 @@
import { VALIDATION_ERRORS } from '../db/enums'
interface ValidationErrors {
type ValidationErrors = {
[key: string]: { properties: { message: string }; kind: string }
}

View File

@@ -2,13 +2,13 @@ import { startOfYear } from 'date-fns'
import { nowInTz } from '../helpers/date'
import { getBookingByUUID, getBookings } from '../db/index'
export interface ServerSideBooking {
export type ServerSideBooking = {
props: {
booking: object
}
}
export interface ServerSideRecentBooking {
export type ServerSideRecentBooking = {
props: {
bookings: object[]
}

View File

@@ -5,7 +5,7 @@ export enum USER_ROLE {
ADMIN = 'admin',
}
export interface UserData {
export type UserData = {
username: string
role: USER_ROLE
}

View File

@@ -8,11 +8,7 @@ import { getServerSideBooking } from '../../../lib/getServerSideProps'
export const getServerSideProps = getServerSideBooking
export default function ShowBookingStored({
booking: booking,
}: {
booking: Booking
}) {
export default function ShowBookingStored({ booking }: { booking: Booking }) {
const [storedBookingData, setStoredBookingData] = useState(null)
const [bookingDataStored, setBookingDataStored] = useState(false)