From eff6ab95c02d2c9ff8dff1fa443ac54a9668c1b5 Mon Sep 17 00:00:00 2001
From: Alberto Defendi <1369-ahl-berto@users.noreply.gitlab.inf.unibz.it>
Date: Thu, 20 May 2021 14:56:09 +0200
Subject: [PATCH] Simplify calls to return only value.

The passed react state setter is removed.
---
 src/api/EntryPoint.ts      |  8 --------
 src/api/fetchCookie.ts     | 17 +++++------------
 src/api/getRole.ts         | 17 +++++++----------
 src/api/isAuthenticated.ts |  4 ++++
 4 files changed, 16 insertions(+), 30 deletions(-)
 delete mode 100644 src/api/EntryPoint.ts

diff --git a/src/api/EntryPoint.ts b/src/api/EntryPoint.ts
deleted file mode 100644
index c3ef802..0000000
--- a/src/api/EntryPoint.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-/**
- * Contains server api entrypoints.
- * Every server api call must be defined here and then used in the code.
- */
-export enum EntryPoint {
-  login = '/api/web/login',
-  seniors = 'api/web/seniors/',
-}
diff --git a/src/api/fetchCookie.ts b/src/api/fetchCookie.ts
index b2b2720..d531ad2 100644
--- a/src/api/fetchCookie.ts
+++ b/src/api/fetchCookie.ts
@@ -1,16 +1,9 @@
 import axios from 'axios';
 
 /**
- *
- * Ask and set csrf cookie to server.
- * @param setIsCookieFetched function to set the state isCookieFetched.
- * @returns null
+ * Call for csrf cookie. This cookie is the user session identifier and
+ * must be sent during the login process.
+ * @returns csrf cookie
  */
-export const fetchCookie = async (
-  setIsCookieFetched: React.Dispatch<React.SetStateAction<string>>,
-): Promise<unknown> => {
-  const response = await axios('/api/web/csrf');
-  setIsCookieFetched(response.data.token);
-  localStorage.setItem('COOKIE', response.data.token);
-  return null;
-};
+export const fetchCookie = async (): Promise<string> =>
+  axios('/api/web/csrf').then((res) => res.data.token);
diff --git a/src/api/getRole.ts b/src/api/getRole.ts
index a20d304..cb06049 100644
--- a/src/api/getRole.ts
+++ b/src/api/getRole.ts
@@ -1,12 +1,9 @@
 import axios from 'axios';
-import { AuthContext } from 'components/AuthUser/AuthContext';
-import { useContext } from 'react';
 
-export const getRole = async (
-  setRole: React.Dispatch<React.SetStateAction<string>>,
-): Promise<void> => {
-  const response = await axios('/api/web/login/get_role').then(
-    (res) => res.data.role,
-  );
-  setRole(response);
-};
+/**
+ * Asks for the current set role.
+ * @returns empty if the role is not set or a name between those defined
+ * in {@see userRoles}
+ */
+export const getRole = async (): Promise<string> =>
+  axios('/api/web/login/get_role').then((res) => res.data.role);
diff --git a/src/api/isAuthenticated.ts b/src/api/isAuthenticated.ts
index 1195e01..041b4be 100644
--- a/src/api/isAuthenticated.ts
+++ b/src/api/isAuthenticated.ts
@@ -1,5 +1,9 @@
 import axios from 'axios';
 
+/**
+ * Ask the server if the user is authenticated.
+ * @returns true or false if the user is authenticated.
+ */
 export const isAuthenticated = async (): Promise<boolean> =>
   axios('/api/web/login/is_authenticated').then(
     (res) => res.data.is_authenticated,
-- 
GitLab