Spaces:
Build error
Build error
File size: 10,876 Bytes
fdaf912 c9a51c1 fdaf912 c9a51c1 fdaf912 |
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
"use client";
import { useState, useEffect } from 'react';
import { Skeleton } from "@nextui-org/react";
import Image from 'next/image';
import { User2, SlidersHorizontal, Info, Trash, RefreshCcw, Save } from 'lucide-react';
import { HeaderNavLink } from '@/app/components/ui/navlink';
import Swal from 'sweetalert2';
import { useSession } from 'next-auth/react';
const ProfileSection: React.FC = () => {
const [userId, setUserId] = useState('');
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [imageURL, setImageURL] = useState('');
const [isLoaded, setIsLoaded] = useState(false);
const [isAdmin, setIsAdmin] = useState(false);
const [initialProfileData, setInitialProfileData] = useState({ name: '', email: '', imageURL: '' });
const { data: session, status } = useSession();
const supabaseAccessToken = session?.supabaseAccessToken;
const handleNameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setName(event.target.value);
};
const handleEmailChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setEmail(event.target.value);
};
const handleImageURLChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
setImageURL(event.target.value);
};
const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
if (!isProfileChanged()) {
console.log('No changes detected, not submitting the form');
return;
}
Swal.fire({
title: 'Are you sure?',
text: 'Do you want to update your profile data? This action cannot be undone!',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#4caf50',
cancelButtonColor: '#b91c1c',
confirmButtonText: 'Yes',
cancelButtonText: 'No',
}).then(async (result) => {
if (result.isConfirmed) {
// Update the profile data in the database
if (await updateProfileData()) {
// Show a success message after updating the profile data
Swal.fire({
title: 'Profile Updated!',
text: 'Your profile data has been updated successfully.',
icon: 'success',
confirmButtonColor: '#4caf50',
});
} else {
// Show an error message if the profile data update failed
Swal.fire({
title: 'Error!',
text: 'Failed to update your profile data. Please try again later. (Check Console for more details)',
icon: 'error',
confirmButtonColor: '#4caf50',
});
}
}
});
};
const handleDeleteProfile = () => {
Swal.fire({
title: 'Are you sure?',
text: 'Do you want to delete your profile & data? This action cannot be undone, and your data will be deleted forever!',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#4caf50',
cancelButtonColor: '#b91c1c',
confirmButtonText: 'Yes',
cancelButtonText: 'No',
}).then(async (result) => {
if (result.isConfirmed) {
// Delete the profile data from the database
if (await deleteProfileData()) {
// Show a success message after deleting the profile data
Swal.fire({
title: 'Profile Deleted!',
text: 'Your profile data has been deleted successfully. You will be redirected to the home page.',
icon: 'success',
confirmButtonColor: '#4caf50',
});
// Redirect to the home page after deleting the profile data
window.location.href = '/';
} else {
// Show an error message if the profile data deletion failed
Swal.fire({
title: 'Error!',
text: 'Failed to delete your profile data. Please try again later. (Check Console for more details)',
icon: 'error',
confirmButtonColor: '#4caf50',
});
}
}
});
};
const handleResetProfile = () => {
setName(initialProfileData.name);
setEmail(initialProfileData.email);
setImageURL(initialProfileData.imageURL);
}
const checkAdminRole = async () => {
const response = await fetch('/api/admin/is-admin');
if (!response.ok) {
console.error('Failed to fetch admin data');
return;
}
const data = await response.json();
setIsAdmin(data.isAdmin);
console.log('Admin role fetched successfully! Data:', data);
};
// Update the profile data in the database, via PUT request
const updateProfileData = async () => {
const response = await fetch('/api/profile', {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
userId: userId,
name: name,
email: email,
image: imageURL,
}),
});
if (!response.ok) {
console.error('Failed to update profile data:', response.statusText);
return false;
}
console.log('Profile data updated successfully!');
// Update initial profile data to the new data
setInitialProfileData({ name, email, imageURL });
return true;
};
// Fetch the profile data from the database, via GET request
const fetchProfileData = async () => {
const response = await fetch('/api/profile');
if (!response.ok) {
console.error('Failed to fetch profile data');
return;
}
const data = await response.json();
const userData = data.userData;
setUserId(userData.id);
setName(userData.name);
setEmail(userData.email);
setImageURL(userData.image);
setInitialProfileData({ name: userData.name, email: userData.email, imageURL: userData.image });
setIsLoaded(true);
console.log('Profile data fetched successfully! Data:', userData);
};
// Delete the profile data from the database, via DELETE request
const deleteProfileData = async () => {
const response = await fetch('/api/profile', {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${supabaseAccessToken}`, // Add the Supabase access token in the Authorization header
},
body: JSON.stringify({
userId: userId,
}),
});
if (!response.ok) {
console.error('Failed to delete profile data:', response.text);
return false;
}
console.log('Profile data deleted successfully!');
Swal.fire({
title: 'Profile Deleted!',
text: 'Your profile data has been deleted successfully.',
icon: 'success',
confirmButtonColor: '#4caf50',
});
return true;
};
useEffect(() => {
fetchProfileData();
checkAdminRole();
}, []);
const isProfileChanged = () => {
return (
name !== initialProfileData.name ||
email !== initialProfileData.email ||
imageURL !== initialProfileData.imageURL
);
};
return (
<div className="rounded-xl shadow-xl p-4 max-w-5xl w-full bg-white dark:bg-zinc-700/30">
<div className="space-y-2 p-4">
<div className="flex flex-col w-full justify-center gap-4">
<div className="flex justify-between items-center">
<div className='flex flex-col'>
<h1 className='flex font-bold text-2xl mb-4'>Profile</h1>
<Skeleton isLoaded={isLoaded} className='rounded-full w-20'>
{imageURL ? <Image className="rounded-full" src={imageURL} alt={name} width={84} height={84} priority={true} /> : <User2 size={84} />}
</Skeleton>
</div>
<div className="flex flex-col gap-4 justify-between">
{isAdmin ? (
<HeaderNavLink href="/admin" title='Admin Page'>
<button className="flex flex-grow justify-center items-center bg-blue-500 text-white rounded-md px-5 py-3 transition duration-300 ease-in-out transform hover:scale-105">
<SlidersHorizontal className="mr-1 h-5 w-5" />
Admin Page
</button>
</HeaderNavLink>
) : null}
<button
className="flex flex-grow justify-center items-center font-bold bg-red-500 text-white rounded-md px-5 py-3 transition duration-300 ease-in-out transform hover:scale-105"
onClick={handleDeleteProfile}
>
<Trash className="mr-1 h-5 w-5" />
Delete Account & Data
</button>
</div>
</div>
<form onSubmit={handleSubmit} className='flex flex-col gap-4 max-w-2xl'>
<label className="flex flex-col">
<span className='mb-2'>Name:</span>
<Skeleton isLoaded={isLoaded} className="rounded-lg">
<input className='h-10 rounded-lg w-full bg-gray-300 dark:bg-zinc-700/65 border px-2' type="text" value={name} onChange={handleNameChange} />
</Skeleton>
</label>
<label className="flex flex-col">
<span className='mb-2'>Email:</span>
<Skeleton isLoaded={isLoaded} className="rounded-lg">
<input className='h-10 rounded-lg w-full bg-gray-300 dark:bg-zinc-700/65 border px-2' type="email" value={email} onChange={handleEmailChange} />
</Skeleton>
</label>
<label className="flex flex-col">
<span className='mb-2'>Image URL:</span>
<Skeleton isLoaded={isLoaded} className="rounded-lg">
<textarea className='h-14 rounded-lg w-full bg-gray-300 dark:bg-zinc-700/65 border px-2' value={imageURL} onChange={handleImageURLChange} />
</Skeleton>
<span className='flex items-center text-sm text-gray-500 dark:text-gray-400'>
<Info className='h-4 w-4 mr-1' />
GoogleUserContent, Imgur, Gravatar URLs allowed.
</span>
</label>
<div className="flex justify-evenly gap-4">
<button type="button" onClick={handleResetProfile} className="flex flex-grow justify-center items-center bg-red-500 font-bold text-white rounded-md px-5 py-3 transition duration-300 ease-in-out transform hover:scale-105">
<RefreshCcw className="mr-1 h-5 w-5" />
Reset
</button>
<button type="submit" disabled={!isProfileChanged()} className="flex flex-grow justify-center items-center text-l disabled:bg-transparent disabled:border disabled:border-gray-500 disabled:text-gray-500 bg-blue-500 text-white px-6 py-3 rounded-md font-bold transition duration-300 ease-in-out transform hover:scale-105 disabled:hover:scale-100">
<Save className="mr-1 h-5 w-5" />
Save
</button>
</div>
</form>
</div>
</div>
</div>
);
};
export default ProfileSection;
|