mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-04 15:07:13 +01:00
move CRUD operations to helpers
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import React, { useEffect, useReducer } from 'react'
|
import React, { useEffect, useReducer } from 'react'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import { clearBookingData, loadBookingData } from '../../../helpers/storage'
|
import { clearBookingData, loadBookingData } from '../../../helpers/storage'
|
||||||
import { ValidationError } from './validationError'
|
|
||||||
|
import { createBooking } from '../../../helpers/booking'
|
||||||
|
|
||||||
interface WizardFormData {
|
interface WizardFormData {
|
||||||
startDate: string
|
startDate: string
|
||||||
@@ -136,33 +137,6 @@ const initialState: WizardStoreState = {
|
|||||||
dataStoredLoaded: undefined,
|
dataStoredLoaded: undefined,
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createBooking(formData: WizardFormData) {
|
|
||||||
const response = await fetch('/api/booking', {
|
|
||||||
method: 'POST',
|
|
||||||
mode: 'cors',
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: 'same-origin',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
referrerPolicy: 'no-referrer',
|
|
||||||
body: JSON.stringify(formData),
|
|
||||||
})
|
|
||||||
|
|
||||||
if (response.status === 400) {
|
|
||||||
const error = await response.json()
|
|
||||||
throw new ValidationError(error.errors)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw Error(
|
|
||||||
'Sorry, konnte nicht gespeichert werden. Bitte versuch es später nochmal!'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.json()
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function WizardStore({ children }) {
|
export default function WizardStore({ children }) {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const [state, dispatch] = useReducer(reducer, initialState)
|
const [state, dispatch] = useReducer(reducer, initialState)
|
||||||
|
|||||||
@@ -1,11 +1,18 @@
|
|||||||
import { MILAGE_TARIFS } from '../db/enums'
|
import { MILAGE_TARIFS } from '../db/enums'
|
||||||
import { AdditionalCost } from '../db/bill'
|
import { AdditionalCost, Bill } from '../db/bill'
|
||||||
|
import fetch from './fetch'
|
||||||
|
|
||||||
function roundToCent(amount: number): number {
|
function roundToCent(amount: number): number {
|
||||||
return Math.round(amount * 100) / 100
|
return Math.round(amount * 100) / 100
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getMilageCosts({ tarif, km }: { tarif: MILAGE_TARIFS; km: number }): number {
|
export function getMilageCosts({
|
||||||
|
tarif,
|
||||||
|
km,
|
||||||
|
}: {
|
||||||
|
tarif: MILAGE_TARIFS
|
||||||
|
km: number
|
||||||
|
}): number {
|
||||||
if (tarif === MILAGE_TARIFS.NOCHARGE) {
|
if (tarif === MILAGE_TARIFS.NOCHARGE) {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@@ -63,3 +70,23 @@ export function getBillTotal({
|
|||||||
|
|
||||||
return roundToCent(milageCosts + additionalCostsSum)
|
return roundToCent(milageCosts + additionalCostsSum)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function createBill(
|
||||||
|
bookingUuid: string,
|
||||||
|
bill: Bill
|
||||||
|
): Promise<Bill> {
|
||||||
|
return fetch(`/api/admin/booking/${bookingUuid}/bill`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: bill,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function patchBill(
|
||||||
|
bookingUuid: string,
|
||||||
|
bill: Bill
|
||||||
|
): Promise<Bill> {
|
||||||
|
return fetch(`/api/admin/booking/${bookingUuid}/bill`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: bill,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { BOOKING_STATUS } from '../db/enums'
|
import { BOOKING_STATUS } from '../db/enums'
|
||||||
|
import fetch from './fetch'
|
||||||
|
|
||||||
export function getBookingStatus(status: BOOKING_STATUS) {
|
export function getBookingStatus(status: BOOKING_STATUS) {
|
||||||
switch (status) {
|
switch (status) {
|
||||||
@@ -14,3 +15,24 @@ export function getBookingStatus(status: BOOKING_STATUS) {
|
|||||||
return 'Unbekannt - bitte kontaktieren Sie uns!'
|
return 'Unbekannt - bitte kontaktieren Sie uns!'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function createBooking(formData: object) {
|
||||||
|
return fetch('/api/booking', {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function cancelBooking(uuid: string) {
|
||||||
|
return fetch(`/api/booking/${uuid}`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
body: { status: BOOKING_STATUS.CANCELED },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function patchBooking(uuid: string, bookingData: object) {
|
||||||
|
return fetch(`/api/admin/booking/${uuid}`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
body: { ...bookingData },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
40
helpers/fetch.ts
Normal file
40
helpers/fetch.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { ValidationError } from './validationError'
|
||||||
|
|
||||||
|
const DEFAULT_FETCH_OPTIONS = {
|
||||||
|
method: 'GET',
|
||||||
|
mode: 'cors' as RequestMode,
|
||||||
|
cache: 'no-cache' as RequestCache,
|
||||||
|
credentials: 'same-origin' as RequestCredentials,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
referrerPolicy: 'no-referrer' as ReferrerPolicy,
|
||||||
|
}
|
||||||
|
|
||||||
|
export type FetchOptions = Omit<RequestInit, 'body'> & {
|
||||||
|
body?: object
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function fetchJSON(
|
||||||
|
url: string,
|
||||||
|
options: FetchOptions = {}
|
||||||
|
) {
|
||||||
|
const response = await fetch(url, {
|
||||||
|
...DEFAULT_FETCH_OPTIONS,
|
||||||
|
...options,
|
||||||
|
body: !!options.body ? JSON.stringify(options.body) : undefined,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (response.status === 400) {
|
||||||
|
const error = await response.json()
|
||||||
|
throw new ValidationError(error.errors)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw Error(
|
||||||
|
'Sorry, konnte nicht gespeichert werden. Bitte versuch es später nochmal!'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.json()
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import useSWR from 'swr'
|
import useSWR from 'swr'
|
||||||
|
import fetch from './fetch'
|
||||||
|
|
||||||
const fetcher = (path: string) => fetch(path).then((r) => r.json())
|
const fetcher = (path: string) => fetch(path)
|
||||||
|
|
||||||
function useDaysBooked() {
|
function useDaysBooked() {
|
||||||
const { data: daysBooked, error: daysBookedError } = useSWR(
|
const { data: daysBooked, error: daysBookedError } = useSWR(
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { VALIDATION_ERRORS } from '../../../db/enums'
|
import { VALIDATION_ERRORS } from '../db/enums'
|
||||||
|
|
||||||
interface ValidationErrors {
|
interface ValidationErrors {
|
||||||
[key: string]: { properties: { message: string }; kind: string }
|
[key: string]: { properties: { message: string }; kind: string }
|
||||||
@@ -3,12 +3,11 @@ import Footer from '../../../../components/footer'
|
|||||||
import Header from '../../../../components/header'
|
import Header from '../../../../components/header'
|
||||||
import Input from '../../../../components/input'
|
import Input from '../../../../components/input'
|
||||||
import Select from '../../../../components/select'
|
import Select from '../../../../components/select'
|
||||||
import { AdditionalCost, Bill } from '../../../../db/bill'
|
|
||||||
import { Booking } from '../../../../db/booking'
|
import { Booking } from '../../../../db/booking'
|
||||||
import { BILL_STATUS, MILAGE_TARIFS } from '../../../../db/enums'
|
import { BILL_STATUS, MILAGE_TARIFS } from '../../../../db/enums'
|
||||||
import { getMilageMax } from '../../../../db/index'
|
import { getMilageMax } from '../../../../db/index'
|
||||||
import { daysFormatFrontend } from '../../../../helpers/date'
|
import { daysFormatFrontend } from '../../../../helpers/date'
|
||||||
import { getBillTotal } from '../../../../helpers/bill'
|
import { getBillTotal, createBill, patchBill } from '../../../../helpers/bill'
|
||||||
import { getBookingStatus } from '../../../../helpers/booking'
|
import { getBookingStatus } from '../../../../helpers/booking'
|
||||||
import withSession, {
|
import withSession, {
|
||||||
isAdminSession,
|
isAdminSession,
|
||||||
@@ -79,31 +78,6 @@ function getBillStatusLabel(status: BILL_STATUS) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function saveBill(
|
|
||||||
booking: Booking,
|
|
||||||
bill: {
|
|
||||||
milageStart: number
|
|
||||||
milageEnd: number
|
|
||||||
milage?: number
|
|
||||||
tarif: MILAGE_TARIFS
|
|
||||||
additionalCosts: AdditionalCost[]
|
|
||||||
status: BILL_STATUS
|
|
||||||
}
|
|
||||||
): Promise<Bill> {
|
|
||||||
const response = await fetch(`/api/admin/booking/${booking.uuid}/bill`, {
|
|
||||||
method: !!booking.bill ? 'PATCH' : 'POST',
|
|
||||||
mode: 'cors',
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: 'same-origin',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
referrerPolicy: 'no-referrer',
|
|
||||||
body: JSON.stringify(bill),
|
|
||||||
})
|
|
||||||
return response.json()
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function BookingBillPage({
|
export default function BookingBillPage({
|
||||||
booking: bookingProp,
|
booking: bookingProp,
|
||||||
milageMax,
|
milageMax,
|
||||||
@@ -136,7 +110,8 @@ export default function BookingBillPage({
|
|||||||
setStoringError(null)
|
setStoringError(null)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const bill = await saveBill(booking, {
|
const saveBill = !!booking.bill ? createBill : patchBill
|
||||||
|
const bill = await saveBill(booking.uuid, {
|
||||||
milageStart,
|
milageStart,
|
||||||
milageEnd,
|
milageEnd,
|
||||||
milage,
|
milage,
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import withSession, {
|
|||||||
} from '../../../../lib/session'
|
} from '../../../../lib/session'
|
||||||
import { getServerSideBooking } from '../../../../lib/getServerSideProps'
|
import { getServerSideBooking } from '../../../../lib/getServerSideProps'
|
||||||
import { Booking } from '../../../../db/booking'
|
import { Booking } from '../../../../db/booking'
|
||||||
import { getBookingStatus } from '../../../../helpers/booking'
|
import { getBookingStatus, patchBooking } from '../../../../helpers/booking'
|
||||||
import { daysFormatFrontend } from '../../../../helpers/date'
|
import { daysFormatFrontend } from '../../../../helpers/date'
|
||||||
import { BOOKING_STATUS } from '../../../../db/enums'
|
import { BOOKING_STATUS } from '../../../../db/enums'
|
||||||
|
|
||||||
@@ -36,21 +36,6 @@ export const getServerSideProps: GetServerSideProps = withSession(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
async function patchBooking(uuid: string, bookingData: any) {
|
|
||||||
const response = await fetch(`/api/admin/booking/${uuid}`, {
|
|
||||||
method: 'PATCH',
|
|
||||||
mode: 'cors',
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: 'same-origin',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
referrerPolicy: 'no-referrer',
|
|
||||||
body: JSON.stringify({ ...bookingData }),
|
|
||||||
})
|
|
||||||
return response.json()
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function ShowBookingAdmin({
|
export default function ShowBookingAdmin({
|
||||||
booking: bookingProp,
|
booking: bookingProp,
|
||||||
}: {
|
}: {
|
||||||
|
|||||||
@@ -5,25 +5,10 @@ import { getServerSideBooking } from '../../../lib/getServerSideProps'
|
|||||||
import { Booking } from '../../../db/booking'
|
import { Booking } from '../../../db/booking'
|
||||||
import { BOOKING_STATUS } from '../../../db/enums'
|
import { BOOKING_STATUS } from '../../../db/enums'
|
||||||
import { daysFormatFrontend } from '../../../helpers/date'
|
import { daysFormatFrontend } from '../../../helpers/date'
|
||||||
import { getBookingStatus } from '../../../helpers/booking'
|
import { getBookingStatus, cancelBooking } from '../../../helpers/booking'
|
||||||
|
|
||||||
export const getServerSideProps = getServerSideBooking
|
export const getServerSideProps = getServerSideBooking
|
||||||
|
|
||||||
async function cancelBooking(booking: Booking) {
|
|
||||||
const response = await fetch(`/api/booking/${booking.uuid}`, {
|
|
||||||
method: 'PATCH',
|
|
||||||
mode: 'cors',
|
|
||||||
cache: 'no-cache',
|
|
||||||
credentials: 'same-origin',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
referrerPolicy: 'no-referrer',
|
|
||||||
body: JSON.stringify({ status: BOOKING_STATUS.CANCELED }),
|
|
||||||
})
|
|
||||||
return response.json()
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function ShowBooking({
|
export default function ShowBooking({
|
||||||
booking: bookingProp,
|
booking: bookingProp,
|
||||||
}: {
|
}: {
|
||||||
@@ -44,7 +29,7 @@ export default function ShowBooking({
|
|||||||
try {
|
try {
|
||||||
setStoringBookingError(null)
|
setStoringBookingError(null)
|
||||||
setStoringBooking(true)
|
setStoringBooking(true)
|
||||||
const updatedBooking = await cancelBooking(booking)
|
const updatedBooking = await cancelBooking(booking.uuid)
|
||||||
setBooking(updatedBooking)
|
setBooking(updatedBooking)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setStoringBookingError(
|
setStoringBookingError(
|
||||||
|
|||||||
Reference in New Issue
Block a user