diff --git a/frontend/src/components/Greeting.tsx b/frontend/src/components/Greeting.tsx index 396089e..13f3004 100644 --- a/frontend/src/components/Greeting.tsx +++ b/frontend/src/components/Greeting.tsx @@ -1,17 +1,51 @@ import { useCookies } from "react-cookie"; import CenteredContainer from "./ChildrenContainer"; import useFetchUser from "../utils/fetchUser"; +import type React from "react"; +import ApologyMessage from "./Attendance"; +import { useState } from "react"; -function Greeting() { - const [cookie] = useCookies(['userName']) - const userName = cookie.userName; - const { updateAttendance } = useFetchUser() +const Attendance: React.FC = () => { + const { updateAttendance, getAttendance } = useFetchUser() + const [attendance, setAttendance] = useState(null) + const fetchAttendance = async () => { + const is = await getAttendance() + setAttendance(is) + } + fetchAttendance() const handleUpdate = async (status: boolean) => { await updateAttendance(status) window.location.reload(); } + if (attendance == false) { + return () + } + + return ( + <> + {attendance == null && ( + <> +

Присоеденишься?

+

(Можно и потом ответить)

+ + + + )} + {attendance == true && ( + <> +

Круто! Ты с нами!

+

Если все же по разным обстоятельствам ты не сможешь/не захочешь, то всегда можно передумать

+ + )} + ) +} + +function Greeting() { + const [cookie] = useCookies(['userName']) + const userName = cookie.userName; + return ( <> @@ -24,11 +58,9 @@ function Greeting() { Приглашаем тебя отпраздновать предстоящий Новый Год 2025-2026 с нами в сосновой избе, в которой, ко всему прочему, будет праздноваться годовщина нашей жизни в ней! Наши двери открыты с 30.12.2025. Праздник обычно длится до 01.01.2025, но если тебе или твоим спутникам будет безумно плохо, то можно остаться и до второго числа. -

Присоеденишься?

-

(Можно и потом ответить)

- - +

+
) diff --git a/frontend/src/components/InitialSetup.tsx b/frontend/src/components/InitialSetup.tsx index d6fb81f..ac8f23a 100644 --- a/frontend/src/components/InitialSetup.tsx +++ b/frontend/src/components/InitialSetup.tsx @@ -3,7 +3,6 @@ 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'; import {Loading} from './Loading'; const InitialSetup = () => { @@ -13,9 +12,8 @@ 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(null); - const { userSet, passwordCreate, signUser, validToken, getAttendance, isLoading } = useFetchUser(); // Destructure functions from the hook + const { userSet, passwordCreate, signUser, validToken, isLoading } = useFetchUser(); // Destructure functions from the hook const notify = useNotification(); const checkUserPassword = async (name: string) => { @@ -35,14 +33,8 @@ const InitialSetup = () => { setIsSubmitted(isTokenValid); }; - const getUserAttendance = async () => { - const attendance = await getAttendance() - setUserAttendance(attendance) - } - useEffect(() => { if (cookie.apiToken !== undefined) { - getUserAttendance() validateToken(); } }, [cookie.apiToken]); @@ -65,17 +57,12 @@ const InitialSetup = () => { } validateToken() }; - - if (isSubmitted && userAttendance !== false) { + + if (isSubmitted) { console.log('Selected', selectedName); return null; // or you can redirect to another component or page } - if (userAttendance == false) { - return ( - - ) - } if (isLoading) { return ( diff --git a/frontend/src/utils/fetchHosting.tsx b/frontend/src/utils/fetchHosting.tsx index 674b674..1616b99 100644 --- a/frontend/src/utils/fetchHosting.tsx +++ b/frontend/src/utils/fetchHosting.tsx @@ -88,7 +88,7 @@ const useFetchHosting = () => { } // Optional: Fetch the updated data after reservation - await fetchData(); + //await fetchData(); } finally { setLoading(false); } diff --git a/frontend/src/utils/fetchUser.tsx b/frontend/src/utils/fetchUser.tsx index cd59f61..f0f25e5 100644 --- a/frontend/src/utils/fetchUser.tsx +++ b/frontend/src/utils/fetchUser.tsx @@ -114,6 +114,28 @@ const useFetchUser = () => { } } + const getAttendance = async (): Promise => { + 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 + } catch (error) { + console.error('Error retrieving attendance:', error); + return null + } + } + const updateAttendance = async (attendanceStatus: boolean): Promise => { const token = apiCookie.apiToken try { @@ -140,28 +162,6 @@ const useFetchUser = () => { } } - const getAttendance = async (): Promise => { - 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, isLoading }; };