import clsx from 'clsx'; import { useState, useEffect, useRef } from 'react'; import { toast } from 'react-hot-toast'; import { Orbit } from "@uiball/loaders"; import { useGetUserByAdminQuery, useUpdateUserMutation } from '../../../app/services/user'; import IconEdit from '../../../assets/icons/edit.svg'; import IconSave from '../../../assets/icons/save.svg'; import IconCancel from '../../../assets/icons/close.circle.svg'; type Props = { uid: number } const WebhookEdit = ({ uid }: Props) => { const formRef = useRef(null); const [editable, setEditable] = useState(false); const [url, setUrl] = useState(""); const { data, isSuccess, refetch } = useGetUserByAdminQuery(uid); const [updateUser, { isSuccess: updateSuccess, isLoading: isUpdating }] = useUpdateUserMutation(); useEffect(() => { if (isSuccess && data) { setUrl(data.webhook_url || ""); } }, [data, isSuccess]); useEffect(() => { if (updateSuccess) { refetch(); } }, [updateSuccess]); const handleEdit = async () => { if (editable && formRef) { const form = formRef.current; // 检查格式 if (!form?.checkValidity()) { form?.reportValidity(); return; } // 保存编辑 const webhook_url = new FormData(form).get("webhook") as string; const resp = await updateUser({ id: uid, webhook_url }); console.log("ressssss", resp); if ("error" in resp) { switch (resp.error.status) { case 406: toast.error("Not Valid URL!"); break; default: break; } return; } } setEditable(prev => !prev); }; const handleEditable = () => { setEditable(true); }; const handleCancelEdit = () => { setEditable(false); const form = formRef.current; if (form) { const input = form.querySelector("input"); input!.value = data?.webhook_url || ""; } }; return (
{(url || editable || updateSuccess) ?
{ evt.preventDefault(); handleEdit(); }}>
{editable && !isUpdating && }
: }
); }; export default WebhookEdit;