import React, { useEffect, useState } from 'react'; import { Button, Form, Header, Segment } from 'semantic-ui-react'; import { useParams } from 'react-router-dom'; import { API, showError, showSuccess } from '../../helpers'; const EditUser = () => { const params = useParams(); const userId = params.id; const [loading, setLoading] = useState(true); const [inputs, setInputs] = useState({ username: '', display_name: '', password: '', github_id: '', wechat_id: '', email: '', quota: 0, }); const { username, display_name, password, github_id, wechat_id, email, quota } = inputs; const handleInputChange = (e, { name, value }) => { setInputs((inputs) => ({ ...inputs, [name]: value })); }; const loadUser = async () => { let res = undefined; if (userId) { res = await API.get(`/api/user/${userId}`); } else { res = await API.get(`/api/user/self`); } const { success, message, data } = res.data; if (success) { data.password = ''; setInputs(data); } else { showError(message); } setLoading(false); }; useEffect(() => { loadUser().then(); }, []); const submit = async () => { let res = undefined; if (userId) { let data = { ...inputs, id: parseInt(userId) }; if (typeof data.quota === 'string') { data.quota = parseInt(data.quota); } res = await API.put(`/api/user/`, data); } else { res = await API.put(`/api/user/self`, inputs); } const { success, message } = res.data; if (success) { showSuccess('用户信息更新成功!'); } else { showError(message); } }; return ( <>
更新用户信息
{ userId && ( ) }
); }; export default EditUser;