From f066b4e5e45e677d6f1dc1accd7cbf0c47c603e0 Mon Sep 17 00:00:00 2001 From: Max Richter Date: Wed, 9 Aug 2023 15:47:37 +0200 Subject: [PATCH] feat: only show edit button when logged in --- components/HashTags.tsx | 7 +++++-- routes/api/resources.ts | 9 +++++++-- routes/articles/[name].tsx | 14 +++++++++----- routes/movies/[name].tsx | 12 ++++++++---- routes/recipes/[name].tsx | 12 ++++++++---- routes/series/[name].tsx | 14 +++++++++----- 6 files changed, 46 insertions(+), 22 deletions(-) diff --git a/components/HashTags.tsx b/components/HashTags.tsx index acf0438..2bf221b 100644 --- a/components/HashTags.tsx +++ b/components/HashTags.tsx @@ -5,9 +5,12 @@ export const HashTags = ({ tags }: { tags: string[] }) => { > {tags.map((t) => { return ( - + #{t} - + ); })} diff --git a/routes/api/resources.ts b/routes/api/resources.ts index 5f40497..20a815c 100644 --- a/routes/api/resources.ts +++ b/routes/api/resources.ts @@ -1,11 +1,16 @@ import { Handlers } from "$fresh/server.ts"; -import { BadRequestError } from "@lib/errors.ts"; +import { AccessDeniedError, BadRequestError } from "@lib/errors.ts"; import { getTypeSenseClient } from "@lib/typesense.ts"; import { json } from "@lib/helpers.ts"; import { extractHashTags } from "@lib/string.ts"; export const handler: Handlers = { - async GET(req, _ctx) { + async GET(req, ctx) { + const session = ctx.state.session; + if (!session) { + throw new AccessDeniedError(); + } + const url = new URL(req.url); let query = url.searchParams.get("q"); if (!query) { diff --git a/routes/articles/[name].tsx b/routes/articles/[name].tsx index f530cff..eac5d3d 100644 --- a/routes/articles/[name].tsx +++ b/routes/articles/[name].tsx @@ -11,13 +11,15 @@ import { RedirectSearchHandler } from "@islands/Search.tsx"; export const handler: Handlers
= { async GET(_, ctx) { - const movie = await getArticle(ctx.params.name); - return ctx.render(movie); + const article = await getArticle(ctx.params.name); + return ctx.render({ article, session: ctx.state.session }); }, }; -export default function Greet(props: PageProps
) { - const article = props.data; +export default function Greet( + props: PageProps<{ article: Article; session: Record }>, +) { + const { article, session } = props.data; const { author = "", date = "" } = article.meta; @@ -34,7 +36,9 @@ export default function Greet(props: PageProps
) { {article.tags.length > 0 && ( diff --git a/routes/movies/[name].tsx b/routes/movies/[name].tsx index 81af709..f381802 100644 --- a/routes/movies/[name].tsx +++ b/routes/movies/[name].tsx @@ -10,12 +10,14 @@ import { RedirectSearchHandler } from "@islands/Search.tsx"; export const handler: Handlers = { async GET(_, ctx) { const movie = await getMovie(ctx.params.name); - return ctx.render(movie); + return ctx.render({ movie, session: ctx.state.session }); }, }; -export default function Greet(props: PageProps) { - const movie = props.data; +export default function Greet( + props: PageProps<{ movie: Movie; session: Record }>, +) { + const { movie, session } = props.data; const { author = "", date = "" } = movie.meta; @@ -28,7 +30,9 @@ export default function Greet(props: PageProps) { {movie.tags.length > 0 && ( diff --git a/routes/recipes/[name].tsx b/routes/recipes/[name].tsx index b48134b..eca5cf3 100644 --- a/routes/recipes/[name].tsx +++ b/routes/recipes/[name].tsx @@ -11,12 +11,14 @@ import { KMenu } from "@islands/KMenu.tsx"; export const handler: Handlers = { async GET(_, ctx) { const recipe = await getRecipe(ctx.params.name); - return ctx.render(recipe); + return ctx.render({ recipe, session: ctx.state.session }); }, }; -export default function Greet(props: PageProps) { - const recipe = props.data; +export default function Greet( + props: PageProps<{ recipe: Recipe; session: Record }>, +) { + const { recipe, session } = props.data; const portion = recipe.meta?.portion; const amount = useSignal(portion || 1); @@ -36,7 +38,9 @@ export default function Greet(props: PageProps) {
diff --git a/routes/series/[name].tsx b/routes/series/[name].tsx index 4930a1d..10ac134 100644 --- a/routes/series/[name].tsx +++ b/routes/series/[name].tsx @@ -9,13 +9,15 @@ import { KMenu } from "@islands/KMenu.tsx"; export const handler: Handlers = { async GET(_, ctx) { - const series = await getSeries(ctx.params.name); - return ctx.render(series); + const serie = await getSeries(ctx.params.name); + return ctx.render({ serie, session: ctx.state.session }); }, }; -export default function Greet(props: PageProps) { - const serie = props.data; +export default function Greet( + props: PageProps<{ serie: Series; session: Record }>, +) { + const { serie, session } = props.data; const { author = "", date = "" } = serie.meta; @@ -28,7 +30,9 @@ export default function Greet(props: PageProps) { {serie.tags.length > 0 && (