Skip to content

Commit

Permalink
Add viewing page history
Browse files Browse the repository at this point in the history
  • Loading branch information
Zokhoi committed Sep 25, 2023
1 parent 7f7203c commit 672deda
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 28 deletions.
28 changes: 28 additions & 0 deletions framerail/src/lib/server/page/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { wellfetch } from "$lib/server/deepwell/index.ts"
import type { Optional } from "$lib/types.ts"

export async function pageHistory(
siteId: number,
pageId: Optional<number>,
slug: string
): Promise<object> {
const response = await wellfetch("/page/revision/range", {
method: "PUT",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
siteId,
pageId: pageId,
revisionNumber: -1,
revisionDirection: "before",
limit: 200
})
})

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

return response.json()
}
11 changes: 11 additions & 0 deletions framerail/src/lib/server/page/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { pageDelete } from "$lib/server/page/delete"
import { pageEdit } from "$lib/server/page/edit"
import { pageMove } from "$lib/server/page/move"
import { pageHistory } from "$lib/server/page/history"

export {
pageDelete,
pageEdit,
pageMove,
pageHistory
}
2 changes: 1 addition & 1 deletion framerail/src/routes/+error.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
let fdata = new FormData(form)
fdata.set("site-id", $page.error.site.siteId)
fdata.set("slug", $page.params.slug)
await fetch(`/${$page.params.slug}`, {
await fetch(`/${$page.params.slug}/edit`, {
method: "POST",
body: fdata
})
Expand Down
65 changes: 40 additions & 25 deletions framerail/src/routes/[slug]/[...extra]/+server.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
import { pageDelete } from "$lib/server/page/delete"
import { pageEdit } from "$lib/server/page/edit"
import { pageMove } from "$lib/server/page/move"
import * as page from "$lib/server/page"

/**
* Edit or create page.
*/
export async function POST(event) {
let data = await event.request.formData()
let slug = event.params.slug

let extra = event.params.extra?.toLowerCase().split("/").filter((flag) => flag.length)

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 wikitext = data.get("wikitext")?.toString()
let title = data.get("title")?.toString()
let altTitle = data.get("alt-title")?.toString()
let tagsStr = data.get("tags")?.toString().trim()
let tags: string[] = []
if (tagsStr?.length) tags = tagsStr.split(" ").filter((tag) => tag.length)

let res = await pageEdit(
siteId,
pageId,
slug,
comments,
wikitext,
title,
altTitle,
tags
)
let res: object = {};

if (extra.includes("edit")) {
/**
* Edit or create page.
*/
let comments = data.get("comments")?.toString() ?? ""
let wikitext = data.get("wikitext")?.toString()
let title = data.get("title")?.toString()
let altTitle = data.get("alt-title")?.toString()
let tagsStr = data.get("tags")?.toString().trim()
let tags: string[] = []
if (tagsStr?.length) tags = tagsStr.split(" ").filter((tag) => tag.length)

res = await page.pageEdit(
siteId,
pageId,
slug,
comments,
wikitext,
title,
altTitle,
tags
)

} else if (extra.includes("history")) {
/**
* Retrieve page revision list.
*/
res = await page.pageHistory(
siteId,
pageId,
slug
)
}

return new Response(JSON.stringify(res))
}
Expand All @@ -46,7 +61,7 @@ export async function DELETE(event) {
let siteId = parseInt(data.get("site-id")?.toString() ?? "1")
let comments = data.get("comments")?.toString() ?? ""

let res = await pageDelete(siteId, pageId, slug, comments)
let res = await page.pageDelete(siteId, pageId, slug, comments)
return new Response(JSON.stringify(res))
}

Expand All @@ -63,7 +78,7 @@ export async function PUT(event) {
let comments = data.get("comments")?.toString() ?? ""
let newSlug = data.get("new-slug")?.toString()

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

return new Response(JSON.stringify(res))
}
75 changes: 73 additions & 2 deletions framerail/src/routes/[slug]/[...extra]/page.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<script lang="ts">
export let data
import { goto, invalidateAll } from "$app/navigation"
let showMoveAction = false
$: showMoveAction = false
$: showHistory = false
let moveInputNewSlugElem: HTMLInputElement
let revisionList = [];
async function handleDelete() {
let fdata = new FormData()
Expand Down Expand Up @@ -38,7 +40,7 @@
let fdata = new FormData(form)
fdata.set("site-id", data.site.siteId)
fdata.set("page-id", data.page.pageId)
await fetch(`/${data.page.slug}`, {
await fetch(`/${data.page.slug}/edit`, {
method: "POST",
body: fdata
})
Expand Down Expand Up @@ -68,6 +70,17 @@
})
$: showMoveAction = false
}
async function handleHistory() {
let fdata = new FormData()
fdata.set("site-id", data.site.siteId)
fdata.set("page-id", data.page.pageId)
revisionList = await fetch(`/${data.page.slug}/history`, {
method: "POST",
body: fdata
}).then(res=>res.json())
$: showHistory = true
}
</script>

<h1>UNTRANSLATED:Loaded page</h1>
Expand Down Expand Up @@ -172,6 +185,15 @@
UT:Edit
</button>
</div>
<div class="action-row other-actions">
<button
class="action-button button-history clickable"
type="button"
on:click={handleHistory}
>
UT:History
</button>
</div>
{/if}

{#if showMoveAction}
Expand Down Expand Up @@ -214,6 +236,41 @@
</form>
{/if}

{#if showHistory}
<div class="revision-list">
<div class="revision-header">
<div class="revision-attribute revision-number">
Revision #
</div>
<div class="revision-attribute created-at">
Created
</div>
<div class="revision-attribute user">
User
</div>
<div class="revision-attribute comments">
Comments
</div>
</div>
{#each revisionList.reverse() as revision}
<div class="revision-row" data-id={revision.revision_id}>
<div class="revision-attribute revision-number">
{revision.revision_number}
</div>
<div class="revision-attribute created-at">
{revision.created_at}
</div>
<div class="revision-attribute user">
{revision.user_id}
</div>
<div class="revision-attribute comments">
{revision.comments}
</div>
</div>
{/each}
</div>
{/if}

<style global lang="scss">
@keyframes spin {
from {
Expand Down Expand Up @@ -275,4 +332,18 @@
align-items: stretch;
gap: 10px;
}
.revision-list {
display: table;
width: 100%;
.revision-header,
.revision-row {
display: table-row;
.revision-attribute {
display: table-cell;
}
}
}
</style>

0 comments on commit 672deda

Please sign in to comment.