casdoor/web/src/UserEditPage.js

194 lines
5.7 KiB
JavaScript
Raw Normal View History

2020-10-20 23:14:03 +08:00
import React from "react";
2021-02-13 00:11:12 +08:00
import {Button, Card, Col, Input, Row, Select} from 'antd';
2020-10-20 23:14:03 +08:00
import * as UserBackend from "./backend/UserBackend";
2021-02-13 00:11:12 +08:00
import * as OrganizationBackend from "./backend/OrganizationBackend";
2020-10-20 23:14:03 +08:00
import * as Setting from "./Setting";
2021-02-13 00:11:12 +08:00
const { Option } = Select;
2020-10-20 23:14:03 +08:00
class UserEditPage extends React.Component {
constructor(props) {
super(props);
this.state = {
classes: props,
2021-02-13 00:11:12 +08:00
organizationName: props.match.params.organizationName,
2020-10-20 23:14:03 +08:00
userName: props.match.params.userName,
user: null,
2021-02-13 00:11:12 +08:00
organizations: [],
2020-10-20 23:14:03 +08:00
};
}
componentWillMount() {
this.getUser();
2021-02-13 00:11:12 +08:00
this.getOrganizations();
2020-10-20 23:14:03 +08:00
}
getUser() {
2021-02-13 00:11:12 +08:00
UserBackend.getUser(this.state.organizationName, this.state.userName)
2020-10-20 23:14:03 +08:00
.then((user) => {
this.setState({
user: user,
});
});
}
2021-02-13 00:11:12 +08:00
getOrganizations() {
OrganizationBackend.getOrganizations("admin")
.then((res) => {
this.setState({
organizations: res,
});
});
}
2020-10-20 23:14:03 +08:00
parseUserField(key, value) {
// if ([].includes(key)) {
// value = Setting.myParseInt(value);
// }
return value;
}
updateUserField(key, value) {
value = this.parseUserField(key, value);
let user = this.state.user;
user[key] = value;
this.setState({
user: user,
});
}
renderUser() {
return (
<Card size="small" title={
<div>
Edit User&nbsp;&nbsp;&nbsp;&nbsp;
<Button type="primary" onClick={this.submitUserEdit.bind(this)}>Save</Button>
</div>
} style={{marginLeft: '5px'}} type="inner">
<Row style={{marginTop: '10px'}} >
2021-02-13 00:11:12 +08:00
<Col style={{marginTop: '5px'}} span={2}>
Organization:
</Col>
<Col span={22} >
<Select virtual={false} style={{width: '100%'}} value={this.state.user.owner} onChange={(value => {this.updateUserField('owner', value);})}>
{
this.state.organizations.map((organization, index) => <Option key={index} value={organization.name}>{organization.name}</Option>)
}
</Select>
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
2020-10-20 23:14:03 +08:00
<Col style={{marginTop: '5px'}} span={2}>
Name:
</Col>
<Col span={22} >
<Input value={this.state.user.name} onChange={e => {
this.updateUserField('name', e.target.value);
}} />
</Col>
</Row>
2020-10-21 00:00:44 +08:00
<Row style={{marginTop: '20px'}} >
2020-10-20 23:14:03 +08:00
<Col style={{marginTop: '5px'}} span={2}>
2020-10-21 00:00:44 +08:00
Password Type:
2020-10-20 23:14:03 +08:00
</Col>
<Col span={22} >
2020-10-21 00:00:44 +08:00
<Input value={this.state.user.passwordType} onChange={e => {
this.updateUserField('passwordType', e.target.value);
2020-10-20 23:14:03 +08:00
}} />
</Col>
</Row>
2020-10-21 00:00:44 +08:00
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
Password:
</Col>
<Col span={22} >
<Input value={this.state.user.password} onChange={e => {
this.updateUserField('password', e.target.value);
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
Display Name:
</Col>
<Col span={22} >
<Input value={this.state.user.displayName} onChange={e => {
this.updateUserField('displayName', e.target.value);
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
Email:
</Col>
<Col span={22} >
<Input value={this.state.user.email} onChange={e => {
this.updateUserField('email', e.target.value);
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
2020-10-20 23:14:03 +08:00
<Col style={{marginTop: '5px'}} span={2}>
2020-10-21 00:00:44 +08:00
Phone:
2020-10-20 23:14:03 +08:00
</Col>
<Col span={22} >
2020-10-21 00:00:44 +08:00
<Input value={this.state.user.phone} onChange={e => {
this.updateUserField('phone', e.target.value);
2020-10-20 23:14:03 +08:00
}} />
</Col>
</Row>
</Card>
)
}
submitUserEdit() {
let user = Setting.deepCopy(this.state.user);
2021-02-13 00:11:12 +08:00
UserBackend.updateUser(this.state.organizationName, this.state.userName, user)
2020-10-20 23:14:03 +08:00
.then((res) => {
if (res) {
Setting.showMessage("success", `Successfully saved`);
this.setState({
2021-02-13 00:11:12 +08:00
organizationName: this.state.user.owner,
2020-10-20 23:14:03 +08:00
userName: this.state.user.name,
});
2021-02-13 00:11:12 +08:00
this.props.history.push(`/users/${this.state.user.owner}/${this.state.user.name}`);
2020-10-20 23:14:03 +08:00
} else {
Setting.showMessage("error", `failed to save: server side failure`);
2021-02-13 00:11:12 +08:00
this.updateUserField('owner', this.state.organizationName);
2020-10-20 23:14:03 +08:00
this.updateUserField('name', this.state.userName);
}
})
.catch(error => {
Setting.showMessage("error", `failed to save: ${error}`);
});
}
render() {
return (
<div>
<Row style={{width: "100%"}}>
<Col span={1}>
</Col>
<Col span={22}>
{
this.state.user !== null ? this.renderUser() : null
}
</Col>
<Col span={1}>
</Col>
</Row>
<Row style={{margin: 10}}>
<Col span={2}>
</Col>
<Col span={18}>
<Button type="primary" size="large" onClick={this.submitUserEdit.bind(this)}>Save</Button>
</Col>
</Row>
</div>
);
}
}
export default UserEditPage;