From 498f212ee013b4bc3631025e26e0399350db094d Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Tue, 8 Jun 2021 23:02:52 +0200 Subject: [PATCH] use types instead of interfaces --- components/input.tsx | 2 +- context/book.tsx | 40 ++++++++++++--------------------- db/bill.ts | 21 +++++++++-------- db/booker.ts | 11 +++++---- db/booking.ts | 33 ++++++++++++++------------- helpers/storage.ts | 6 +++-- helpers/validationError.ts | 2 +- lib/getServerSideProps.ts | 4 ++-- lib/session.ts | 2 +- pages/booking/[uuid]/stored.tsx | 6 +---- 10 files changed, 56 insertions(+), 71 deletions(-) diff --git a/components/input.tsx b/components/input.tsx index d8baaab..dee9518 100644 --- a/components/input.tsx +++ b/components/input.tsx @@ -2,7 +2,7 @@ import React from 'react' import InputWrapper from './inputWrapper' -interface InputProps extends React.InputHTMLAttributes { +type InputProps = React.InputHTMLAttributes & { label: string } diff --git a/context/book.tsx b/context/book.tsx index f01d25b..7e3305f 100644 --- a/context/book.tsx +++ b/context/book.tsx @@ -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 - storeData?: boolean -} +export type BookFormData = Omit & + 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 onChange: (data: object) => void onChangeEvent: (event: React.ChangeEvent>) => void @@ -43,13 +31,13 @@ interface BookStore { forgetData: () => void } -interface BookAction { +type BookAction = { type: string payload?: any } -export const BookContext: React.Context = React.createContext< - BookStore +export const BookContext: React.Context = 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, diff --git a/db/bill.ts b/db/bill.ts index 6baf960..76f054b 100644 --- a/db/bill.ts +++ b/db/bill.ts @@ -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 { } +export type BillModel = mongoose.Model const BillSchema = new mongoose.Schema( { @@ -29,7 +28,7 @@ const BillSchema = new mongoose.Schema( type: Number, required: true, validate: { - validator: function(v: number): boolean { + validator: function (v: number): boolean { const bill = this as BillDocument return v <= bill.milageEnd @@ -43,7 +42,7 @@ const BillSchema = new mongoose.Schema( required: true, validate: { - validator: function(v: number): boolean { + validator: function (v: number): boolean { const bill = this as BillDocument return v >= bill.milageStart @@ -77,12 +76,12 @@ const BillSchema = new mongoose.Schema( } ) -BillSchema.virtual('milage').get(function(): number { +BillSchema.virtual('milage').get(function (): number { const bill = this as BillDocument return bill.milageEnd - bill.milageStart }) -BillSchema.virtual('total').get(function(): number { +BillSchema.virtual('total').get(function (): number { const bill = this as BillDocument return getBillTotal(bill) diff --git a/db/booker.ts b/db/booker.ts index 0f72290..d03006c 100644 --- a/db/booker.ts +++ b/db/booker.ts @@ -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 {} +export type BookerModel = mongoose.Model const BookerSchema = new mongoose.Schema( { diff --git a/db/booking.ts b/db/booking.ts index 4dc8f77..18f3dc5 100644 --- a/db/booking.ts +++ b/db/booking.ts @@ -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 { +export type BookingModel = mongoose.Model & { findBookedDays(uuidsToIngore?: string[]): Promise } @@ -52,10 +51,11 @@ const BookingSchema = new mongoose.Schema( required: true, get: dateFormatBackend, validate: { - validator: function(v: Date): boolean { + 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: { @@ -63,17 +63,18 @@ const BookingSchema = new mongoose.Schema( required: false, get: dateFormatBackend, validate: { - validator: function(v: Date): boolean { + 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: { type: [String], required: true, validate: { - validator: async function(days: string[]): Promise { + validator: async function (days: string[]): Promise { const booking = this as Booking const uuid = booking.uuid && [booking.uuid] const bookedDays = await getBookedDays(uuid) @@ -105,7 +106,7 @@ const BookingSchema = new mongoose.Schema( } ) -BookingSchema.pre('validate', function(next: () => void): void { +BookingSchema.pre('validate', function (next: () => void): void { const booking = this as BookingDocument booking.days = getDays({ startDate: new Date(booking.startDate), @@ -114,7 +115,7 @@ BookingSchema.pre('validate', function(next: () => void): void { next() }) -BookingSchema.static('findBookedDays', async function( +BookingSchema.static('findBookedDays', async function ( uuidsToIngore: string[] = [] ): Promise { const model = this as BookingModel diff --git a/helpers/storage.ts b/helpers/storage.ts index 7f7b444..d8df461 100644 --- a/helpers/storage.ts +++ b/helpers/storage.ts @@ -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( diff --git a/helpers/validationError.ts b/helpers/validationError.ts index 0dd4058..5fb306f 100644 --- a/helpers/validationError.ts +++ b/helpers/validationError.ts @@ -1,6 +1,6 @@ import { VALIDATION_ERRORS } from '../db/enums' -interface ValidationErrors { +type ValidationErrors = { [key: string]: { properties: { message: string }; kind: string } } diff --git a/lib/getServerSideProps.ts b/lib/getServerSideProps.ts index 432d187..55fc2eb 100644 --- a/lib/getServerSideProps.ts +++ b/lib/getServerSideProps.ts @@ -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[] } diff --git a/lib/session.ts b/lib/session.ts index 8d3ed88..7b75934 100644 --- a/lib/session.ts +++ b/lib/session.ts @@ -5,7 +5,7 @@ export enum USER_ROLE { ADMIN = 'admin', } -export interface UserData { +export type UserData = { username: string role: USER_ROLE } diff --git a/pages/booking/[uuid]/stored.tsx b/pages/booking/[uuid]/stored.tsx index b9ac301..537177c 100644 --- a/pages/booking/[uuid]/stored.tsx +++ b/pages/booking/[uuid]/stored.tsx @@ -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)