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