Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add markdown marketing pages #50

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib/utils/apiNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const apiNames = {
BUCKET: <bucketName extends string>(name: bucketName) =>
crud(`fileHandler:${name.substring(4)}`), // remove "dev-" prefix
},
MARKDOWN: <markdownDocumentName extends string>(name: markdownDocumentName) =>
crud(`markdowns:${name}`),
MEMBER: {
...crud("core:member"),
PING: "core:member:ping",
Expand Down
17 changes: 17 additions & 0 deletions src/routes/info/[slug]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { PageServerLoad } from "../$types";
import prisma from "$lib/utils/prisma";
import { error } from "@sveltejs/kit";

export const load: PageServerLoad = async ({ params }) => {
const markdownPage = await prisma.markdown.findUnique({
where: {
name: params.slug,
},
});
if (markdownPage == undefined) {
throw error(404, {
message: "Not found",
});
}
return { markdown: markdownPage, slug: params.slug };
};
19 changes: 19 additions & 0 deletions src/routes/info/[slug]/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script lang="ts">
export let data;
import { goto } from "$app/navigation";
import MarkdownBody from "$lib/components/MarkdownBody.svelte";
</script>

{#if data && data.accessPolicies.includes(`markdowns:${data.slug}:update`)}
trilleplay marked this conversation as resolved.
Show resolved Hide resolved
<button
type="button"
class="btn"
on:click={() => {
goto(`${data.slug}/edit`);
}}>Edit</button
>
{/if}

<div class="p-2 text-neutral-content">
<MarkdownBody body={data.markdown?.markdown} />
</div>
55 changes: 55 additions & 0 deletions src/routes/info/[slug]/edit/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Prisma } from "@prisma/client";
import { error, fail, redirect } from "@sveltejs/kit";
import prisma from "$lib/utils/prisma";
import { policyAccessGuard, withAccess } from "$lib/utils/access";
import apiNames from "$lib/utils/apiNames";
import type { PageServerLoad } from "./$types";

export const load: PageServerLoad = async ({ parent, params }) => {
const markdownPage = await prisma.markdown.findUnique({
where: {
name: params.slug,
},
});
if (markdownPage == undefined) {
throw error(404, {
message: "Not found",
});
}
const { accessPolicies } = await parent();
policyAccessGuard(apiNames.MARKDOWN(params.slug).UPDATE, accessPolicies);
return { markdown: markdownPage, slug: params.slug };
};

export const actions = {
default: async ({ request, locals }) => {
// read the form data sent by the browser
const formData = await request.formData();
const name = String(formData.get("name"));
const session = await locals.getSession();
return withAccess(apiNames.MARKDOWN(name).UPDATE, session?.user, async () => {
try {
await prisma.markdown.update({
where: {
name: name,
},
data: {
markdown: String(formData.get("markdown")),
},
});
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
return fail(400, {
error: e.message,
data: Object.fromEntries(formData),
});
}
return fail(500, {
error: "Unknown error",
data: Object.fromEntries(formData),
});
}
throw redirect(303, `/info/${name}`);
});
},
};
14 changes: 14 additions & 0 deletions src/routes/info/[slug]/edit/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script lang="ts">
export let data;
import MarkdownEditor from "$lib/components/MarkdownEditor.svelte";
let markdown = data.markdown?.markdown;
</script>

<div class="p-2 text-neutral-content">
<form method="POST">
<MarkdownEditor bind:value={markdown} />
<input type="hidden" name="name" value={data.slug} />
<input type="hidden" name="markdown" bind:value={markdown} />
<button class="btn" type="submit">Submit</button>
</form>
</div>