feat(app): add changelog remove from page

This commit is contained in:
Lars Hampe 2024-10-24 23:16:40 +02:00
parent ce4cd21937
commit 8e093590c0
2 changed files with 21 additions and 19 deletions

View File

@ -54,23 +54,31 @@ export const registerPageUpdate = (api: typeof pageApi) => {
.update(page) .update(page)
.set({ .set({
...rest, ...rest,
userId: userId, userId,
}) })
.where(and(eq(page.userId, userId), eq(page.id, id))) .where(and(eq(page.userId, userId), eq(page.id, id)))
.returning() .returning()
// TODO: implement transaction // TODO: implement transaction
if (changelogIds) { if (changelogIds) {
await db if (changelogIds.length === 0) {
.delete(changelogs_to_pages) await db
.where(eq(changelogs_to_pages.pageId, result.id)) .delete(changelogs_to_pages)
await db.insert(changelogs_to_pages).values( .where(eq(changelogs_to_pages.pageId, result.id))
changelogIds.map((changelogId) => ({ }
changelogId, if (changelogIds?.length >= 1) {
pageId: result.id, 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) { if (!result) {
throw new HTTPException(404, { message: 'Not Found' }) throw new HTTPException(404, { message: 'Not Found' })
} }

View File

@ -13,12 +13,12 @@ const Component = () => {
const { id } = Route.useParams() const { id } = Route.useParams()
const { data, isPending } = usePageById({ id }) const { data, isPending } = usePageById({ id })
const pageUpdate = usePageUpdate() const pageUpdate = usePageUpdate()
const removeChangelog = (id: string) => { const removeChangelog = (idToRemove: string) => {
const payload = { const payload = {
title: data?.title, title: data?.title,
description: data?.description, description: data?.description,
changelogIds: data?.changelogs changelogIds: data?.changelogs
.filter((log) => log.id !== id) .filter((log) => log.id !== idToRemove)
.map((l) => l.id), .map((l) => l.id),
} }
pageUpdate.mutate({ id, payload }) pageUpdate.mutate({ id, payload })
@ -32,12 +32,6 @@ const Component = () => {
<CardHeader> <CardHeader>
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<CardTitle>Changelogs ({data.changelogs?.length})</CardTitle> <CardTitle>Changelogs ({data.changelogs?.length})</CardTitle>
<Link to="/changelog/$id/versionCreate" params={{ id }}>
<Button variant={'ghost'} size={'icon'}>
<PlusCircleIcon strokeWidth={1.5} className="w-5 h-5" />
</Button>
</Link>
</div> </div>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
@ -57,7 +51,7 @@ const Component = () => {
<Button <Button
size={'icon'} size={'icon'}
variant={'destructive'} variant={'ghost-destructive'}
onClick={() => removeChangelog(changelog.id)} onClick={() => removeChangelog(changelog.id)}
> >
<CircleMinusIcon className="w-4 h-4" /> <CircleMinusIcon className="w-4 h-4" />