From 4db2103b8750c71d7ab162307e4fcf3403ed81cf Mon Sep 17 00:00:00 2001 From: Zak Laberg Date: Fri, 30 Sep 2022 14:21:20 +0200 Subject: [PATCH] delete quote --- api/quote.ts | 19 ++++++++++++ components/deeplink/DeepLinkViewer.tsx | 36 ++++++++++++----------- pages/api/booking-domain/delete-quote.ts | 22 ++++++++++++++ pages/index.tsx | 37 ++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 17 deletions(-) create mode 100644 api/quote.ts create mode 100644 pages/api/booking-domain/delete-quote.ts diff --git a/api/quote.ts b/api/quote.ts new file mode 100644 index 0000000..b1d4cd1 --- /dev/null +++ b/api/quote.ts @@ -0,0 +1,19 @@ +const { BOOKING_DOMAIN_URL } = process.env; + +export const deleteQuote = async (quoteId: string) => { + const delete_ = (url: string) => + fetch(url, { + method: "DELETE", + }) + .then((resp) => { + if (resp.status.toString()[0] !== "2") { + throw new Error(`${resp.status} ${resp.statusText}`); + } + return resp; + }) + .then((resp) => resp.text()); + + const res = await delete_(`${BOOKING_DOMAIN_URL || ""}/Api/Quote/${quoteId}`); + + return res; +}; diff --git a/components/deeplink/DeepLinkViewer.tsx b/components/deeplink/DeepLinkViewer.tsx index 0be0f90..b834e3e 100644 --- a/components/deeplink/DeepLinkViewer.tsx +++ b/components/deeplink/DeepLinkViewer.tsx @@ -2,7 +2,7 @@ import { useEffect, useState } from "react"; import { Deeplink } from "../../util/deeplink"; import IconButton from "../IconButton"; import { ClipboardLine } from "../icons/Document"; -import { ButtonModes } from "../inputs/Button"; +import Button, { ButtonModes } from "../inputs/Button"; import TextInput from "../inputs/TextInput"; const DeepLinkViewer = ({ deeplink }: { deeplink: Deeplink | null }) => { @@ -28,23 +28,25 @@ const DeepLinkViewer = ({ deeplink }: { deeplink: Deeplink | null }) => { const baseUrl = `http://www.hurtigruten.com/${deeplink.locale}/expeditions/dl/`; return ( -
+

Your deeplink

- - - navigator.clipboard.writeText(`${baseUrl}${encodedLink}`) - } - /> +
+ + + navigator.clipboard.writeText(`${baseUrl}${encodedLink}`) + } + /> +
); }; diff --git a/pages/api/booking-domain/delete-quote.ts b/pages/api/booking-domain/delete-quote.ts new file mode 100644 index 0000000..0140c6c --- /dev/null +++ b/pages/api/booking-domain/delete-quote.ts @@ -0,0 +1,22 @@ +import { NextApiRequest, NextApiResponse } from "next"; + +import { deleteQuote } from "../../../api/quote"; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + if (req.method !== "GET") { + res.status(405).json("Unsupported method"); + return; + } + const { quoteId: quoteIdRaw } = req.query; + const quoteId = Array.isArray(quoteIdRaw) ? quoteIdRaw[0] : quoteIdRaw; + try { + await deleteQuote(quoteId); + res.status(200).json({ success: true }); + } catch (error) { + console.error(error); + res.status(500).json({ success: false }); + } +} diff --git a/pages/index.tsx b/pages/index.tsx index 4ace17e..718db96 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -10,6 +10,7 @@ import DepartureOptions, { import CabinOptions from "../components/deeplink/CabinOptions"; import Summary from "@components/Summary"; import { sumPassengersInCabin } from "@src/util/sumPassengersInCabin"; +import Button from "@components/inputs/Button"; const defaultDeepLink: Deeplink = { version: "1", @@ -32,6 +33,8 @@ const DeepLinkBuilder = () => { const [shipCodesForAvailableShips, setShipCodesForAvailableShips] = useState< string[] | null >(null); + const [isDeletingQuote, setIsDeletingQuote] = useState(false); + const [deletedQuoteIds, setDeletedQuoteIds] = useState([]); const onLocaleSelected = (locale: string) => { setDeeplink({ @@ -97,6 +100,11 @@ const DeepLinkBuilder = () => { departure, }, }); + + console.log( + selectedDep?.departure.quoteId, + selectedDep?.departure.voyageId + ); }; const summary = { @@ -129,8 +137,31 @@ const DeepLinkBuilder = () => { })) ?? null, }; + const deleteQuoteHandler = async () => { + setIsDeletingQuote(true); + try { + await fetch( + `/api/booking-domain/delete-quote?quoteId=${ + chosenDeparture?.departure.quoteId ?? "" + }` + ); + setDeletedQuoteIds([ + ...deletedQuoteIds, + chosenDeparture?.departure.quoteId ?? "", + ]); + } catch (e) { + console.log("Failed to delete quote", e); + } finally { + setIsDeletingQuote(false); + } + }; + return ( <> +
+ {chosenDeparture?.departure.quoteId} + {chosenDeparture?.departure.voyageId} +

Create deeplink

@@ -165,6 +196,12 @@ const DeepLinkBuilder = () => { {(deeplink.search || deeplink.cabins) && ( )} + {chosenDeparture?.departure.quoteId && + !deletedQuoteIds.includes(chosenDeparture.departure.quoteId) && ( + + )}