el.xwx.moe/components/Modal/User/ProfileSettings.tsx

155 lines
5.2 KiB
TypeScript
Raw Normal View History

import { Dispatch, SetStateAction, useEffect } from "react";
2023-05-16 12:08:28 -05:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faClose } from "@fortawesome/free-solid-svg-icons";
2023-05-18 13:02:05 -05:00
import useAccountStore from "@/store/account";
2023-05-20 14:25:00 -05:00
import { AccountSettings } from "@/types/global";
2023-05-21 04:54:42 -05:00
import { useSession } from "next-auth/react";
2023-05-22 07:20:48 -05:00
import { resizeImage } from "@/lib/client/resizeImage";
import { faPenToSquare } from "@fortawesome/free-regular-svg-icons";
import SubmitButton from "../../SubmitButton";
import ProfilePhoto from "../../ProfilePhoto";
2023-05-16 12:08:28 -05:00
type Props = {
toggleSettingsModal: Function;
setUser: Dispatch<SetStateAction<AccountSettings>>;
user: AccountSettings;
2023-05-16 12:08:28 -05:00
};
export default function ProfileSettings({
toggleSettingsModal,
setUser,
user,
}: Props) {
2023-05-21 04:54:42 -05:00
const { update } = useSession();
2023-05-20 14:25:00 -05:00
const { account, updateAccount } = useAccountStore();
2023-05-22 07:20:48 -05:00
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);
2023-05-20 14:25:00 -05:00
2023-05-22 07:20:48 -05:00
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.");
}
2023-05-18 13:02:05 -05:00
};
2023-05-16 12:08:28 -05:00
useEffect(() => {
setUser({ ...user, oldPassword: undefined, newPassword: undefined });
}, []);
2023-05-18 13:02:05 -05:00
const submit = async () => {
2023-05-27 13:00:48 -05:00
const response = await updateAccount({
...user,
});
setUser({ ...user, oldPassword: undefined, newPassword: undefined });
2023-05-20 14:25:00 -05:00
2023-05-22 07:20:48 -05:00
if (user.email !== account.email || user.name !== account.name)
2023-05-21 04:54:42 -05:00
update({ email: user.email, name: user.name });
2023-05-27 13:00:48 -05:00
if (response) toggleSettingsModal();
2023-05-16 12:08:28 -05:00
};
return (
<div className="flex flex-col gap-3 justify-between sm:w-[35rem] w-80">
2023-05-18 13:02:05 -05:00
<div className="grid sm:grid-cols-2 gap-3 auto-rows-auto">
<div className="sm:row-span-2 sm:justify-self-center mx-auto mb-3">
<p className="text-sm text-sky-500 mb-2 text-center">Profile Photo</p>
2023-05-18 13:02:05 -05:00
<div className="w-28 h-28 flex items-center justify-center border border-sky-100 rounded-full relative">
2023-06-08 08:39:22 -05:00
<ProfilePhoto
src={user.profilePic}
className="h-28 w-28 border-[1px]"
/>
{user.profilePic && (
<div
onClick={() =>
setUser({
...user,
profilePic: "",
})
}
className="absolute top-1 left-1 w-5 h-5 flex items-center justify-center border p-1 bg-white border-slate-200 rounded-full text-gray-500 hover:text-red-500 duration-100 cursor-pointer"
>
<FontAwesomeIcon icon={faClose} className="w-3 h-3" />
2023-05-22 07:20:48 -05:00
</div>
)}
2023-05-18 13:02:05 -05:00
<div className="absolute -bottom-2 left-0 right-0 mx-auto w-fit text-center">
<label
htmlFor="upload-photo"
title="PNG or JPG (Max: 3MB)"
2023-06-08 08:39:22 -05:00
className="border border-slate-200 rounded-md bg-white px-2 text-center select-none cursor-pointer text-sky-900 duration-100 hover:border-sky-500"
2023-05-18 13:02:05 -05:00
>
Browse...
<input
type="file"
name="photo"
id="upload-photo"
2023-05-22 07:20:48 -05:00
accept=".png, .jpeg, .jpg"
2023-05-18 13:02:05 -05:00
className="hidden"
2023-05-22 07:20:48 -05:00
onChange={handleImageUpload}
2023-05-18 13:02:05 -05:00
/>
</label>
</div>
</div>
2023-05-22 07:20:48 -05:00
</div>
<div className="flex flex-col gap-3">
<div>
<p className="text-sm text-sky-500 mb-2">Display Name</p>
<input
type="text"
value={user.name}
onChange={(e) => 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"
/>
</div>
<div>
<p className="text-sm text-sky-500 mb-2">Email</p>
<input
type="text"
value={user.email}
onChange={(e) => 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"
/>
</div>
</div>
2023-05-18 13:02:05 -05:00
</div>
{/* <hr /> TODO: Export functionality
2023-05-18 13:02:05 -05:00
<p className="text-sky-600">Data Settings</p>
<div className="w-fit">
<div className="border border-sky-100 rounded-md bg-white px-2 py-1 text-center select-none cursor-pointer text-sky-900 duration-100 hover:border-sky-500">
Export Data
</div>
</div> */}
2023-05-31 13:33:01 -05:00
<SubmitButton
2023-05-22 07:20:48 -05:00
onClick={submit}
2023-05-31 13:33:01 -05:00
label="Apply Settings"
icon={faPenToSquare}
className="mx-auto mt-2"
/>
2023-05-16 12:08:28 -05:00
</div>
);
}