import React, { useRef } from "react"; import useSWR, { useSWRConfig } from "swr"; import { AdminData } from "../api/adminsRoute.ts"; import { fetchApi, handleResponse } from "./apiClient.ts"; export function AdminsPage(props: { sessionId: string | null }) { const { sessionId } = props; const { mutate } = useSWRConfig(); const addDialogRef = useRef(null); const getSession = useSWR( sessionId ? ["/sessions/:sessionId", { params: { sessionId } }] as const : null, (args) => fetchApi(...args).then(handleResponse), ); const getUser = useSWR( getSession.data?.userId ? ["/users/:userId", { params: { userId: String(getSession.data.userId) } }] as const : null, (args) => fetchApi(...args).then(handleResponse), ); const getAdmins = useSWR( ["/admins", { method: "GET" }] as const, (args) => fetchApi(...args).then(handleResponse), ); return ( <> {getUser.data && getAdmins.data && getAdmins.data.length === 0 && ( )} {getAdmins.data?.length ? ( ) : getAdmins.data?.length === 0 ? ( ) : getAdmins.error ?

Loading admins failed

:
} {getUser.data?.admin && ( )} ); } function AddAdminDialog(props: { dialogRef: React.RefObject; sessionId: string | null; }) { const { dialogRef, sessionId } = props; const { mutate } = useSWRConfig(); return (
{ e.preventDefault(); const data = new FormData(e.target as HTMLFormElement); fetchApi("/admins", { method: "POST", query: { sessionId: sessionId! }, body: { tgUserId: Number(data.get("tgUserId") as string), }, }).then(handleResponse).then(() => mutate(() => true)); dialogRef.current?.close(); }} >
); } function AdminListItem(props: { admin: AdminData; sessionId: string | null }) { const { admin, sessionId } = props; const deleteDialogRef = useRef(null); const getAdminUser = useSWR( ["/users/:userId", { params: { userId: String(admin.tgUserId) } }] as const, (args) => fetchApi(...args).then(handleResponse), ); const getSession = useSWR( sessionId ? ["/sessions/:sessionId", { params: { sessionId } }] as const : null, (args) => fetchApi(...args).then(handleResponse), ); const getUser = useSWR( getSession.data?.userId ? ["/users/:userId", { params: { userId: String(getSession.data.userId) } }] as const : null, (args) => fetchApi(...args).then(handleResponse), ); return (
  • {getAdminUser.data?.first_name ?? admin.tgUserId} {getAdminUser.data?.last_name}{" "} {getAdminUser.data?.username ? ( @{getAdminUser.data.username} ) : null}

    {getAdminUser.data?.bio ? (

    {getAdminUser.data?.bio}

    ) : null} {getUser.data?.admin && (

    )}
  • ); } function DeleteAdminDialog(props: { dialogRef: React.RefObject; adminId: number; sessionId: string | null; }) { const { dialogRef, adminId, sessionId } = props; const { mutate } = useSWRConfig(); return (
    { e.preventDefault(); fetchApi("/admins/:adminId", { method: "DELETE", query: { sessionId: sessionId! }, params: { adminId: String(adminId) }, }).then(handleResponse).then(() => mutate(() => true)); dialogRef.current?.close(); }} >

    Are you sure you want to delete this admin?

    ); }