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

@@ -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,
},
};
}