refactor: remove axios

This commit is contained in:
Sonny
2023-11-12 03:30:58 +01:00
parent c35eb67890
commit fbf12d9947
13 changed files with 133 additions and 272 deletions

View File

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