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' import InputWrapper from './inputWrapper'
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> { type InputProps = React.InputHTMLAttributes<HTMLInputElement> & {
label: string label: string
} }

View File

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

View File

@@ -2,12 +2,12 @@ import * as mongoose from 'mongoose'
import { BILL_STATUS, MILAGE_TARIFS } from './enums' import { BILL_STATUS, MILAGE_TARIFS } from './enums'
import { getBillTotal } from '../helpers/bill' import { getBillTotal } from '../helpers/bill'
export interface AdditionalCost { export type AdditionalCost = {
name: string name: string
value: number value: number
} }
export interface Bill { export type Bill = {
milageStart: number milageStart: number
milageEnd: number milageEnd: number
milage?: number milage?: number
@@ -16,12 +16,11 @@ export interface Bill {
additionalCosts: AdditionalCost[] additionalCosts: AdditionalCost[]
} }
export interface BillDocument export type BillDocument = Bill &
extends Bill, mongoose.SchemaTimestampsConfig &
mongoose.SchemaTimestampsConfig, mongoose.Document
mongoose.Document { }
export interface BillModel extends mongoose.Model<BillDocument> { } export type BillModel = mongoose.Model<BillDocument>
const BillSchema = new mongoose.Schema<BillDocument>( const BillSchema = new mongoose.Schema<BillDocument>(
{ {
@@ -29,7 +28,7 @@ const BillSchema = new mongoose.Schema<BillDocument>(
type: Number, type: Number,
required: true, required: true,
validate: { validate: {
validator: function(v: number): boolean { validator: function (v: number): boolean {
const bill = this as BillDocument const bill = this as BillDocument
return v <= bill.milageEnd return v <= bill.milageEnd
@@ -43,7 +42,7 @@ const BillSchema = new mongoose.Schema<BillDocument>(
required: true, required: true,
validate: { validate: {
validator: function(v: number): boolean { validator: function (v: number): boolean {
const bill = this as BillDocument const bill = this as BillDocument
return v >= bill.milageStart return v >= bill.milageStart
@@ -77,12 +76,12 @@ const BillSchema = new mongoose.Schema<BillDocument>(
} }
) )
BillSchema.virtual('milage').get(function(): number { BillSchema.virtual('milage').get(function (): number {
const bill = this as BillDocument const bill = this as BillDocument
return bill.milageEnd - bill.milageStart return bill.milageEnd - bill.milageStart
}) })
BillSchema.virtual('total').get(function(): number { BillSchema.virtual('total').get(function (): number {
const bill = this as BillDocument const bill = this as BillDocument
return getBillTotal(bill) return getBillTotal(bill)

View File

@@ -1,6 +1,6 @@
import * as mongoose from 'mongoose' import * as mongoose from 'mongoose'
export interface Booker { export type Booker = {
name: string name: string
email: string email: string
phone: string phone: string
@@ -9,12 +9,11 @@ export interface Booker {
city: string city: string
} }
export interface BookerDocument export type BookerDocument = Booker &
extends Booker, mongoose.SchemaTimestampsConfig &
mongoose.SchemaTimestampsConfig, mongoose.Document
mongoose.Document {}
export interface BookerModel extends mongoose.Model<BookerDocument> {} export type BookerModel = mongoose.Model<BookerDocument>
const BookerSchema = new mongoose.Schema<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 { BOOKING_STATUS, VALIDATION_ERRORS } from './enums'
import { getBookedDays } from './index' import { getBookedDays } from './index'
export interface Booking { export type Booking = {
uuid: string uuid: string
booker: Booker booker?: Booker
bill: Bill bill?: Bill
startDate: string startDate: string
endDate: string endDate: string
status: BOOKING_STATUS status?: BOOKING_STATUS
purpose?: string purpose?: string
org?: string org?: string
destination?: string destination?: string
days?: string[] days?: string[]
} }
export interface BookingDocument export type BookingDocument = Booking &
extends Booking, mongoose.Document &
mongoose.Document, mongoose.SchemaTimestampsConfig
mongoose.SchemaTimestampsConfig { }
export interface BookingModel extends mongoose.Model<BookingDocument> { export type BookingModel = mongoose.Model<BookingDocument> & {
findBookedDays(uuidsToIngore?: string[]): Promise<string[]> findBookedDays(uuidsToIngore?: string[]): Promise<string[]>
} }
@@ -52,10 +51,11 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
required: true, required: true,
get: dateFormatBackend, get: dateFormatBackend,
validate: { validate: {
validator: function(v: Date): boolean { validator: function (v: Date): boolean {
return v >= nowInTz() 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: { endDate: {
@@ -63,17 +63,18 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
required: false, required: false,
get: dateFormatBackend, get: dateFormatBackend,
validate: { validate: {
validator: function(v: Date): boolean { validator: function (v: Date): boolean {
return v >= nowInTz() 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: { days: {
type: [String], type: [String],
required: true, required: true,
validate: { validate: {
validator: async function(days: string[]): Promise<boolean> { validator: async function (days: string[]): Promise<boolean> {
const booking = this as Booking const booking = this as Booking
const uuid = booking.uuid && [booking.uuid] const uuid = booking.uuid && [booking.uuid]
const bookedDays = await getBookedDays(uuid) const bookedDays = await getBookedDays(uuid)
@@ -105,7 +106,7 @@ const BookingSchema = new mongoose.Schema<BookingDocument>(
} }
) )
BookingSchema.pre('validate', function(next: () => void): void { BookingSchema.pre('validate', function (next: () => void): void {
const booking = this as BookingDocument const booking = this as BookingDocument
booking.days = getDays({ booking.days = getDays({
startDate: new Date(booking.startDate), startDate: new Date(booking.startDate),
@@ -114,7 +115,7 @@ BookingSchema.pre('validate', function(next: () => void): void {
next() next()
}) })
BookingSchema.static('findBookedDays', async function( BookingSchema.static('findBookedDays', async function (
uuidsToIngore: string[] = [] uuidsToIngore: string[] = []
): Promise<string[]> { ): Promise<string[]> {
const model = this as BookingModel const model = this as BookingModel

View File

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

View File

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

View File

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

View File

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

View File

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