From 78915b6b999f7fffab88686d3b63ca461ad9b688 Mon Sep 17 00:00:00 2001 From: Sonny Date: Wed, 10 Apr 2024 19:42:26 +0200 Subject: [PATCH] fix: error when removing a category without a previous category --- public/locales/en/common.json | 3 ++- public/locales/fr/common.json | 3 ++- src/pages/api/category/[cid].ts | 22 +++++++++++++--------- src/pages/category/remove/[cid].tsx | 10 ++++++++++ 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 33858ac..edf4b3b 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -19,6 +19,7 @@ "category": "Category", "name": "Category name", "description": "Category description", + "no-description": "No description", "create": "Create a category", "edit": "Edit a category", "remove": "Delete a category", @@ -47,4 +48,4 @@ "footer": { "made_by": "Made with ❤\uFE0F by" } -} \ No newline at end of file +} diff --git a/public/locales/fr/common.json b/public/locales/fr/common.json index 18c3b3c..a516823 100644 --- a/public/locales/fr/common.json +++ b/public/locales/fr/common.json @@ -19,6 +19,7 @@ "category": "Catégorie", "name": "Nom de la catégorie", "description": "Description de la catégorie", + "no-description": "Aucune description", "create": "Créer une catégorie", "edit": "Modifier une catégorie", "remove": "Supprimer une catégorie", @@ -47,4 +48,4 @@ "footer": { "made_by": "Fait avec ❤\uFE0F par" } -} \ No newline at end of file +} diff --git a/src/pages/api/category/[cid].ts b/src/pages/api/category/[cid].ts index aa9b104..c7f2b97 100644 --- a/src/pages/api/category/[cid].ts +++ b/src/pages/api/category/[cid].ts @@ -134,18 +134,22 @@ async function deleteCategory({ req, res, user }) { where: { id: cid }, }); - const { id: previousCategoryId } = await prisma.category.findFirst({ + const previousCategory = await prisma.category.findFirst({ where: { nextId: category.id }, select: { id: true }, }); - await prisma.category.update({ - where: { - id: previousCategoryId, - }, - data: { - nextId: category.nextId, - }, - }); + + if (previousCategory) { + await prisma.category.update({ + where: { + id: previousCategory?.id, + }, + data: { + nextId: category.nextId, + }, + }); + } + return res.send({ success: 'Category successfully deleted', categoryId: category.id, diff --git a/src/pages/category/remove/[cid].tsx b/src/pages/category/remove/[cid].tsx index 33bbd5d..7f09ac2 100644 --- a/src/pages/category/remove/[cid].tsx +++ b/src/pages/category/remove/[cid].tsx @@ -70,6 +70,16 @@ export default function PageRemoveCategory({ fieldClass={styles['input-field']} disabled={true} /> +