From 0869febf3ef7c23331aec0653a54b5f53b48cf8f Mon Sep 17 00:00:00 2001 From: Zokhoi <20432565+Zokhoi@users.noreply.github.com> Date: Wed, 25 Sep 2024 05:46:18 +0800 Subject: [PATCH] Change sent data Send added or removed parents instead of old and new parents --- framerail/src/lib/server/deepwell/page.ts | 4 +-- .../src/routes/[slug]/[...extra]/+server.ts | 29 +++++++++---------- .../src/routes/[slug]/[...extra]/page.svelte | 18 ++++++++++-- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/framerail/src/lib/server/deepwell/page.ts b/framerail/src/lib/server/deepwell/page.ts index 44b6bfc9d1..63f0f3a679 100644 --- a/framerail/src/lib/server/deepwell/page.ts +++ b/framerail/src/lib/server/deepwell/page.ts @@ -170,8 +170,8 @@ export async function pageParentUpdate( siteId: number, pageId: number, userId: number, - add: string[], - remove: string[] + add: Optional, + remove: Optional ): Promise { return client.request("parent_update", { site_id: siteId, diff --git a/framerail/src/routes/[slug]/[...extra]/+server.ts b/framerail/src/routes/[slug]/[...extra]/+server.ts index 9fd8a57adf..70e1d2583e 100644 --- a/framerail/src/routes/[slug]/[...extra]/+server.ts +++ b/framerail/src/routes/[slug]/[...extra]/+server.ts @@ -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")) { diff --git a/framerail/src/routes/[slug]/[...extra]/page.svelte b/framerail/src/routes/[slug]/[...extra]/page.svelte index f0fba5cc59..e1beb98a9a 100644 --- a/framerail/src/routes/[slug]/[...extra]/page.svelte +++ b/framerail/src/routes/[slug]/[...extra]/page.svelte @@ -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 @@ -574,7 +588,7 @@ on:submit|preventDefault={setParents} >