Files
fredy/ui/src/views/user/mutation/UserMutator.jsx

117 lines
3.5 KiB
React
Raw Normal View History

2025-12-11 10:40:55 +01:00
/*
* Copyright (c) 2026 by Christian Kellner.
2025-12-11 10:40:55 +01:00
* Licensed under Apache-2.0 with Commons Clause and Attribution/Naming Clause
*/
import React from 'react';
import { xhrGet, xhrPost } from '../../../services/xhr';
2025-09-02 20:18:37 +02:00
import { useNavigate, useParams } from 'react-router-dom';
import { useActions } from '../../../services/state/store';
2023-03-20 08:52:13 +01:00
import { Divider, Input, Switch, Button, Toast } from '@douyinfe/semi-ui';
import './UserMutator.less';
2022-03-27 19:42:58 +02:00
import { SegmentPart } from '../../../components/segment/SegmentPart';
2023-03-20 08:52:13 +01:00
import { IconPlusCircle } from '@douyinfe/semi-icons';
const UserMutator = function UserMutator() {
const params = useParams();
const [username, setUsername] = React.useState('');
const [password, setPassword] = React.useState('');
const [password2, setPassword2] = React.useState('');
const [isAdmin, setIsAdmin] = React.useState(false);
2025-09-02 20:18:37 +02:00
const navigate = useNavigate();
const actions = useActions();
2022-12-19 21:10:00 +01:00
React.useEffect(() => {
async function init() {
if (params.userId != null) {
try {
const userJson = await xhrGet(`/api/admin/users/${params.userId}`);
const user = userJson.json;
const defaultName = user?.username || '';
const defaultIsAdmin = user?.isAdmin || false;
setUsername(defaultName);
setIsAdmin(defaultIsAdmin);
} catch (Exception) {
console.error(Exception);
}
}
}
2023-03-20 08:52:13 +01:00
2022-12-19 21:10:00 +01:00
init();
}, [params.userId]);
const saveUser = async () => {
try {
await xhrPost('/api/admin/users', {
userId: params.userId || null,
username,
password,
password2,
isAdmin,
});
await actions.user.getUsers();
2023-03-20 08:52:13 +01:00
Toast.success('User successfully saved...');
2025-09-02 20:18:37 +02:00
navigate('/users');
2023-03-20 08:52:13 +01:00
} catch (error) {
console.error(error);
Toast.error(error.json.message);
}
};
return (
2023-03-20 08:52:13 +01:00
<form className="userMutator">
2022-03-27 19:42:58 +02:00
<SegmentPart name="Username" helpText="The username used to login to Fredy">
2023-03-20 08:52:13 +01:00
<Input
2022-03-27 19:42:58 +02:00
type="text"
label="Username"
maxLength={30}
placeholder="Username"
autoFocus
width={6}
2023-03-20 08:52:13 +01:00
value={username}
onChange={(val) => setUsername(val)}
2022-03-27 19:42:58 +02:00
/>
</SegmentPart>
2023-03-20 08:52:13 +01:00
<Divider margin="1rem" />
2022-03-27 19:42:58 +02:00
<SegmentPart name="Password" helpText="The password used to login to Fredy">
2023-03-20 08:52:13 +01:00
<Input
mode="password"
2022-03-27 19:42:58 +02:00
label="Password"
placeholder="Password"
width={6}
2023-03-20 08:52:13 +01:00
value={password}
onChange={(val) => setPassword(val)}
2022-03-27 19:42:58 +02:00
/>
</SegmentPart>
2023-03-20 08:52:13 +01:00
<Divider margin="1rem" />
2022-03-27 19:42:58 +02:00
<SegmentPart name="Retype password" helpText="Retype the password to make sure they match">
2023-03-20 08:52:13 +01:00
<Input
mode="password"
2022-03-27 19:42:58 +02:00
label="Retype password"
placeholder="Retype password"
width={6}
2023-03-20 08:52:13 +01:00
value={password2}
onChange={(val) => setPassword2(val)}
2022-03-27 19:42:58 +02:00
/>
</SegmentPart>
2023-03-20 08:52:13 +01:00
<Divider margin="1rem" />
<SegmentPart name="Is user an admin?" helpText="Check this if the user is an administrator">
<Switch checked={isAdmin} onChange={(checked) => setIsAdmin(checked)} />
2022-03-27 19:42:58 +02:00
</SegmentPart>
2023-03-20 08:52:13 +01:00
<Divider margin="1rem" />
2025-09-02 20:18:37 +02:00
<Button type="danger" style={{ marginRight: '1rem' }} onClick={() => navigate('/users')}>
Cancel
</Button>
2023-03-20 08:52:13 +01:00
<Button type="primary" icon={<IconPlusCircle />} onClick={saveUser}>
Save
</Button>
2023-03-20 08:52:13 +01:00
</form>
);
};
export default UserMutator;