add attendance
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import React from 'react';
|
||||
import useFetchUser from '../utils/fetchUser';
|
||||
|
||||
const ApologyMessage: React.FC = () => {
|
||||
const { updateAttendance } = useFetchUser()
|
||||
const handleButtonClick = async () => {
|
||||
await updateAttendance(true)
|
||||
window.location.reload();
|
||||
};
|
||||
|
||||
return (
|
||||
<div style={styles.container}>
|
||||
<p>
|
||||
Нам очень жаль, что ты в этот раз не будешь с нами... Но может ты еще поменяешь свое мнение
|
||||
</p>
|
||||
<button onClick={handleButtonClick}>
|
||||
Изменить мнение
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const styles = {
|
||||
container: {
|
||||
position: 'fixed' as 'fixed',
|
||||
top: 0,
|
||||
left: 0,
|
||||
width: '100vw',
|
||||
height: '100vh',
|
||||
backgroundColor: 'rgba(0, 0, 0, 1)',
|
||||
display: 'flex',
|
||||
flexDirection: 'column' as 'column',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
color: '#fff',
|
||||
zIndex: 1000,
|
||||
overflow: 'hidden',
|
||||
}
|
||||
}
|
||||
|
||||
// Export the component
|
||||
export default ApologyMessage;
|
||||
|
||||
@@ -3,6 +3,7 @@ import { useCookies } from 'react-cookie';
|
||||
import { GUESTS } from '../constants/constants';
|
||||
import useFetchUser from '../utils/fetchUser'; // Import your custom hook
|
||||
import { useNotification } from '../NotificationContext';
|
||||
import ApologyMessage from './Attendance';
|
||||
|
||||
const InitialSetup = () => {
|
||||
const [cookie, setCookie] = useCookies();
|
||||
@@ -11,8 +12,9 @@ const InitialSetup = () => {
|
||||
const [isSubmitted, setIsSubmitted] = useState(false);
|
||||
const [password, setPassword] = useState('');
|
||||
const [isPasswordSet, setIsPasswordSet] = useState(false); // To track if password is set
|
||||
const [userAttendance, setUserAttendance] = useState<boolean | null>(null);
|
||||
|
||||
const { userSet, passwordCreate, signUser, validToken } = useFetchUser(); // Destructure functions from the hook
|
||||
const { userSet, passwordCreate, signUser, validToken, getAttendance } = useFetchUser(); // Destructure functions from the hook
|
||||
const notify = useNotification();
|
||||
|
||||
const checkUserPassword = async (name: string) => {
|
||||
@@ -32,8 +34,16 @@ const InitialSetup = () => {
|
||||
setIsSubmitted(isTokenValid);
|
||||
};
|
||||
|
||||
const getUserAttendance = async () => {
|
||||
const attendance = await getAttendance()
|
||||
setUserAttendance(attendance)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (cookie.apiToken !== undefined) validateToken();
|
||||
if (cookie.apiToken !== undefined) {
|
||||
getUserAttendance()
|
||||
validateToken();
|
||||
}
|
||||
}, [cookie.apiToken]);
|
||||
|
||||
|
||||
@@ -54,13 +64,18 @@ const InitialSetup = () => {
|
||||
}
|
||||
validateToken()
|
||||
};
|
||||
|
||||
|
||||
if (isSubmitted) {
|
||||
if (isSubmitted && userAttendance !== false) {
|
||||
console.log('Selected', selectedName);
|
||||
return null; // or you can redirect to another component or page
|
||||
}
|
||||
|
||||
if (userAttendance == false) {
|
||||
return (
|
||||
<ApologyMessage/>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={styles.container}>
|
||||
<h2 style={styles.title}>Выбери себя</h2>
|
||||
|
||||
@@ -3,7 +3,7 @@ import { API_URL } from '../constants/constants';
|
||||
import { hashPassword } from './hashPassword';
|
||||
|
||||
const useFetchUser = () => {
|
||||
const [, setApiCookie] = useCookies(['apiToken']);
|
||||
const [apiCookie, setApiCookie] = useCookies(['apiToken']);
|
||||
const [, setUserNameCookie] = useCookies(['userName'])
|
||||
|
||||
const userSet = async (userName: string): Promise<boolean> => {
|
||||
@@ -108,7 +108,55 @@ const useFetchUser = () => {
|
||||
}
|
||||
}
|
||||
|
||||
return { userSet, passwordCreate, signUser, validToken };
|
||||
const updateAttendance = async (attendanceStatus: boolean): Promise<boolean> => {
|
||||
const token = apiCookie.apiToken
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/users/attendance`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
token,
|
||||
attendance: attendanceStatus,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const data = await response.json();
|
||||
|
||||
if (!data.success) throw new Error(data.message);
|
||||
|
||||
return true; // Attendance updated successfully
|
||||
} catch (error) {
|
||||
console.error('Error updating attendance:', error);
|
||||
return false; // Attendance update failed
|
||||
}
|
||||
}
|
||||
|
||||
const getAttendance = async (): Promise<boolean | null> => {
|
||||
const token = apiCookie.apiToken
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/users/attendance?token=${encodeURIComponent(token)}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
const data = await response.json();
|
||||
|
||||
if (!data.success) throw new Error(data.message);
|
||||
|
||||
return data.attendance; // Returns attendance status (true/false)
|
||||
} catch (error) {
|
||||
console.error('Error retrieving attendance:', error);
|
||||
return null; // In case of error or if attendance status is not found
|
||||
}
|
||||
}
|
||||
|
||||
return { userSet, passwordCreate, signUser, validToken, updateAttendance, getAttendance };
|
||||
};
|
||||
|
||||
export default useFetchUser;
|
||||
|
||||
Reference in New Issue
Block a user