Skip to content

Commit

Permalink
Allow page move
Browse files Browse the repository at this point in the history
  • Loading branch information
Zokhoi committed Sep 11, 2023
1 parent 8d0f614 commit c9eb28f
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 6 deletions.
31 changes: 31 additions & 0 deletions framerail/src/lib/server/page/move.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { wellfetch } from "$lib/server/deepwell/index.ts"
import type { Optional } from "$lib/types.ts"

export async function pageMove(
siteId: number,
pageId: Optional<number>,
slug: string,
newSlug: string,
revisionComments: Optional<string>
): object {
let endpoint = "/page/move"
const response = await wellfetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
siteId,
page: pageId ?? slug,
newSlug,
userId: 1, // TODO: identify user session and pass the user to the API request
revisionComments
})
})

if (!response.ok) {
throw new Error("Unable to send data to server")
}

return response.json()
}
23 changes: 23 additions & 0 deletions framerail/src/lib/sigma-esque/sigma-esque.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@
}
}
.page-move-actions {
.page-move-button {
border-radius: 0.5em;
padding: 0.5em 1em;
}
}
@media (prefers-color-scheme: light) {
.sigma-esque-container {
background-color: #fff;
Expand Down Expand Up @@ -124,6 +131,14 @@
border: 1px solid #0006;
}
}
.page-move-actions {
.page-move-button {
background-color: #fff;
color: #111;
border: 1px solid #0006;
}
}
}
@media (prefers-color-scheme: dark) {
Expand Down Expand Up @@ -155,5 +170,13 @@
border: 1px solid #b6c2cf;
}
}
.page-move-actions {
.page-move-button {
background-color: #222;
color: #b6c2cf;
border: 1px solid #b6c2cf;
}
}
}
</style>
16 changes: 16 additions & 0 deletions framerail/src/routes/[slug]/[...extra]/+server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { pageDelete } from "$lib/server/page/delete"
import { pageEdit } from "$lib/server/page/edit"
import { pageMove } from "$lib/server/page/move"

export async function POST(event) {
let data = await event.request.formData()
Expand Down Expand Up @@ -42,3 +43,18 @@ export async function DELETE(event) {
let res = await pageDelete(siteId, pageId, slug, comments)
return new Response(JSON.stringify(res))
}

export async function PUT(event) {
let data = await event.request.formData()
let slug = event.params.slug

let pageIdVal = data.get("page-id")?.toString()
let pageId = pageIdVal ? parseInt(pageIdVal) : null
let siteId = parseInt(data.get("site-id")?.toString() ?? "1")
let comments = data.get("comments")?.toString() ?? ""
let newSlug = data.get("new-slug")?.toString()

let res = await pageMove(siteId, pageId, slug, newSlug, comments)

return new Response(JSON.stringify(res))
}
93 changes: 87 additions & 6 deletions framerail/src/routes/[slug]/[...extra]/page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<script lang="ts">
export let data
import { goto, invalidateAll } from "$app/navigation"
let showMoveAction = false
let moveInputNewSlugElem: HTMLInputElement
async function handleDelete() {
let fdata = new FormData()
Expand All @@ -14,13 +16,19 @@
}
function navigateEdit() {
goto(`/${data.page.slug}/edit`, {
let options: string[] = []
if (data.options.noRender) options.push("norender")
options = options.map((opt) => `/${opt}`)
goto(`/${data.page.slug}${options.join("")}/edit`, {
noScroll: true
})
}
function cancelEdit() {
goto(`/${data.page.slug}`, {
let options: string[] = []
if (data.options.noRender) options.push("norender")
options = options.map((opt) => `/${opt}`)
goto(`/${data.page.slug}${options.join("")}`, {
noScroll: true
})
}
Expand All @@ -38,6 +46,27 @@
noScroll: true
})
}
async function handleMove() {
let form = document.getElementById("page-move")
let fdata = new FormData(form)
let newSlug = fdata.get("new-slug")
if (!newSlug) {
moveInputNewSlugElem.style.outline = "1px solid red"
return
} else {
moveInputNewSlugElem.style.outline = ""
}
fdata.set("site-id", data.site.siteId)
fdata.set("page-id", data.page.pageId)
await fetch(`/${data.page.slug}`, {
method: "PUT",
body: fdata
})
goto(`/${newSlug}`, {
noScroll: true
})
}
</script>

<h1>UNTRANSLATED:Loaded page</h1>
Expand All @@ -53,7 +82,11 @@
<hr />

<div class="page-content">
{@html data.compiledHtml}
{#if data.options?.noRender}
UNTRANSLATED: Content not shown.
{:else}
{@html data.compiledHtml}
{/if}
</div>

<div class="page-tags-container">
Expand Down Expand Up @@ -109,6 +142,15 @@
</form>
{:else}
<div class="editor-actions">
<button
class="editor-button button-move clickable"
type="button"
on:click={() => {
$: showMoveAction = true
}}
>
UT:Move
</button>
<button
class="editor-button button-delete clickable"
type="button"
Expand All @@ -126,6 +168,41 @@
</div>
{/if}

{#if showMoveAction}
<form
id="page-move"
class="page-move"
method="PUT"
on:submit|preventDefault={handleMove}
>
<input
bind:this={moveInputNewSlugElem}
name="new-slug"
class="page-move-new-slug"
placeholder="new slug"
type="text"
/>
<div class="page-move-actions">
<button
class="page-move-button button-cancel clickable"
type="button"
on:click|stopPropagation={() => {
$: showMoveAction = false
}}
>
UT:Cancel
</button>
<button
class="page-move-button button-move clickable"
type="submit"
on:click|stopPropagation
>
UT:Move
</button>
</div>
</form>
{/if}

<style global lang="scss">
@keyframes spin {
from {
Expand All @@ -147,7 +224,9 @@
}
.page-content,
.page-tags-container {
.page-tags-container,
.editor-actions,
.page-move {
padding: 0 0 2em;
}
Expand All @@ -163,7 +242,8 @@
list-style: none;
}
.editor {
.editor,
.page-move {
width: 80vw;
display: flex;
flex-direction: column;
Expand All @@ -176,7 +256,8 @@
height: 60vh;
}
.editor-actions {
.editor-actions,
.page-move-actions {
width: 100%;
display: flex;
flex-direction: row;
Expand Down

0 comments on commit c9eb28f

Please sign in to comment.