-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathRequests.jsx
More file actions
213 lines (200 loc) · 6.21 KB
/
Requests.jsx
File metadata and controls
213 lines (200 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import React, { useContext, useEffect, useState } from "react";
import InfoCard from "../components/InfoCard";
import { RiseLoader } from "react-spinners";
import { useNavigate } from "react-router-dom";
import { AuthContext } from "../context/AuthContext";
import axios from "axios";
import { ExtraContext } from "../context/ExtraContext";
import { RUST_MAIN_URL } from "../utils/constant.js";
import { motion } from "framer-motion";
const Requests = () => {
const [boys, setBoys] = useState([]);
const [currentIndex, setCurrentIndex] = useState(0);
const [isLoading, setIsLoading] = useState(true);
const [currentUserDetails, setCurrentUserDetails] = useState(null);
const [noMoreUsers, setNoMoreUsers] = useState(false);
const navigate = useNavigate();
const { user } = useContext(AuthContext);
const { contestId, setContestId } = useContext(ExtraContext);
const color = "#ff0059";
// Fetch the list of boys
useEffect(() => {
const getBoys = async () => {
setIsLoading(true);
try {
const response = await axios.post(
`${RUST_MAIN_URL}getgirlrequests`,
{
email: user.email,
}
);
setBoys(response.data);
console.log("Boys data:", response.data);
} catch (error) {
console.error("Error fetching boys:", error);
} finally {
setIsLoading(false);
}
};
getBoys();
}, []);
// Fetch details of the current boy in the list
useEffect(() => {
const getCurrentUserDetails = async () => {
if (boys.length > 0 && currentIndex < boys.length) {
setIsLoading(true);
console.log("Loading", boys[currentIndex], currentIndex);
const currentBoy = boys[currentIndex];
console.log("Current boy:", currentBoy);
setContestId(currentBoy.id);
if (!currentBoy || !currentBoy.girl_email_id) {
console.error("Invalid boy data or missing email");
setIsLoading(false);
return;
}
try {
const response = await axios.post(
`${RUST_MAIN_URL}getuser`,
{
email: currentBoy.girl_email_id,
}
);
console.log("Fetched user details:", response.data);
setCurrentUserDetails(response.data);
} catch (error) {
console.error("Error fetching user details:", error);
} finally {
setIsLoading(false);
}
} else if (boys.length > 0) {
setNoMoreUsers(true);
}
};
getCurrentUserDetails();
}, [boys, currentIndex, setContestId]);
// Handle navigation and updating to the next user
const handleNextUser = () => {
setCurrentIndex((prevIndex) => {
if (prevIndex + 1 < boys.length) {
return prevIndex + 1;
} else {
setNoMoreUsers(true);
return prevIndex;
}
});
};
const onReject = async () => {
try {
const boyId = String(boys[currentIndex]?.id);
console.log("Current boy ID:", boyId);
if (!boyId) {
console.error("No valid boy ID found.");
return;
}
const response = await axios.post(
`${RUST_MAIN_URL}reject`,
{
boy_email: boyId,
girl_email: currentUserDetails.email
}
);
if (response.status === 200) {
console.log("Success:", response);
handleNextUser();
} else {
console.error("Failed with status code:", response.status);
handleNextUser();
}
} catch (error) {
console.error("Error updating flag:", error);
handleNextUser();
}
};
const onLike = async () => {
try {
const boyId = String(boys[currentIndex]?.id);
console.log("Current boy ID:", boyId);
if (!boyId) {
console.error("No valid boy ID found.");
return;
}
const response = await axios.post(
`${RUST_MAIN_URL}changeflag`,
{
email: boyId,
}
);
if (response.status === 202) {
console.log("Success:", response);
navigate("/coderun");
} else {
console.error("Failed with status code:", response.status);
handleNextUser();
}
} catch (error) {
console.error("Error updating flag:", error);
handleNextUser();
}
};
const emptyStateVariants = {
initial: { opacity: 0, y: 20 },
animate: { opacity: 1, y: 0, transition: { duration: 0.5 } },
};
const heartVariants = {
initial: { scale: 0 },
animate: { scale: [0, 1.2, 1], transition: { duration: 1, repeat: Infinity, repeatType: "reverse" } },
};
const renderContent = () => {
if (isLoading) {
return (
<div className="w-full h-full flex items-center justify-center">
<RiseLoader size={20} color="#ff0059" />
</div>
);
} else if (noMoreUsers || boys.length === 0) {
return (
<motion.div
className="text-center"
variants={emptyStateVariants}
initial="initial"
animate="animate"
>
<motion.div
className="text-[#ff0059] text-6xl mb-4"
variants={heartVariants}
initial="initial"
animate="animate"
>
❤️
</motion.div>
<h2 className="text-2xl font-bold mb-4 text-[#ff0059]">No More Matching Requests</h2>
<p className="text-gray-600 mb-6">Don't worry, love takes time. Keep your profile updated and stay active!</p>
<motion.button
className="bg-[#ff0059] text-white px-8 py-3 rounded-full font-semibold text-lg"
whileHover={{ scale: 1.05, boxShadow: "0 0 15px rgba(255,0,89,0.5)" }}
whileTap={{ scale: 0.95 }}
onClick={() => navigate("/explore")} // Assuming you have an explore page
>
Explore More Matches
</motion.button>
</motion.div>
);
} else if (currentUserDetails) {
return (
<InfoCard
user={currentUserDetails}
onLike={onLike}
onReject={onReject}
/>
);
}
};
return (
<div className="flex h-[95%]">
<div className="flex items-center justify-center h-[100%] w-[100%]">
{renderContent()}
</div>
</div>
);
};
export default Requests;