implement wihslist and secret santa
This commit is contained in:
@@ -1,7 +1,36 @@
|
||||
import CenteredContainer from "./ChildrenContainer";
|
||||
import useFetchUser from "../utils/fetchUser.tsx"
|
||||
import type { SantaInfo } from "../types/index";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useNotification } from "../NotificationContext.tsx";
|
||||
|
||||
function SecretSanta() {
|
||||
|
||||
const { updateWishlist, getSantaInfo } = useFetchUser()
|
||||
const [ santaInfo, setSantaInfo ] = useState<SantaInfo | null>(null)
|
||||
const [ wishListUrl, setWishListUrl ] = useState('')
|
||||
|
||||
const notify = useNotification();
|
||||
|
||||
const fetchSecretSanta = async () => {
|
||||
const santaInfoData = await getSantaInfo()
|
||||
setSantaInfo(santaInfoData)
|
||||
}
|
||||
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
||||
event.preventDefault()
|
||||
const updated = await updateWishlist(wishListUrl)
|
||||
|
||||
if (updated) {
|
||||
notify('Вишлсит обновлен', 'success')
|
||||
} else {
|
||||
notify('Не удалось обновить вишлист, все вопросы к админу', 'error')
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
fetchSecretSanta()
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<>
|
||||
<CenteredContainer>
|
||||
@@ -9,11 +38,33 @@ function SecretSanta() {
|
||||
<p className="mainText">
|
||||
Тут вы сможете узнать кому вы дарите свой подарок, а так же увидеть его вишлист, если он его добавит. Вы тоже сможете добавить свой вишлист, если захотите, чтобы ваш санта его видел!
|
||||
<br/><br/>
|
||||
Таблица в производстве... Ожидайте к <b>середине-концу ноября</b>
|
||||
<h3>Добавить свой вишлист</h3>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div>
|
||||
<label>
|
||||
Ссылка на вишлист:
|
||||
<input
|
||||
type="url"
|
||||
value={wishListUrl}
|
||||
onChange={(e) => setWishListUrl(e.target.value)}
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<button type="submit">
|
||||
{'Отправить'}
|
||||
</button>
|
||||
</form>
|
||||
<p>Вы санта для Пятки: <b>{santaInfo?.santa_to}</b></p>
|
||||
{santaInfo?.wishlist ? (
|
||||
<h4>Пятка оставила вам <a href={santaInfo.wishlist}>вишлсит</a></h4>
|
||||
): (
|
||||
<h4>Пятка не оставила вам вишлист, используйте свое воображение. Либо ждите пока добавит....</h4>
|
||||
)}
|
||||
</p>
|
||||
</CenteredContainer>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default SecretSanta;
|
||||
export default SecretSanta;
|
||||
|
||||
7
frontend/src/types/index.d.ts
vendored
7
frontend/src/types/index.d.ts
vendored
@@ -14,4 +14,9 @@ export interface Hosting {
|
||||
export interface User {
|
||||
attendance: boolean | null
|
||||
name: string
|
||||
}
|
||||
}
|
||||
|
||||
export interface SantaInfo {
|
||||
santa_to: string,
|
||||
wishlist: string
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useCookies } from 'react-cookie';
|
||||
import { API_URL } from '../constants/constants';
|
||||
import { hashPassword } from './hashPassword';
|
||||
import { useState } from 'react';
|
||||
import type { User } from '../types';
|
||||
import type { User, SantaInfo } from '../types';
|
||||
|
||||
const useFetchUser = () => {
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
@@ -137,6 +137,54 @@ const useFetchUser = () => {
|
||||
}
|
||||
}
|
||||
|
||||
const updateWishlist = async (wishlistUrl: string): Promise<boolean> => {
|
||||
const token = apiCookie.apiToken
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/users/wishlist`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
token,
|
||||
wishlist: wishlistUrl,
|
||||
}),
|
||||
});
|
||||
|
||||
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 wishlist:', error);
|
||||
return false; // Attendance update failed
|
||||
}
|
||||
}
|
||||
|
||||
const getSantaInfo = async (): Promise<SantaInfo | null> => {
|
||||
const token = apiCookie.apiToken
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/users/santa?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.santa_info
|
||||
} catch (error) {
|
||||
console.error('Error retrieving attendance:', error);
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
const updateAttendance = async (attendanceStatus: boolean): Promise<boolean> => {
|
||||
const token = apiCookie.apiToken
|
||||
try {
|
||||
@@ -186,7 +234,7 @@ const useFetchUser = () => {
|
||||
}
|
||||
|
||||
|
||||
return { userSet, passwordCreate, signUser, validToken, updateAttendance, getAttendance, isLoading, getAttendanceAll };
|
||||
return { userSet, passwordCreate, signUser, validToken, updateAttendance, updateWishlist, getAttendance, isLoading, getAttendanceAll, getSantaInfo };
|
||||
};
|
||||
|
||||
export default useFetchUser;
|
||||
|
||||
Reference in New Issue
Block a user