frontend: add Loading
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
import { useCookies } from 'react-cookie';
|
||||
import { API_URL } from '../constants/constants';
|
||||
import { hashPassword } from './hashPassword';
|
||||
import { useState } from 'react';
|
||||
|
||||
const useFetchUser = () => {
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
const [apiCookie, setApiCookie] = useCookies(['apiToken']);
|
||||
const [, setUserNameCookie] = useCookies(['userName'])
|
||||
|
||||
const userSet = async (userName: string): Promise<boolean> => {
|
||||
try {
|
||||
setIsLoading(true)
|
||||
const response = await fetch(`${API_URL}/users/isSet?userName=${encodeURIComponent(userName)}`);
|
||||
setIsLoading(false)
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const data = await response.json();
|
||||
return data; // Assuming the server returns true/false
|
||||
@@ -84,6 +88,7 @@ const useFetchUser = () => {
|
||||
|
||||
const validToken = async (token: string | undefined): Promise<boolean> => {
|
||||
try {
|
||||
setIsLoading(true)
|
||||
const response = await fetch(`${API_URL}/login/validateToken`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -93,6 +98,7 @@ const useFetchUser = () => {
|
||||
token
|
||||
}),
|
||||
});
|
||||
setIsLoading(false)
|
||||
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const data = await response.json();
|
||||
@@ -156,7 +162,7 @@ const useFetchUser = () => {
|
||||
}
|
||||
}
|
||||
|
||||
return { userSet, passwordCreate, signUser, validToken, updateAttendance, getAttendance };
|
||||
return { userSet, passwordCreate, signUser, validToken, updateAttendance, getAttendance, isLoading };
|
||||
};
|
||||
|
||||
export default useFetchUser;
|
||||
|
||||
Reference in New Issue
Block a user