2019-10-20 18:02:52 +02:00
|
|
|
import React from "react";
|
|
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
import { withRouter } from "react-router-dom";
|
|
|
|
|
import UserForm from "../components/UserForm";
|
|
|
|
|
import DeleteUser from "./DeleteUser";
|
|
|
|
|
import { User } from "@scm-manager/ui-types";
|
2019-10-21 10:57:56 +02:00
|
|
|
import { modifyUser, isModifyUserPending, getModifyUserFailure, modifyUserReset } from "../modules/users";
|
2019-10-20 18:02:52 +02:00
|
|
|
import { History } from "history";
|
|
|
|
|
import { ErrorNotification } from "@scm-manager/ui-components";
|
2020-01-08 13:40:07 +01:00
|
|
|
import { compose } from "redux";
|
2018-07-18 17:40:05 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
loading: boolean;
|
|
|
|
|
error: Error;
|
2018-07-30 13:38:15 +02:00
|
|
|
|
|
|
|
|
// dispatch functions
|
2019-10-19 16:38:07 +02:00
|
|
|
modifyUser: (user: User, callback?: () => void) => void;
|
|
|
|
|
modifyUserReset: (p: User) => void;
|
2018-07-30 13:38:15 +02:00
|
|
|
|
|
|
|
|
// context objects
|
2019-10-19 16:38:07 +02:00
|
|
|
user: User;
|
|
|
|
|
history: History;
|
2018-07-18 17:40:05 +02:00
|
|
|
};
|
|
|
|
|
|
2019-01-29 08:22:43 +01:00
|
|
|
class EditUser extends React.Component<Props> {
|
2018-11-06 11:24:01 +01:00
|
|
|
componentDidMount() {
|
|
|
|
|
const { modifyUserReset, user } = this.props;
|
|
|
|
|
modifyUserReset(user);
|
|
|
|
|
}
|
2019-01-23 11:55:57 +01:00
|
|
|
|
2018-07-26 08:51:05 +02:00
|
|
|
userModified = (user: User) => () => {
|
|
|
|
|
this.props.history.push(`/user/${user.name}`);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
modifyUser = (user: User) => {
|
|
|
|
|
this.props.modifyUser(user, this.userModified(user));
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-18 17:40:05 +02:00
|
|
|
render() {
|
2018-07-30 13:38:15 +02:00
|
|
|
const { user, loading, error } = this.props;
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<ErrorNotification error={error} />
|
2019-10-21 10:57:56 +02:00
|
|
|
<UserForm submitForm={user => this.modifyUser(user)} user={user} loading={loading} />
|
2019-02-06 11:44:03 +01:00
|
|
|
<DeleteUser user={user} />
|
2018-07-30 13:38:15 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
2018-07-18 17:40:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-08 13:40:07 +01:00
|
|
|
const mapStateToProps = (state: any, ownProps: Props) => {
|
2019-02-06 10:42:25 +01:00
|
|
|
const loading = isModifyUserPending(state, ownProps.user.name);
|
|
|
|
|
const error = getModifyUserFailure(state, ownProps.user.name);
|
2019-01-23 10:08:15 +01:00
|
|
|
return {
|
|
|
|
|
loading,
|
2019-10-20 18:02:52 +02:00
|
|
|
error
|
2019-01-23 10:08:15 +01:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-08 13:40:07 +01:00
|
|
|
const mapDispatchToProps = (dispatch: any) => {
|
2018-07-18 17:40:05 +02:00
|
|
|
return {
|
2018-07-26 08:51:05 +02:00
|
|
|
modifyUser: (user: User, callback?: () => void) => {
|
|
|
|
|
dispatch(modifyUser(user, callback));
|
2018-11-06 11:24:01 +01:00
|
|
|
},
|
|
|
|
|
modifyUserReset: (user: User) => {
|
|
|
|
|
dispatch(modifyUserReset(user));
|
2019-10-20 18:02:52 +02:00
|
|
|
}
|
2018-07-18 17:40:05 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-08 13:40:07 +01:00
|
|
|
export default compose(connect(mapStateToProps, mapDispatchToProps), withRouter)(EditUser);
|