mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-10 15:35:35 +00:00
feat: disable link to home page when user has 0 category
This commit is contained in:
@@ -21,6 +21,7 @@ interface FormProps {
|
|||||||
classBtnConfirm?: string;
|
classBtnConfirm?: string;
|
||||||
|
|
||||||
children: any;
|
children: any;
|
||||||
|
disableHomeLink?: boolean;
|
||||||
}
|
}
|
||||||
export default function Form({
|
export default function Form({
|
||||||
title,
|
title,
|
||||||
@@ -33,6 +34,7 @@ export default function Form({
|
|||||||
textBtnConfirm = "Valider",
|
textBtnConfirm = "Valider",
|
||||||
classBtnConfirm = "",
|
classBtnConfirm = "",
|
||||||
children,
|
children,
|
||||||
|
disableHomeLink = false,
|
||||||
}: FormProps) {
|
}: FormProps) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -49,9 +51,11 @@ export default function Form({
|
|||||||
{textBtnConfirm}
|
{textBtnConfirm}
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
<Link href={categoryId ? `/?categoryId=${categoryId}` : "/"}>
|
{!disableHomeLink && (
|
||||||
← Revenir à l'accueil
|
<Link href={categoryId ? `/?categoryId=${categoryId}` : "/"}>
|
||||||
</Link>
|
← Revenir à l'accueil
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
<MessageManager
|
<MessageManager
|
||||||
info={infoMessage}
|
info={infoMessage}
|
||||||
error={errorMessage}
|
error={errorMessage}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ export default function LinkItem({
|
|||||||
categoryId: link.category.id,
|
categoryId: link.category.id,
|
||||||
};
|
};
|
||||||
axios
|
axios
|
||||||
.put(`${PATHS.API.LINK.EDIT}/${link.id}`, payload)
|
.put(`${PATHS.API.LINK}/${link.id}`, payload)
|
||||||
.then(() => toggleFavorite(link.id))
|
.then(() => toggleFavorite(link.id))
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -13,16 +13,8 @@ const PATHS = {
|
|||||||
REMOVE: "/link/remove",
|
REMOVE: "/link/remove",
|
||||||
},
|
},
|
||||||
API: {
|
API: {
|
||||||
CATEGORY: {
|
CATEGORY: "/api/category",
|
||||||
CREATE: "/api/category/create",
|
LINK: "/api/link",
|
||||||
EDIT: "/api/category/edit",
|
|
||||||
REMOVE: "/api/category/remove",
|
|
||||||
},
|
|
||||||
LINK: {
|
|
||||||
CREATE: "/api/link/create",
|
|
||||||
EDIT: "/api/link/edit",
|
|
||||||
REMOVE: "/api/link/remove",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
NOT_FOUND: "/404",
|
NOT_FOUND: "/404",
|
||||||
SERVER_ERROR: "/505",
|
SERVER_ERROR: "/505",
|
||||||
|
|||||||
10
src/lib/category/getUserCategoriesCount.ts
Normal file
10
src/lib/category/getUserCategoriesCount.ts
Normal 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,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import { useHotkeys } from "react-hotkeys-hook";
|
|||||||
|
|
||||||
import AuthRequired from "components/AuthRequired";
|
import AuthRequired from "components/AuthRequired";
|
||||||
import * as Keys from "constants/keys";
|
import * as Keys from "constants/keys";
|
||||||
|
import PATHS from "constants/paths";
|
||||||
|
|
||||||
import "nprogress/nprogress.css";
|
import "nprogress/nprogress.css";
|
||||||
import "styles/globals.scss";
|
import "styles/globals.scss";
|
||||||
@@ -14,8 +15,8 @@ import "styles/globals.scss";
|
|||||||
function MyApp({ Component, pageProps: { session, ...pageProps } }) {
|
function MyApp({ Component, pageProps: { session, ...pageProps } }) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
useHotkeys(Keys.CLOSE_SEARCH_KEY, () => router.push("/"), {
|
useHotkeys(Keys.CLOSE_SEARCH_KEY, () => router.push(PATHS.HOME), {
|
||||||
enabled: router.pathname !== "/",
|
enabled: router.pathname !== PATHS.HOME,
|
||||||
enableOnFormTags: ["INPUT"],
|
enableOnFormTags: ["INPUT"],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -57,6 +57,10 @@ async function deleteCategory({ req, res, user }) {
|
|||||||
throw new Error("Unable to find category " + cid);
|
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({
|
await prisma.category.delete({
|
||||||
where: { id: cid },
|
where: { id: cid },
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -12,9 +12,12 @@ import useAutoFocus from "hooks/useAutoFocus";
|
|||||||
import { redirectWithoutClientCache } from "utils/client";
|
import { redirectWithoutClientCache } from "utils/client";
|
||||||
import { HandleAxiosError } from "utils/front";
|
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 styles from "styles/create.module.scss";
|
||||||
|
import { getSession } from "utils/session";
|
||||||
|
|
||||||
function CreateCategory() {
|
function CreateCategory({ categoriesCount }: { categoriesCount: number }) {
|
||||||
const autoFocusRef = useAutoFocus();
|
const autoFocusRef = useAutoFocus();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const info = useRouter().query?.info as string;
|
const info = useRouter().query?.info as string;
|
||||||
@@ -36,10 +39,8 @@ function CreateCategory() {
|
|||||||
nProgress.start();
|
nProgress.start();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { data } = await axios.post("/api/category/create", { name });
|
|
||||||
const { data } = await axios.post(PATHS.API.CATEGORY, { name });
|
const { data } = await axios.post(PATHS.API.CATEGORY, { name });
|
||||||
redirectWithoutClientCache(router, "");
|
redirectWithoutClientCache(router, "");
|
||||||
router.push(`/?categoryId=${data?.categoryId}`);
|
|
||||||
router.push(`${PATHS.HOME}?categoryId=${data?.categoryId}`);
|
router.push(`${PATHS.HOME}?categoryId=${data?.categoryId}`);
|
||||||
setSubmitted(true);
|
setSubmitted(true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -58,6 +59,7 @@ function CreateCategory() {
|
|||||||
infoMessage={info}
|
infoMessage={info}
|
||||||
canSubmit={canSubmit}
|
canSubmit={canSubmit}
|
||||||
handleSubmit={handleSubmit}
|
handleSubmit={handleSubmit}
|
||||||
|
disableHomeLink={categoriesCount === 0}
|
||||||
>
|
>
|
||||||
<TextBox
|
<TextBox
|
||||||
name="name"
|
name="name"
|
||||||
@@ -75,3 +77,15 @@ function CreateCategory() {
|
|||||||
|
|
||||||
CreateCategory.authRequired = true;
|
CreateCategory.authRequired = true;
|
||||||
export default CreateCategory;
|
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,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user