frontend: add Loading

This commit is contained in:
tylen
2025-11-02 13:50:44 +02:00
parent 98e5ef06c0
commit cf9b0d53c1
6 changed files with 83 additions and 3 deletions

View File

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