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

196 lines
6.1 KiB
TypeScript
Raw Normal View History

2023-06-11 08:55:36 -05:00
import { Dispatch, SetStateAction, useEffect, useState } 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-07-16 10:51:24 -05:00
import { signOut, 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";
import { toast } from "react-hot-toast";
2023-08-17 15:05:44 -05:00
import TextInput from "@/components/TextInput";
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
};
const emailEnabled = process.env.NEXT_PUBLIC_EMAIL_PROVIDER;
2023-07-12 13:26:34 -05:00
export default function ProfileSettings({
toggleSettingsModal,
setUser,
user,
}: Props) {
2023-07-19 15:39:59 -05:00
const { update, data } = useSession();
2023-05-20 14:25:00 -05:00
const { account, updateAccount } = useAccountStore();
2023-06-11 08:55:36 -05:00
const [profileStatus, setProfileStatus] = useState(true);
const [submitLoader, setSubmitLoader] = useState(false);
2023-06-11 08:55:36 -05:00
const handleProfileStatus = (e: boolean) => {
setProfileStatus(!e);
};
2023-05-20 14:25:00 -05:00
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 {
toast.error("Please select a PNG or JPEG file thats less than 1MB.");
2023-05-22 07:20:48 -05:00
}
} else {
toast.error("Invalid file format.");
2023-05-22 07:20:48 -05:00
}
2023-05-18 13:02:05 -05:00
};
2023-05-16 12:08:28 -05:00
useEffect(() => {
2023-07-12 13:26:34 -05:00
setUser({ ...user, newPassword: undefined });
}, []);
2023-05-18 13:02:05 -05:00
const submit = async () => {
setSubmitLoader(true);
const load = toast.loading("Applying...");
2023-05-27 13:00:48 -05:00
const response = await updateAccount({
...user,
});
toast.dismiss(load);
if (response.ok) {
toast.success("Settings Applied!");
2023-07-19 15:51:53 -05:00
if (user.email !== account.email) {
2023-07-18 11:44:37 -05:00
update({
2023-07-19 15:39:59 -05:00
id: data?.user.id,
2023-07-18 11:44:37 -05:00
});
2023-05-20 14:25:00 -05:00
2023-07-18 11:44:37 -05:00
signOut();
2023-07-19 15:51:53 -05:00
} else if (
user.username !== account.username ||
user.name !== account.name
)
update({
id: data?.user.id,
});
2023-07-16 10:51:24 -05:00
2023-07-12 13:26:34 -05:00
setUser({ ...user, newPassword: undefined });
toggleSettingsModal();
2023-07-18 11:44:37 -05:00
} else toast.error(response.data as string);
setSubmitLoader(false);
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">
2023-08-11 00:11:02 -05:00
<p className="text-sm text-black dark:text-white mb-2 text-center">
2023-08-10 23:44:44 -05:00
Profile Photo
</p>
<div className="w-28 h-28 flex items-center justify-center rounded-full relative">
2023-06-08 08:39:22 -05:00
<ProfilePhoto
src={user.profilePic}
className="h-auto border-none w-28"
2023-06-11 08:55:36 -05:00
status={handleProfileStatus}
2023-06-08 08:39:22 -05:00
/>
2023-06-11 08:55:36 -05:00
{profileStatus && (
2023-06-08 08:39:22 -05:00
<div
onClick={() =>
setUser({
...user,
profilePic: "",
})
}
className="absolute top-1 left-1 w-5 h-5 flex items-center justify-center border p-1 border-slate-200 dark:border-neutral-700 rounded-full bg-white dark:bg-neutral-800 text-center select-none cursor-pointer duration-100 hover:text-red-500"
2023-06-08 08:39:22 -05:00
>
<FontAwesomeIcon icon={faClose} className="w-3 h-3" />
2023-05-22 07:20:48 -05:00
</div>
)}
2023-07-18 17:14:26 -05:00
<div className="absolute -bottom-3 left-0 right-0 mx-auto w-fit text-center">
2023-05-18 13:02:05 -05:00
<label
htmlFor="upload-photo"
title="PNG or JPG (Max: 3MB)"
2023-08-14 22:25:25 -05:00
className="border border-slate-200 dark:border-neutral-700 rounded-md bg-white dark:bg-neutral-800 px-2 text-center select-none cursor-pointer duration-100 hover:border-sky-300 hover:dark:border-sky-600"
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>
2023-08-11 00:11:02 -05:00
<p className="text-sm text-black dark:text-white mb-2">
2023-08-10 23:44:44 -05:00
Display Name
</p>
2023-08-17 15:05:44 -05:00
<TextInput
value={user.name || ""}
onChange={(e) => setUser({ ...user, name: e.target.value })}
/>
</div>
<div>
2023-08-11 00:11:02 -05:00
<p className="text-sm text-black dark:text-white mb-2">Username</p>
2023-08-17 15:05:44 -05:00
<TextInput
2023-07-19 15:39:59 -05:00
value={user.username || ""}
2023-07-08 05:35:43 -05:00
onChange={(e) => setUser({ ...user, username: e.target.value })}
/>
</div>
2023-07-12 13:26:34 -05:00
{emailEnabled ? (
2023-07-12 13:26:34 -05:00
<div>
2023-08-15 20:29:38 -05:00
<p className="text-sm text-black dark:text-white mb-2">Email</p>
2023-08-17 15:05:44 -05:00
<TextInput
2023-07-12 13:26:34 -05:00
value={user.email || ""}
onChange={(e) => setUser({ ...user, email: e.target.value })}
/>
</div>
) : undefined}
2023-07-19 13:00:45 -05:00
2023-07-19 15:51:53 -05:00
{user.email !== account.email ? (
2023-07-19 13:00:45 -05:00
<p className="text-gray-500">
2023-07-19 15:51:53 -05:00
You will need to log back in after you apply this Email.
2023-07-19 13:00:45 -05:00
</p>
) : undefined}
</div>
2023-05-18 13:02:05 -05:00
</div>
2023-05-31 13:33:01 -05:00
<SubmitButton
2023-05-22 07:20:48 -05:00
onClick={submit}
loading={submitLoader}
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>
);
}