feat: disable link to home page when user has 0 category

This commit is contained in:
Sonny
2023-06-04 00:44:30 +02:00
parent 5c2c2de1bd
commit d9757bdc18
7 changed files with 44 additions and 19 deletions

View File

@@ -21,6 +21,7 @@ interface FormProps {
classBtnConfirm?: string;
children: any;
disableHomeLink?: boolean;
}
export default function Form({
title,
@@ -33,6 +34,7 @@ export default function Form({
textBtnConfirm = "Valider",
classBtnConfirm = "",
children,
disableHomeLink = false,
}: FormProps) {
return (
<>
@@ -49,9 +51,11 @@ export default function Form({
{textBtnConfirm}
</button>
</form>
<Link href={categoryId ? `/?categoryId=${categoryId}` : "/"}>
Revenir à l'accueil
</Link>
{!disableHomeLink && (
<Link href={categoryId ? `/?categoryId=${categoryId}` : "/"}>
Revenir à l'accueil
</Link>
)}
<MessageManager
info={infoMessage}
error={errorMessage}

View File

@@ -31,7 +31,7 @@ export default function LinkItem({
categoryId: link.category.id,
};
axios
.put(`${PATHS.API.LINK.EDIT}/${link.id}`, payload)
.put(`${PATHS.API.LINK}/${link.id}`, payload)
.then(() => toggleFavorite(link.id))
.catch(console.error);
};

View File

@@ -13,16 +13,8 @@ const PATHS = {
REMOVE: "/link/remove",
},
API: {
CATEGORY: {
CREATE: "/api/category/create",
EDIT: "/api/category/edit",
REMOVE: "/api/category/remove",
},
LINK: {
CREATE: "/api/link/create",
EDIT: "/api/link/edit",
REMOVE: "/api/link/remove",
},
CATEGORY: "/api/category",
LINK: "/api/link",
},
NOT_FOUND: "/404",
SERVER_ERROR: "/505",

View File

@@ -0,0 +1,10 @@
import { User } from "@prisma/client";
import prisma from "utils/prisma";
export default async function getUserCategoriesCount(user: User) {
return await prisma.category.count({
where: {
authorId: user.id,
},
});
}

View File

@@ -7,6 +7,7 @@ import { useHotkeys } from "react-hotkeys-hook";
import AuthRequired from "components/AuthRequired";
import * as Keys from "constants/keys";
import PATHS from "constants/paths";
import "nprogress/nprogress.css";
import "styles/globals.scss";
@@ -14,8 +15,8 @@ import "styles/globals.scss";
function MyApp({ Component, pageProps: { session, ...pageProps } }) {
const router = useRouter();
useHotkeys(Keys.CLOSE_SEARCH_KEY, () => router.push("/"), {
enabled: router.pathname !== "/",
useHotkeys(Keys.CLOSE_SEARCH_KEY, () => router.push(PATHS.HOME), {
enabled: router.pathname !== PATHS.HOME,
enableOnFormTags: ["INPUT"],
});

View File

@@ -57,6 +57,10 @@ async function deleteCategory({ req, res, user }) {
throw new Error("Unable to find category " + cid);
}
if (category.links.length !== 0) {
throw new Error("You cannot remove category with links");
}
await prisma.category.delete({
where: { id: cid },
});

View File

@@ -12,9 +12,12 @@ import useAutoFocus from "hooks/useAutoFocus";
import { redirectWithoutClientCache } from "utils/client";
import { HandleAxiosError } from "utils/front";
import getUserCategoriesCount from "lib/category/getUserCategoriesCount";
import getUser from "lib/user/getUser";
import styles from "styles/create.module.scss";
import { getSession } from "utils/session";
function CreateCategory() {
function CreateCategory({ categoriesCount }: { categoriesCount: number }) {
const autoFocusRef = useAutoFocus();
const router = useRouter();
const info = useRouter().query?.info as string;
@@ -36,10 +39,8 @@ function CreateCategory() {
nProgress.start();
try {
const { data } = await axios.post("/api/category/create", { name });
const { data } = await axios.post(PATHS.API.CATEGORY, { name });
redirectWithoutClientCache(router, "");
router.push(`/?categoryId=${data?.categoryId}`);
router.push(`${PATHS.HOME}?categoryId=${data?.categoryId}`);
setSubmitted(true);
} catch (error) {
@@ -58,6 +59,7 @@ function CreateCategory() {
infoMessage={info}
canSubmit={canSubmit}
handleSubmit={handleSubmit}
disableHomeLink={categoriesCount === 0}
>
<TextBox
name="name"
@@ -75,3 +77,15 @@ function CreateCategory() {
CreateCategory.authRequired = true;
export default CreateCategory;
export async function getServerSideProps({ req, res }) {
const session = await getSession(req, res);
const user = await getUser(session);
const categoriesCount = await getUserCategoriesCount(user);
return {
props: {
categoriesCount,
},
};
}