diff --git a/apps/api/src/page/update.ts b/apps/api/src/page/update.ts
index 1e639a9..28aa7b7 100644
--- a/apps/api/src/page/update.ts
+++ b/apps/api/src/page/update.ts
@@ -54,23 +54,31 @@ export const registerPageUpdate = (api: typeof pageApi) => {
.update(page)
.set({
...rest,
- userId: userId,
+ userId,
})
.where(and(eq(page.userId, userId), eq(page.id, id)))
.returning()
// TODO: implement transaction
if (changelogIds) {
- await db
- .delete(changelogs_to_pages)
- .where(eq(changelogs_to_pages.pageId, result.id))
- await db.insert(changelogs_to_pages).values(
- changelogIds.map((changelogId) => ({
- changelogId,
- pageId: result.id,
- })),
- )
+ if (changelogIds.length === 0) {
+ await db
+ .delete(changelogs_to_pages)
+ .where(eq(changelogs_to_pages.pageId, result.id))
+ }
+ if (changelogIds?.length >= 1) {
+ await db
+ .delete(changelogs_to_pages)
+ .where(eq(changelogs_to_pages.pageId, result.id))
+ await db.insert(changelogs_to_pages).values(
+ changelogIds.map((changelogId) => ({
+ changelogId,
+ pageId: result.id,
+ })),
+ )
+ }
}
+
if (!result) {
throw new HTTPException(404, { message: 'Not Found' })
}
diff --git a/apps/app/src/routes/page.$id.index.lazy.tsx b/apps/app/src/routes/page.$id.index.lazy.tsx
index b4e8c34..5978b3b 100644
--- a/apps/app/src/routes/page.$id.index.lazy.tsx
+++ b/apps/app/src/routes/page.$id.index.lazy.tsx
@@ -13,12 +13,12 @@ const Component = () => {
const { id } = Route.useParams()
const { data, isPending } = usePageById({ id })
const pageUpdate = usePageUpdate()
- const removeChangelog = (id: string) => {
+ const removeChangelog = (idToRemove: string) => {
const payload = {
title: data?.title,
description: data?.description,
changelogIds: data?.changelogs
- .filter((log) => log.id !== id)
+ .filter((log) => log.id !== idToRemove)
.map((l) => l.id),
}
pageUpdate.mutate({ id, payload })
@@ -32,12 +32,6 @@ const Component = () => {
Changelogs ({data.changelogs?.length})
-
-
-
-
@@ -57,7 +51,7 @@ const Component = () => {