import { createRef, useCallback, useEffect, useState } from "react"; import { User } from "../network/models"; import { network } from "../network/network"; import showToast from "../components/toast"; import Loading from "../components/loading"; import { MdMoreHoriz, MdSearch } from "react-icons/md"; import Pagination from "../components/pagination"; import showPopup, { PopupMenuItem } from "../components/popup"; import { useTranslation } from "react-i18next"; export default function UserView() { const { t } = useTranslation(); const [searchKeyword, setSearchKeyword] = useState(""); const [page, setPage] = useState(1); const [totalPages, setTotalPages] = useState(0); return <>
{ e.preventDefault(); setPage(0); const input = e.currentTarget.querySelector("input[type=search]") as HTMLInputElement; setSearchKeyword(input.value); }}>
{totalPages ? : null}
} function UserTable({ page, searchKeyword, totalPagesCallback }: { page: number, searchKeyword: string, totalPagesCallback: (totalPages: number) => void }) { const { t } = useTranslation(); const [users, setUsers] = useState(null); const fetchUsers = useCallback(() => { if (searchKeyword) { network.searchUsers(searchKeyword, page).then((response) => { if (response.success) { setUsers(response.data!); totalPagesCallback(response.totalPages!); } else { showToast({ type: "error", message: response.message, }) } }); } else { network.listUsers(page).then((response) => { if (response.success) { setUsers(response.data!); totalPagesCallback(response.totalPages!); } else { showToast({ type: "error", message: response.message, }) } }); } }, [page, searchKeyword, totalPagesCallback]); useEffect(() => { fetchUsers(); }, [page, searchKeyword]); const handleChanged = useCallback(async () => { setUsers(null); fetchUsers(); }, [fetchUsers]); if (users === null) { return ; } return
{ users.map((u) => { return }) }
{t("Username")} {t("Created At")} {t("Admin")} {t("Can Upload")} {t("Actions")}
} function UserRow({ user, onChanged }: { user: User, onChanged: () => void }) { const { t } = useTranslation(); const [isLoading, setIsLoading] = useState(false); const buttonRef = createRef(); const handleDelete = async () => { if (isLoading) { return; } setIsLoading(true); const res = await network.deleteUser(user.id); if (res.success) { showToast({ type: "success", message: t("User deleted successfully"), }); onChanged(); } else { showToast({ type: "error", message: res.message, }); } setIsLoading(false); } const handleSetAdmin = async () => { if (isLoading) { return; } setIsLoading(true); const res = await network.setUserAdmin(user.id, true); if (res.success) { showToast({ type: "success", message: t("User set as admin successfully"), }); onChanged(); } else { showToast({ type: "error", message: res.message, }); } setIsLoading(false); } const handleSetUser = async () => { if (isLoading) { return; } setIsLoading(true); const res = await network.setUserAdmin(user.id, false); if (res.success) { showToast({ type: "success", message: t("User set as user successfully"), }); onChanged(); } else { showToast({ type: "error", message: res.message, }); } setIsLoading(false); } const handleSetUploadPermission = async () => { if (isLoading) { return; } setIsLoading(true); const res = await network.setUserUploadPermission(user.id, true); if (res.success) { showToast({ type: "success", message: t("User set as upload permission successfully"), }); onChanged(); } else { showToast({ type: "error", message: res.message, }); } setIsLoading(false); } const handleRemoveUploadPermission = async () => { if (isLoading) { return; } setIsLoading(true); const res = await network.setUserUploadPermission(user.id, false); if (res.success) { showToast({ type: "success", message: t("User removed upload permission successfully"), }); onChanged(); } else { showToast({ type: "error", message: res.message, }); } setIsLoading(false); } return {user.username} {(new Date(user.created_at)).toLocaleDateString()} {user.is_admin ? t("Yes") : t("No")} {user.can_upload ? t("Yes") : t("No")}

{t("Delete User")}

{t("Are you sure you want to delete user")} {user.username}? {t("This action cannot be undone.")}

}