Skip to content

Commit

Permalink
Change sent data
Browse files Browse the repository at this point in the history
Send added or removed parents instead of old and new parents
  • Loading branch information
Zokhoi committed Sep 24, 2024
1 parent b0ae2dc commit 0869feb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
4 changes: 2 additions & 2 deletions framerail/src/lib/server/deepwell/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ export async function pageParentUpdate(
siteId: number,
pageId: number,
userId: number,
add: string[],
remove: string[]
add: Optional<string[]>,
remove: Optional<string[]>
): Promise<object> {
return client.request("parent_update", {
site_id: siteId,
Expand Down
29 changes: 13 additions & 16 deletions framerail/src/routes/[slug]/[...extra]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,19 @@ export async function POST(event) {

res = await page.pageLayout(siteId, pageId, session?.user_id, layout)
} else if (extra.includes("parent-set")) {
let oldParentStr = data.get("old-parents")?.toString().trim() ?? ""
let oldParent = oldParentStr.split(" ")
let newParentStr = data.get("new-parents")?.toString().trim() ?? ""
let newParent = newParentStr.split(" ")
let removed: string[] = []
let common: string[] = []
let added: string[] = []
for (let i = 0; i < oldParent.length; i++) {
if (!newParent.includes(oldParent[i])) removed.push(oldParent[i])
else common.push(oldParent[i])
}
for (let i = 0; i < newParent.length; i++) {
if (!common.includes(newParent[i])) added.push(newParent[i])
}

res = await page.pageParentUpdate(siteId, pageId, session?.user_id, added, removed)
let addParentStr = data.get("add-parents")?.toString().trim() ?? ""
let addParents = addParentStr.split(" ").filter((p) => p)
let removeParentStr = data.get("remove-parents")?.toString().trim() ?? ""
let removeParents = removeParentStr.split(" ").filter((p) => p)

if (addParents.length + removeParents.length)
res = await page.pageParentUpdate(
siteId,
pageId,
session?.user_id,
addParents.length ? addParents : undefined,
removeParents.length ? removeParents : undefined
)
} else if (extra.includes("parent-get")) {
res = await page.pageParentGet(siteId, pageId, slug)
} else if (extra.includes("score")) {
Expand Down
18 changes: 16 additions & 2 deletions framerail/src/routes/[slug]/[...extra]/page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,21 @@
let fdata = new FormData(form)
fdata.set("site-id", $page.data.site.site_id)
fdata.set("page-id", $page.data.page.page_id)
fdata.set("old-parents", parents)
let newParents = (fdata.get("parents")?.toString() ?? "").split(" ").filter((p) => p)
let oldParents = parents.split(" ").filter((p) => p)
let added: string[] = []
let removed: string[] = []
let common: string[] = []
for (let i = 0; i < oldParents.length; i++) {
if (!newParents.includes(oldParents[i])) removed.push(oldParents[i])
else common.push(oldParents[i])
}
for (let i = 0; i < newParents.length; i++) {
if (!common.includes(newParents[i])) added.push(newParents[i])
}
if (added.length) fdata.set("add-parents", added.join(" "))
if (removed.length) fdata.set("remove-parents", removed.join(" "))
let res = await fetch(`/${$page.data.page.slug}/parent-set`, {
method: "POST",
body: fdata
Expand Down Expand Up @@ -574,7 +588,7 @@
on:submit|preventDefault={setParents}
>
<input
name="new-parents"
name="parents"
class="page-parent-new-parents"
placeholder={$page.data.internationalization?.parents}
type="text"
Expand Down

0 comments on commit 0869feb

Please sign in to comment.