mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 07:03:25 +00:00
refactor: remove axios
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import axios from "axios";
|
||||
import FormLayout from "components/FormLayout";
|
||||
import PageTransition from "components/PageTransition";
|
||||
import TextBox from "components/TextBox";
|
||||
@@ -8,15 +7,13 @@ import { getServerSideTranslation } from "i18n";
|
||||
import getUserCategoriesCount from "lib/category/getUserCategoriesCount";
|
||||
import { useTranslation } from "next-i18next";
|
||||
import { useRouter } from "next/router";
|
||||
import nProgress from "nprogress";
|
||||
import { useMemo, useState } from "react";
|
||||
import { FormEvent, useMemo, useState } from "react";
|
||||
import styles from "styles/form.module.scss";
|
||||
import { redirectWithoutClientCache } from "utils/client";
|
||||
import { HandleAxiosError } from "utils/front";
|
||||
import { withAuthentication } from "utils/session";
|
||||
import { makeRequest } from "lib/request";
|
||||
|
||||
export default function PageCreateCategory({
|
||||
categoriesCount,
|
||||
categoriesCount
|
||||
}: {
|
||||
categoriesCount: number;
|
||||
}) {
|
||||
@@ -35,23 +32,19 @@ export default function PageCreateCategory({
|
||||
[name.length, submitted]
|
||||
);
|
||||
|
||||
const handleSubmit = async (event) => {
|
||||
const handleSubmit = async (event: FormEvent<HTMLFormElement>) => {
|
||||
event.preventDefault();
|
||||
setError(null);
|
||||
setSubmitted(true);
|
||||
nProgress.start();
|
||||
|
||||
try {
|
||||
const { data } = await axios.post(PATHS.API.CATEGORY, { name });
|
||||
redirectWithoutClientCache(router, "");
|
||||
router.push(`${PATHS.HOME}?categoryId=${data?.categoryId}`);
|
||||
setSubmitted(true);
|
||||
} catch (error) {
|
||||
setError(HandleAxiosError(error));
|
||||
setSubmitted(false);
|
||||
} finally {
|
||||
nProgress.done();
|
||||
}
|
||||
makeRequest({
|
||||
url: PATHS.API.CATEGORY,
|
||||
method: "POST",
|
||||
body: { name }
|
||||
})
|
||||
.then((data) => router.push(`${PATHS.HOME}?categoryId=${data?.categoryId}`))
|
||||
.catch(setError)
|
||||
.finally(() => setSubmitted(false));
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -86,8 +79,8 @@ export const getServerSideProps = withAuthentication(
|
||||
props: {
|
||||
session,
|
||||
categoriesCount,
|
||||
...(await getServerSideTranslation(locale)),
|
||||
},
|
||||
...(await getServerSideTranslation(locale))
|
||||
}
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user