// Copyright (C) 2022-present Daniel31x13 // This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3. // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. // You should have received a copy of the GNU General Public License along with this program. If not, see . import { useEffect, useState } from "react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faUser, faClose } from "@fortawesome/free-solid-svg-icons"; import Checkbox from "../Checkbox"; import useAccountStore from "@/store/account"; import { AccountSettings } from "@/types/global"; import { useSession } from "next-auth/react"; import { resizeImage } from "@/lib/client/resizeImage"; import fileExists from "@/lib/client/fileExists"; import Modal from "."; import ChangePassword from "./ChangePassword"; import { faPenToSquare } from "@fortawesome/free-regular-svg-icons"; type Props = { toggleSettingsModal: Function; }; export default function UserSettings({ toggleSettingsModal }: Props) { const { update } = useSession(); const { account, updateAccount } = useAccountStore(); const [user, setUser] = useState({ ...account, profilePic: null, }); const [whitelistedUsersTextbox, setWhiteListedUsersTextbox] = useState( user.whitelistedUsers.join(", ") ); const [passwordFormModal, setPasswordFormModal] = useState(false); const togglePasswordFormModal = () => { setPasswordFormModal(!passwordFormModal); }; const setPasswordForm = (oldPassword?: string, newPassword?: string) => { setUser({ ...user, oldPassword, newPassword }); }; useEffect(() => { const determineProfilePicSource = async () => { const path = `/api/avatar/${account.id}`; const imageExists = await fileExists(path).catch((e) => console.log(e)); if (imageExists) setUser({ ...user, profilePic: path }); }; determineProfilePicSource(); }, []); useEffect(() => { setUser({ ...user, whitelistedUsers: stringToArray(whitelistedUsersTextbox), }); }, [whitelistedUsersTextbox]); const stringToArray = (str: string) => { const stringWithoutSpaces = str.replace(/\s+/g, ""); const wordsArray = stringWithoutSpaces.split(","); return wordsArray; }; const handleImageUpload = async (e: any) => { const file: File = e.target.files[0]; const fileExtension = file.name.split(".").pop()?.toLowerCase(); const allowedExtensions = ["png", "jpeg", "jpg"]; if (allowedExtensions.includes(fileExtension as string)) { const resizedFile = await resizeImage(file); if ( resizedFile.size < 1048576 // 1048576 Bytes == 1MB ) { const reader = new FileReader(); reader.onload = () => { setUser({ ...user, profilePic: reader.result as string }); }; reader.readAsDataURL(resizedFile); } else { console.log("Please select a PNG or JPEG file thats less than 1MB."); } } else { console.log("Invalid file format."); } }; const submit = async () => { console.log(user); await updateAccount({ ...user, profilePic: user.profilePic === `/api/avatar/${account.id}` ? null : user.profilePic, }); setPasswordForm(undefined, undefined); if (user.email !== account.email || user.name !== account.name) update({ email: user.email, name: user.name }); }; return (

Settings

Profile Settings

{user.email !== account.email || user.name !== account.name ? (

Note: The page will be refreshed to apply the changes of "Email" or "Display Name".

) : null}

Display Name

setUser({ ...user, name: e.target.value })} className="w-full rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100" />

Email

setUser({ ...user, email: e.target.value })} className="w-full rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100" />

Password

Change Password
{user.newPassword && user.oldPassword ? (

Password modified. Please click{" "} "Apply Settings" to apply the changes..

) : null}

Profile Photo

{user.profilePic && user.profilePic !== "DELETE" ? (
Profile Photo
setUser({ ...user, profilePic: "DELETE", }) } className="absolute top-1 left-1 w-5 h-5 flex items-center justify-center border p-1 bg-white border-sky-100 rounded-full text-gray-500 hover:text-red-500 duration-100 cursor-pointer" >
) : ( )}
{/*
TODO: Export functionality

Data Settings

Export Data
*/}

Privacy Settings

setUser({ ...user, isPrivate: !user.isPrivate })} />

This will limit who can find and add you to other Collections.

{user.isPrivate ? (

Whitelisted Users

Please provide the Email addresses of the users you wish to grant visibility to your profile. Separate the addresses with a comma. Users not included will be unable to view your profile.

) : null}
Apply Settings
{passwordFormModal ? ( ) : null}
); }