mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 15:05:35 +00:00
refactor: update front api endpoints
This commit is contained in:
@@ -7,6 +7,7 @@ import FormLayout from "components/FormLayout";
|
|||||||
import PageTransition from "components/PageTransition";
|
import PageTransition from "components/PageTransition";
|
||||||
import TextBox from "components/TextBox";
|
import TextBox from "components/TextBox";
|
||||||
|
|
||||||
|
import PATHS from "constants/paths";
|
||||||
import useAutoFocus from "hooks/useAutoFocus";
|
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";
|
||||||
@@ -36,8 +37,10 @@ function CreateCategory() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const { data } = await axios.post("/api/category/create", { name });
|
const { data } = await axios.post("/api/category/create", { name });
|
||||||
|
const { data } = await axios.post(PATHS.API.CATEGORY, { name });
|
||||||
redirectWithoutClientCache(router, "");
|
redirectWithoutClientCache(router, "");
|
||||||
router.push(`/?categoryId=${data?.categoryId}`);
|
router.push(`/?categoryId=${data?.categoryId}`);
|
||||||
|
router.push(`${PATHS.HOME}?categoryId=${data?.categoryId}`);
|
||||||
setSubmitted(true);
|
setSubmitted(true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError(HandleAxiosError(error));
|
setError(HandleAxiosError(error));
|
||||||
|
|||||||
@@ -7,10 +7,13 @@ import FormLayout from "components/FormLayout";
|
|||||||
import PageTransition from "components/PageTransition";
|
import PageTransition from "components/PageTransition";
|
||||||
import TextBox from "components/TextBox";
|
import TextBox from "components/TextBox";
|
||||||
|
|
||||||
|
import PATHS from "constants/paths";
|
||||||
import useAutoFocus from "hooks/useAutoFocus";
|
import useAutoFocus from "hooks/useAutoFocus";
|
||||||
|
import getUserCategory from "lib/category/getUserCategory";
|
||||||
|
import getUser from "lib/user/getUser";
|
||||||
import { Category } from "types";
|
import { Category } from "types";
|
||||||
import { BuildCategory, HandleAxiosError } from "utils/front";
|
import { HandleAxiosError } from "utils/front";
|
||||||
import prisma from "utils/prisma";
|
import { getSession } from "utils/session";
|
||||||
|
|
||||||
import styles from "styles/create.module.scss";
|
import styles from "styles/create.module.scss";
|
||||||
|
|
||||||
@@ -35,12 +38,10 @@ function EditCategory({ category }: { category: Category }) {
|
|||||||
nProgress.start();
|
nProgress.start();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const payload = { name };
|
const { data } = await axios.put(`${PATHS.API.CATEGORY}/${category.id}`, {
|
||||||
const { data } = await axios.put(
|
name,
|
||||||
`/api/category/edit/${category.id}`,
|
});
|
||||||
payload
|
router.push(`${PATHS.HOME}?categoryId=${data?.categoryId}`);
|
||||||
);
|
|
||||||
router.push(`/?categoryId=${data?.categoryId}`);
|
|
||||||
setSubmitted(true);
|
setSubmitted(true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError(HandleAxiosError(error));
|
setError(HandleAxiosError(error));
|
||||||
@@ -75,22 +76,21 @@ function EditCategory({ category }: { category: Category }) {
|
|||||||
EditCategory.authRequired = true;
|
EditCategory.authRequired = true;
|
||||||
export default EditCategory;
|
export default EditCategory;
|
||||||
|
|
||||||
export async function getServerSideProps({ query }) {
|
export async function getServerSideProps({ req, res, query }) {
|
||||||
const { cid } = query;
|
const { cid } = query;
|
||||||
const categoryDB = await prisma.category.findFirst({
|
|
||||||
where: { id: Number(cid) },
|
|
||||||
include: { links: true, author: true },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!categoryDB) {
|
const session = await getSession(req, res);
|
||||||
|
const user = await getUser(session);
|
||||||
|
|
||||||
|
const category = await getUserCategory(user, Number(cid));
|
||||||
|
if (!category) {
|
||||||
return {
|
return {
|
||||||
redirect: {
|
redirect: {
|
||||||
destination: "/",
|
destination: PATHS.HOME,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const category = BuildCategory(categoryDB);
|
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
category: JSON.parse(JSON.stringify(category)),
|
category: JSON.parse(JSON.stringify(category)),
|
||||||
|
|||||||
@@ -8,9 +8,12 @@ import FormLayout from "components/FormLayout";
|
|||||||
import PageTransition from "components/PageTransition";
|
import PageTransition from "components/PageTransition";
|
||||||
import TextBox from "components/TextBox";
|
import TextBox from "components/TextBox";
|
||||||
|
|
||||||
|
import PATHS from "constants/paths";
|
||||||
|
import getUserCategory from "lib/category/getUserCategory";
|
||||||
|
import getUser from "lib/user/getUser";
|
||||||
import { Category } from "types";
|
import { Category } from "types";
|
||||||
import { BuildCategory, HandleAxiosError } from "utils/front";
|
import { HandleAxiosError } from "utils/front";
|
||||||
import prisma from "utils/prisma";
|
import { getSession } from "utils/session";
|
||||||
|
|
||||||
import styles from "styles/create.module.scss";
|
import styles from "styles/create.module.scss";
|
||||||
|
|
||||||
@@ -36,8 +39,8 @@ function RemoveCategory({ category }: { category: Category }) {
|
|||||||
nProgress.start();
|
nProgress.start();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await axios.delete(`/api/category/remove/${category.id}`);
|
await axios.delete(`${PATHS.API.CATEGORY}/${category.id}`);
|
||||||
router.push("/");
|
router.push(PATHS.HOME);
|
||||||
setSubmitted(true);
|
setSubmitted(true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError(HandleAxiosError(error));
|
setError(HandleAxiosError(error));
|
||||||
@@ -80,22 +83,21 @@ function RemoveCategory({ category }: { category: Category }) {
|
|||||||
RemoveCategory.authRequired = true;
|
RemoveCategory.authRequired = true;
|
||||||
export default RemoveCategory;
|
export default RemoveCategory;
|
||||||
|
|
||||||
export async function getServerSideProps({ query }) {
|
export async function getServerSideProps({ req, res, query }) {
|
||||||
const { cid } = query;
|
const { cid } = query;
|
||||||
const categoryDB = await prisma.category.findFirst({
|
|
||||||
where: { id: Number(cid) },
|
|
||||||
include: { links: true, author: true },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!categoryDB) {
|
const session = await getSession(req, res);
|
||||||
|
const user = await getUser(session);
|
||||||
|
|
||||||
|
const category = await getUserCategory(user, Number(cid));
|
||||||
|
if (!category) {
|
||||||
return {
|
return {
|
||||||
redirect: {
|
redirect: {
|
||||||
destination: "/",
|
destination: PATHS.HOME,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const category = BuildCategory(categoryDB);
|
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
category: JSON.parse(JSON.stringify(category)),
|
category: JSON.parse(JSON.stringify(category)),
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import PageTransition from "components/PageTransition";
|
|||||||
import Selector from "components/Selector";
|
import Selector from "components/Selector";
|
||||||
import TextBox from "components/TextBox";
|
import TextBox from "components/TextBox";
|
||||||
|
|
||||||
|
import PATHS from "constants/paths";
|
||||||
import useAutoFocus from "hooks/useAutoFocus";
|
import useAutoFocus from "hooks/useAutoFocus";
|
||||||
import getUserCategories from "lib/category/getUserCategories";
|
import getUserCategories from "lib/category/getUserCategories";
|
||||||
import getUser from "lib/user/getUser";
|
import getUser from "lib/user/getUser";
|
||||||
@@ -51,8 +52,8 @@ function CreateLink({ categories }: { categories: Category[] }) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const payload = { name, url, favorite, categoryId };
|
const payload = { name, url, favorite, categoryId };
|
||||||
const { data } = await axios.post("/api/link/create", payload);
|
const { data } = await axios.post(PATHS.API.LINK, payload);
|
||||||
router.push(`/?categoryId=${data?.categoryId}`);
|
router.push(`${PATHS.HOME}?categoryId=${data?.categoryId}`);
|
||||||
setSubmitted(true);
|
setSubmitted(true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError(HandleAxiosError(error));
|
setError(HandleAxiosError(error));
|
||||||
@@ -120,7 +121,7 @@ export async function getServerSideProps({ req, res }) {
|
|||||||
if (categories.length === 0) {
|
if (categories.length === 0) {
|
||||||
return {
|
return {
|
||||||
redirect: {
|
redirect: {
|
||||||
destination: "/",
|
destination: PATHS.HOME,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,12 +12,14 @@ import TextBox from "components/TextBox";
|
|||||||
import useAutoFocus from "hooks/useAutoFocus";
|
import useAutoFocus from "hooks/useAutoFocus";
|
||||||
import { Category, Link } from "types";
|
import { Category, Link } from "types";
|
||||||
import { HandleAxiosError, IsValidURL } from "utils/front";
|
import { HandleAxiosError, IsValidURL } from "utils/front";
|
||||||
|
import { getSessionOrThrow } from "utils/session";
|
||||||
|
|
||||||
import getUserCategories from "lib/category/getUserCategories";
|
import getUserCategories from "lib/category/getUserCategories";
|
||||||
import getUserLink from "lib/link/getUserLink";
|
import getUserLink from "lib/link/getUserLink";
|
||||||
import getUserOrThrow from "lib/user/getUserOrThrow";
|
import getUserOrThrow from "lib/user/getUserOrThrow";
|
||||||
|
|
||||||
|
import PATHS from "constants/paths";
|
||||||
import styles from "styles/create.module.scss";
|
import styles from "styles/create.module.scss";
|
||||||
import { getSessionOrThrow } from "utils/session";
|
|
||||||
|
|
||||||
function EditLink({
|
function EditLink({
|
||||||
link,
|
link,
|
||||||
@@ -71,8 +73,8 @@ function EditLink({
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const payload = { name, url, favorite, categoryId };
|
const payload = { name, url, favorite, categoryId };
|
||||||
const { data } = await axios.put(`/api/link/edit/${link.id}`, payload);
|
const { data } = await axios.put(`${PATHS.API.LINK}/${link.id}`, payload);
|
||||||
router.push(`/?categoryId=${data?.categoryId}`);
|
router.push(`${PATHS.HOME}?categoryId=${data?.categoryId}`);
|
||||||
setSubmitted(true);
|
setSubmitted(true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError(HandleAxiosError(error));
|
setError(HandleAxiosError(error));
|
||||||
@@ -142,7 +144,7 @@ export async function getServerSideProps({ req, res, query }) {
|
|||||||
if (!link) {
|
if (!link) {
|
||||||
return {
|
return {
|
||||||
redirect: {
|
redirect: {
|
||||||
destination: "/",
|
destination: PATHS.HOME,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import FormLayout from "components/FormLayout";
|
|||||||
import PageTransition from "components/PageTransition";
|
import PageTransition from "components/PageTransition";
|
||||||
import TextBox from "components/TextBox";
|
import TextBox from "components/TextBox";
|
||||||
|
|
||||||
|
import PATHS from "constants/paths";
|
||||||
import getUserLink from "lib/link/getUserLink";
|
import getUserLink from "lib/link/getUserLink";
|
||||||
import getUser from "lib/user/getUser";
|
import getUser from "lib/user/getUser";
|
||||||
import { Link } from "types";
|
import { Link } from "types";
|
||||||
@@ -34,8 +35,8 @@ function RemoveLink({ link }: { link: Link }) {
|
|||||||
nProgress.start();
|
nProgress.start();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { data } = await axios.delete(`/api/link/remove/${link.id}`);
|
const { data } = await axios.delete(`${PATHS.API.LINK}/${link.id}`);
|
||||||
router.push(`/?categoryId=${data?.categoryId}`);
|
router.push(`${PATHS.HOME}?categoryId=${data?.categoryId}`);
|
||||||
setSubmitted(true);
|
setSubmitted(true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError(HandleAxiosError(error));
|
setError(HandleAxiosError(error));
|
||||||
@@ -106,7 +107,7 @@ export async function getServerSideProps({ req, res, query }) {
|
|||||||
if (!link) {
|
if (!link) {
|
||||||
return {
|
return {
|
||||||
redirect: {
|
redirect: {
|
||||||
destination: "/",
|
destination: PATHS.HOME,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user