From 4b0ff168e4a18581eefbd518c35dba13f01a6eca Mon Sep 17 00:00:00 2001
From: Alberto Defendi <1369-ahl-berto@users.noreply.gitlab.inf.unibz.it>
Date: Sun, 16 May 2021 18:05:46 +0200
Subject: [PATCH] Apply type change and call new functions.

---
 .../AuthUser/SignInForm/SignInForm.tsx        | 36 +++++++------------
 1 file changed, 12 insertions(+), 24 deletions(-)

diff --git a/src/components/AuthUser/SignInForm/SignInForm.tsx b/src/components/AuthUser/SignInForm/SignInForm.tsx
index 6ca0569..b7c0ea3 100644
--- a/src/components/AuthUser/SignInForm/SignInForm.tsx
+++ b/src/components/AuthUser/SignInForm/SignInForm.tsx
@@ -7,43 +7,31 @@ import { useHistory } from 'react-router-dom';
 import { AuthRoutes } from 'api/routes';
 import { AuthContext } from 'components/AuthUser/AuthContext';
 import { useStyles } from './useStyles';
+import { fetchCookie } from './fetchCookie';
+import { CredentialsType } from './CredentialsType';
 
-const configDjangoCookieName = (): void => {
-  axios.defaults.xsrfHeaderName = 'X-CSRFTOKEN';
-  axios.defaults.xsrfCookieName = 'csrftoken';
-  axios.defaults.withCredentials = true;
+const defaultValues: CredentialsType = {
+  username: '',
+  password: '',
 };
 
 export const SignInForm: FC = () => {
-  const [isCookieFetched, setisCookieFetched] = useState<string>('');
+  const [isCookieFetched, setIsCookieFetched] = useState<string>('');
 
-  configDjangoCookieName();
   useEffect(() => {
-    const fetchCookie = async (): Promise<unknown> => {
-      const response = await axios('/api/web/csrf');
-      setisCookieFetched(response.data.token);
-      return null;
-    };
-    if (!isCookieFetched) fetchCookie();
+    if (!isCookieFetched) fetchCookie(setIsCookieFetched);
   }, [isCookieFetched]);
+
   const history = useHistory();
   const { setRole } = useContext(AuthContext);
 
-  type FormData = {
-    username: string;
-    password: string;
-  };
-
-  const defaultValues: FormData = {
-    username: '',
-    password: '',
-  };
-
-  const { control, errors, setError, handleSubmit } = useForm<FormData>({
+  const { control, errors, setError, handleSubmit } = useForm<CredentialsType>({
     defaultValues,
   });
 
-  const onSubmit: SubmitHandler<FormData> = (values: FormData) => {
+  const onSubmit: SubmitHandler<CredentialsType> = (
+    values: CredentialsType,
+  ) => {
     axios
       .post(
         '/api/web/login',
-- 
GitLab