mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-10 07:25:35 +00:00
Revert "Add: tRPC + register on first time login"
This reverts commit cb5f9017a9a3e1e0886bfa67b9b9f5554f527d35.
This commit is contained in:
@@ -1,72 +1,64 @@
|
||||
import axios, { AxiosResponse } from "axios";
|
||||
import { useRouter } from "next/router";
|
||||
import nProgress from "nprogress";
|
||||
import { FormEvent, useMemo, useState } from "react";
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import FormLayout from "../../components/FormLayout";
|
||||
import TextBox from "../../components/TextBox";
|
||||
import nProgress from 'nprogress';
|
||||
import axios, { AxiosResponse } from 'axios';
|
||||
|
||||
import { Category } from "../../types";
|
||||
import { HandleAxiosError } from "../../utils/front";
|
||||
import { trpc } from "../../utils/trpc";
|
||||
import FormLayout from '../../components/FormLayout';
|
||||
import TextBox from '../../components/TextBox';
|
||||
|
||||
import styles from "../../styles/create.module.scss";
|
||||
import styles from '../../styles/create.module.scss';
|
||||
import { HandleAxiosError } from '../../utils/front';
|
||||
import { useRouter } from 'next/router';
|
||||
|
||||
function CreateCategory() {
|
||||
const hello = trpc.hello.useQuery({ text: "client" });
|
||||
const info = useRouter().query?.info as string;
|
||||
const [name, setName] = useState<string>("");
|
||||
const info = useRouter().query?.info as string;
|
||||
const [name, setName] = useState<string>('');
|
||||
|
||||
const [error, setError] = useState<string | undefined>();
|
||||
const [success, setSuccess] = useState<string | undefined>();
|
||||
const [loading, setLoading] = useState<boolean>(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [success, setSuccess] = useState<string | null>(null);
|
||||
const [canSubmit, setCanSubmit] = useState<boolean>(false);
|
||||
|
||||
const canSubmit = useMemo<boolean>(
|
||||
() => name.length !== 0 && !loading,
|
||||
[loading, name.length]
|
||||
);
|
||||
const handleSubmit = async (event: FormEvent<HTMLFormElement>) => {
|
||||
event.preventDefault();
|
||||
setSuccess(undefined);
|
||||
setError(undefined);
|
||||
setLoading(true);
|
||||
nProgress.start();
|
||||
useEffect(() => setCanSubmit(name.length !== 0), [name]);
|
||||
|
||||
try {
|
||||
const payload = { name };
|
||||
const { data }: AxiosResponse<{ success: string; category: Category }> =
|
||||
await axios.post("/api/category/create", payload);
|
||||
const handleSubmit = async (event) => {
|
||||
event.preventDefault();
|
||||
setSuccess(null);
|
||||
setError(null);
|
||||
setCanSubmit(false);
|
||||
nProgress.start();
|
||||
|
||||
console.log(data);
|
||||
setSuccess(data.success);
|
||||
} catch (error) {
|
||||
setError(HandleAxiosError(error));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
nProgress.done();
|
||||
try {
|
||||
const payload = { name };
|
||||
const { data }: AxiosResponse<any> = await axios.post('/api/category/create', payload);
|
||||
setSuccess(data?.success || 'Categorie créée avec succès');
|
||||
setCanSubmit(false);
|
||||
} catch (error) {
|
||||
setError(HandleAxiosError(error));
|
||||
setCanSubmit(true);
|
||||
} finally {
|
||||
nProgress.done();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<FormLayout
|
||||
title="Créer une catégorie"
|
||||
errorMessage={error}
|
||||
successMessage={success}
|
||||
infoMessage={info}
|
||||
canSubmit={canSubmit}
|
||||
handleSubmit={handleSubmit}
|
||||
>
|
||||
<TextBox
|
||||
name="name"
|
||||
label="Nom de la catégorie"
|
||||
onChangeCallback={(value) => setName(value)}
|
||||
value={name}
|
||||
fieldClass={styles["input-field"]}
|
||||
placeholder="Nom..."
|
||||
/>
|
||||
{JSON.stringify(hello)}
|
||||
</FormLayout>
|
||||
);
|
||||
return (<>
|
||||
<FormLayout
|
||||
title='Créer une catégorie'
|
||||
errorMessage={error}
|
||||
successMessage={success}
|
||||
infoMessage={info}
|
||||
canSubmit={canSubmit}
|
||||
handleSubmit={handleSubmit}
|
||||
>
|
||||
<TextBox
|
||||
name='name'
|
||||
label='Nom de la catégorie'
|
||||
onChangeCallback={(value) => setName(value)}
|
||||
value={name}
|
||||
fieldClass={styles['input-field']}
|
||||
placeholder='Nom...'
|
||||
/>
|
||||
</FormLayout>
|
||||
</>);
|
||||
}
|
||||
|
||||
CreateCategory.authRequired = true;
|
||||
|
||||
Reference in New Issue
Block a user