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 Modal from "."; import ChangePassword from "./ChangePassword"; import { faPenToSquare } from "@fortawesome/free-regular-svg-icons"; import SubmitButton from "../SubmitButton"; type Props = { toggleSettingsModal: Function; }; export default function UserSettings({ toggleSettingsModal }: Props) { const { update } = useSession(); const { account, updateAccount } = useAccountStore(); const [user, setUser] = useState({ ...account, }); 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(() => { 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 () => { const response = await updateAccount({ ...user, }); setPasswordForm(undefined, undefined); if (user.email !== account.email || user.name !== account.name) update({ email: user.email, name: user.name }); if (response) toggleSettingsModal(); }; return (

Settings

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
*/}

Profile Visibility

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. Separated by comma.