import { useTranslation } from "react-i18next";
import { app } from "../app";
import { ErrorAlert } from "../components/alert";
import { network } from "../network/network";
import { ReactNode, useState } from "react";
import {
MdOutlineAccountCircle,
MdLockOutline,
MdOutlineEditNote,
} from "react-icons/md";
import Button from "../components/button";
import showToast from "../components/toast";
import { useNavigator } from "../components/navigator";
import Input from "../components/input.tsx";
export function ManageMePage() {
const { t } = useTranslation();
if (!app.user) {
return (
);
}
return (
);
}
function ListTile({
title,
icon,
onClick,
}: {
title: string;
icon: ReactNode;
onClick: () => void;
}) {
return (
);
}
function ChangeAvatarDialog() {
const [avatar, setAvatar] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const navigator = useNavigator();
const { t } = useTranslation();
const selectAvatar = () => {
const input = document.createElement("input");
input.type = "file";
input.accept = "image/*";
input.onchange = (e) => {
const files = (e.target as HTMLInputElement).files;
if (files && files.length > 0) {
setAvatar(files[0]);
}
};
input.click();
};
const handleSubmit = async () => {
if (!avatar) {
return;
}
setIsLoading(true);
const res = await network.changeAvatar(avatar);
if (!res.success) {
setError(res.message);
} else {
app.user = res.data!;
navigator.refresh();
showToast({
message: t("Avatar changed successfully"),
type: "success",
});
const dialog = document.getElementById(
"change_avatar_dialog",
) as HTMLDialogElement;
if (dialog) {
dialog.close();
}
}
};
return (
<>
}
title={t("Change Avatar")}
onClick={() => {
const dialog = document.getElementById(
"change_avatar_dialog",
) as HTMLDialogElement;
if (dialog) {
dialog.showModal();
}
}}
/>
>
);
}
function ChangeUsernameDialog() {
const [newUsername, setNewUsername] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const navigator = useNavigator();
const { t } = useTranslation();
const handleSubmit = async () => {
if (!newUsername.trim()) {
setError(t("Username cannot be empty"));
return;
}
setIsLoading(true);
const res = await network.changeUsername(newUsername);
setIsLoading(false);
if (!res.success) {
setError(res.message);
} else {
app.user = res.data!;
navigator.refresh();
showToast({
message: t("Username changed successfully"),
type: "success",
});
const dialog = document.getElementById(
"change_username_dialog",
) as HTMLDialogElement;
if (dialog) {
dialog.close();
}
setNewUsername("");
setError(null);
}
};
return (
<>
}
title={t("Change Username")}
onClick={() => {
const dialog = document.getElementById(
"change_username_dialog",
) as HTMLDialogElement;
if (dialog) {
dialog.showModal();
}
}}
/>
>
);
}
function ChangePasswordDialog() {
const [oldPassword, setOldPassword] = useState("");
const [newPassword, setNewPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const { t } = useTranslation();
const handleSubmit = async () => {
// Validate input
if (!oldPassword || !newPassword || !confirmPassword) {
setError(t("All fields are required"));
return;
}
if (newPassword !== confirmPassword) {
setError(t("New passwords don't match"));
return;
}
if (newPassword.length < 6) {
setError(t("New password must be at least 6 characters long"));
return;
}
setIsLoading(true);
const res = await network.changePassword(oldPassword, newPassword);
setIsLoading(false);
if (!res.success) {
setError(res.message);
} else {
// Update the token as it might have changed
app.token = res.data!.token;
app.user = res.data!;
showToast({
message: t("Password changed successfully"),
type: "success",
});
const dialog = document.getElementById(
"change_password_dialog",
) as HTMLDialogElement;
if (dialog) {
dialog.close();
}
// Reset form
setOldPassword("");
setNewPassword("");
setConfirmPassword("");
setError(null);
}
};
return (
<>
}
title={t("Change Password")}
onClick={() => {
const dialog = document.getElementById(
"change_password_dialog",
) as HTMLDialogElement;
if (dialog) {
dialog.showModal();
}
}}
/>
>
);
}
function ChangeBioDialog() {
const [bio, setBio] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const { t } = useTranslation();
const handleSubmit = async () => {
if (!bio.trim()) {
setError(t("Bio cannot be empty"));
return;
} else if (bio.length > 200) {
setError(t("Bio cannot be longer than 200 characters"));
return;
}
setIsLoading(true);
const res = await network.changeBio(bio);
setIsLoading(false);
if (!res.success) {
setError(res.message);
} else {
app.user = res.data!;
showToast({
message: t("Bio changed successfully"),
type: "success",
});
const dialog = document.getElementById(
"change_bio_dialog",
) as HTMLDialogElement;
if (dialog) {
dialog.close();
}
setBio("");
setError(null);
}
};
return (
<>
}
title={t("Change Bio")}
onClick={() => {
const dialog = document.getElementById(
"change_bio_dialog",
) as HTMLDialogElement;
if (dialog) {
dialog.showModal();
}
}}
/>
>
);
}