mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 15:05:35 +00:00
fix: edit category not working when updating name
This commit is contained in:
@@ -25,10 +25,9 @@ async function editCategory({ req, res, user }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const isCategoryNameAlreadyUsed = await getUserCategoryByName(user, name);
|
const isCategoryNameAlreadyUsed = await getUserCategoryByName(user, name);
|
||||||
if (isCategoryNameAlreadyUsed.id !== cid) {
|
if (isCategoryNameAlreadyUsed && isCategoryNameAlreadyUsed?.id !== cid) {
|
||||||
throw new Error('Category name already used');
|
throw new Error('Category name already used');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (category.id === nextId) {
|
if (category.id === nextId) {
|
||||||
throw new Error('Category nextId cannot be equal to current category ID');
|
throw new Error('Category nextId cannot be equal to current category ID');
|
||||||
}
|
}
|
||||||
@@ -81,6 +80,7 @@ async function editCategory({ req, res, user }) {
|
|||||||
id: category.id,
|
id: category.id,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
|
name,
|
||||||
nextId,
|
nextId,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
@@ -96,6 +96,17 @@ async function editCategory({ req, res, user }) {
|
|||||||
}),
|
}),
|
||||||
].filter((a) => a !== null && a !== undefined),
|
].filter((a) => a !== null && a !== undefined),
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
await prisma.category.update({
|
||||||
|
where: {
|
||||||
|
authorId: userId,
|
||||||
|
id: category.id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
name,
|
||||||
|
nextId: category.nextId,
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.send({
|
return res.send({
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ export default function PageEditCategory({
|
|||||||
makeRequest({
|
makeRequest({
|
||||||
url: `${PATHS.API.CATEGORY}/${category.id}`,
|
url: `${PATHS.API.CATEGORY}/${category.id}`,
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
body: { name },
|
body: { name, nextId: category.nextId },
|
||||||
})
|
})
|
||||||
.then((data) =>
|
.then((data) =>
|
||||||
router.push(`${PATHS.HOME}?categoryId=${data?.categoryId}`),
|
router.push(`${PATHS.HOME}?categoryId=${data?.categoryId}`),
|
||||||
|
|||||||
Reference in New Issue
Block a user