fix attendnace
This commit is contained in:
parent
c2e6f81775
commit
c39384badf
@ -1,17 +1,51 @@
|
|||||||
import { useCookies } from "react-cookie";
|
import { useCookies } from "react-cookie";
|
||||||
import CenteredContainer from "./ChildrenContainer";
|
import CenteredContainer from "./ChildrenContainer";
|
||||||
import useFetchUser from "../utils/fetchUser";
|
import useFetchUser from "../utils/fetchUser";
|
||||||
|
import type React from "react";
|
||||||
|
import ApologyMessage from "./Attendance";
|
||||||
|
import { useState } from "react";
|
||||||
|
|
||||||
function Greeting() {
|
const Attendance: React.FC = () => {
|
||||||
const [cookie] = useCookies<string>(['userName'])
|
const { updateAttendance, getAttendance } = useFetchUser()
|
||||||
const userName = cookie.userName;
|
const [attendance, setAttendance] = useState<boolean | null>(null)
|
||||||
const { updateAttendance } = useFetchUser()
|
const fetchAttendance = async () => {
|
||||||
|
const is = await getAttendance()
|
||||||
|
setAttendance(is)
|
||||||
|
}
|
||||||
|
fetchAttendance()
|
||||||
|
|
||||||
const handleUpdate = async (status: boolean) => {
|
const handleUpdate = async (status: boolean) => {
|
||||||
await updateAttendance(status)
|
await updateAttendance(status)
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (attendance == false) {
|
||||||
|
return (<ApologyMessage/>)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{attendance == null && (
|
||||||
|
<>
|
||||||
|
<h3>Присоеденишься?</h3>
|
||||||
|
<p>(Можно и потом ответить)</p>
|
||||||
|
<button style={localStyles.buttonOk} onClick={() => handleUpdate(true)}>Да!</button>
|
||||||
|
<button style={localStyles.buttonNok} onClick={() => handleUpdate(false)}>Не смогу в этот раз</button>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
{attendance == true && (
|
||||||
|
<>
|
||||||
|
<h3>Круто! Ты с нами!</h3>
|
||||||
|
<p>Если все же по разным обстоятельствам ты не сможешь/не захочешь, то всегда можно передумать</p>
|
||||||
|
<button style={localStyles.buttonNok} onClick={() => handleUpdate(false)}>Все же никак</button>
|
||||||
|
</>)}
|
||||||
|
</>)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Greeting() {
|
||||||
|
const [cookie] = useCookies<string>(['userName'])
|
||||||
|
const userName = cookie.userName;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<CenteredContainer>
|
<CenteredContainer>
|
||||||
@ -24,11 +58,9 @@ function Greeting() {
|
|||||||
Приглашаем тебя отпраздновать предстоящий Новый Год <b>2025-2026</b> с нами в сосновой избе, в которой, ко всему прочему, будет праздноваться годовщина нашей жизни в ней!
|
Приглашаем тебя отпраздновать предстоящий Новый Год <b>2025-2026</b> с нами в сосновой избе, в которой, ко всему прочему, будет праздноваться годовщина нашей жизни в ней!
|
||||||
|
|
||||||
Наши двери открыты с <b>30.12.2025</b>. Праздник обычно длится до <b>01.01.2025</b>, но если тебе или твоим спутникам будет безумно плохо, то можно остаться и до второго числа.
|
Наши двери открыты с <b>30.12.2025</b>. Праздник обычно длится до <b>01.01.2025</b>, но если тебе или твоим спутникам будет безумно плохо, то можно остаться и до второго числа.
|
||||||
<h3>Присоеденишься?</h3>
|
|
||||||
<p>(Можно и потом ответить)</p>
|
|
||||||
<button style={localStyles.buttonOk} onClick={() => handleUpdate(true)}>Да!</button>
|
|
||||||
<button style={localStyles.buttonNok}onClick={() => handleUpdate(false)}>Не смогу в этот раз</button>
|
|
||||||
</p>
|
</p>
|
||||||
|
<Attendance/>
|
||||||
</CenteredContainer>
|
</CenteredContainer>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import { useCookies } from 'react-cookie';
|
|||||||
import { GUESTS } from '../constants/constants';
|
import { GUESTS } from '../constants/constants';
|
||||||
import useFetchUser from '../utils/fetchUser'; // Import your custom hook
|
import useFetchUser from '../utils/fetchUser'; // Import your custom hook
|
||||||
import { useNotification } from '../NotificationContext';
|
import { useNotification } from '../NotificationContext';
|
||||||
import ApologyMessage from './Attendance';
|
|
||||||
import {Loading} from './Loading';
|
import {Loading} from './Loading';
|
||||||
|
|
||||||
const InitialSetup = () => {
|
const InitialSetup = () => {
|
||||||
@ -13,9 +12,8 @@ const InitialSetup = () => {
|
|||||||
const [isSubmitted, setIsSubmitted] = useState(false);
|
const [isSubmitted, setIsSubmitted] = useState(false);
|
||||||
const [password, setPassword] = useState('');
|
const [password, setPassword] = useState('');
|
||||||
const [isPasswordSet, setIsPasswordSet] = useState(false); // To track if password is set
|
const [isPasswordSet, setIsPasswordSet] = useState(false); // To track if password is set
|
||||||
const [userAttendance, setUserAttendance] = useState<boolean | null>(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 notify = useNotification();
|
||||||
|
|
||||||
const checkUserPassword = async (name: string) => {
|
const checkUserPassword = async (name: string) => {
|
||||||
@ -35,14 +33,8 @@ const InitialSetup = () => {
|
|||||||
setIsSubmitted(isTokenValid);
|
setIsSubmitted(isTokenValid);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getUserAttendance = async () => {
|
|
||||||
const attendance = await getAttendance()
|
|
||||||
setUserAttendance(attendance)
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (cookie.apiToken !== undefined) {
|
if (cookie.apiToken !== undefined) {
|
||||||
getUserAttendance()
|
|
||||||
validateToken();
|
validateToken();
|
||||||
}
|
}
|
||||||
}, [cookie.apiToken]);
|
}, [cookie.apiToken]);
|
||||||
@ -66,16 +58,11 @@ const InitialSetup = () => {
|
|||||||
validateToken()
|
validateToken()
|
||||||
};
|
};
|
||||||
|
|
||||||
if (isSubmitted && userAttendance !== false) {
|
if (isSubmitted) {
|
||||||
console.log('Selected', selectedName);
|
console.log('Selected', selectedName);
|
||||||
return null; // or you can redirect to another component or page
|
return null; // or you can redirect to another component or page
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userAttendance == false) {
|
|
||||||
return (
|
|
||||||
<ApologyMessage/>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -88,7 +88,7 @@ const useFetchHosting = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Optional: Fetch the updated data after reservation
|
// Optional: Fetch the updated data after reservation
|
||||||
await fetchData();
|
//await fetchData();
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -114,6 +114,28 @@ const useFetchUser = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error retrieving attendance:', error);
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const updateAttendance = async (attendanceStatus: boolean): Promise<boolean> => {
|
const updateAttendance = async (attendanceStatus: boolean): Promise<boolean> => {
|
||||||
const token = apiCookie.apiToken
|
const token = apiCookie.apiToken
|
||||||
try {
|
try {
|
||||||
@ -140,28 +162,6 @@ const useFetchUser = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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, isLoading };
|
return { userSet, passwordCreate, signUser, validToken, updateAttendance, getAttendance, isLoading };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user