Skip to content

Commit

Permalink
exchange layout and market middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartosz Nowak committed Oct 22, 2023
1 parent baa5209 commit 202632b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
Empty file.
28 changes: 28 additions & 0 deletions src/routes/exchange/[market]/index.tsx
Original file line number Diff line number Diff line change
@@ -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 <></>;
});
12 changes: 12 additions & 0 deletions src/routes/exchange/[market]/parseMarket.ts
Original file line number Diff line number Diff line change
@@ -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");
}
};
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions src/routes/exchange/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { component$, Slot } from "@builder.io/qwik";

export default component$(() => {
return (
<>
<Slot />
</>
);
});

0 comments on commit 202632b

Please sign in to comment.