From 202632b3cb192c054cd9e629245d1bdfe331b379 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Sun, 22 Oct 2023 21:07:17 +0200 Subject: [PATCH] exchange layout and market middleware --- src/routes/exchange/[market]/index.css.ts | 0 src/routes/exchange/[market]/index.tsx | 28 +++++++++++++++++++++ src/routes/exchange/[market]/parseMarket.ts | 12 +++++++++ src/routes/exchange/index.css.ts | 0 src/routes/exchange/layout.css.ts | 0 src/routes/exchange/layout.tsx | 9 +++++++ 6 files changed, 49 insertions(+) create mode 100644 src/routes/exchange/[market]/index.css.ts create mode 100644 src/routes/exchange/[market]/index.tsx create mode 100644 src/routes/exchange/[market]/parseMarket.ts create mode 100644 src/routes/exchange/index.css.ts create mode 100644 src/routes/exchange/layout.css.ts create mode 100644 src/routes/exchange/layout.tsx diff --git a/src/routes/exchange/[market]/index.css.ts b/src/routes/exchange/[market]/index.css.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/routes/exchange/[market]/index.tsx b/src/routes/exchange/[market]/index.tsx new file mode 100644 index 0000000..40fedf4 --- /dev/null +++ b/src/routes/exchange/[market]/index.tsx @@ -0,0 +1,28 @@ +import { component$ } from "@builder.io/qwik"; +import { type RequestHandler, routeLoader$ } from "@builder.io/qwik-city"; +import { parseMarket } from "./parseMarket"; + +export const onRequest: RequestHandler = ({ params, redirect, sharedMap }) => { + try { + const market = parseMarket(params.market); + sharedMap.set("quoteAsset", market.quoteAsset); + sharedMap.set("baseAsset", market.baseAsset); + } catch (error) { + redirect(300, "/"); + } +}; + +export const useMarket = routeLoader$(({ sharedMap }) => { + return { + quoteAsset: sharedMap.get("quoteAsset") as string, + baseAsset: sharedMap.get("baseAsset") as string, + }; +}); + +export default component$(() => { + const { quoteAsset, baseAsset } = useMarket().value; + console.log(quoteAsset); + console.log(baseAsset); + + return <>; +}); diff --git a/src/routes/exchange/[market]/parseMarket.ts b/src/routes/exchange/[market]/parseMarket.ts new file mode 100644 index 0000000..2235f1e --- /dev/null +++ b/src/routes/exchange/[market]/parseMarket.ts @@ -0,0 +1,12 @@ +export const parseMarket = (market: string) => { + const regex = /^(\w+)-(\w+)$/; + const match = market.match(regex); + if (match) { + return { + quoteAsset: match[1].toUpperCase(), + baseAsset: match[2].toUpperCase(), + }; + } else { + throw new Error("Cound not parse market"); + } +}; diff --git a/src/routes/exchange/index.css.ts b/src/routes/exchange/index.css.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/routes/exchange/layout.css.ts b/src/routes/exchange/layout.css.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/routes/exchange/layout.tsx b/src/routes/exchange/layout.tsx new file mode 100644 index 0000000..8503e9b --- /dev/null +++ b/src/routes/exchange/layout.tsx @@ -0,0 +1,9 @@ +import { component$, Slot } from "@builder.io/qwik"; + +export default component$(() => { + return ( + <> + + + ); +});