Files
SCM-Manager/scm-ui/src/users/containers/GeneralUser.js

100 lines
2.4 KiB
JavaScript
Raw Normal View History

//@flow
import React from "react";
import { connect } from "react-redux";
import { withRouter } from "react-router-dom";
2019-01-23 10:08:15 +01:00
import UserForm from "../components/UserForm";
import DeleteUser from "../components/DeleteUser";
import type { User } from "@scm-manager/ui-types";
import {
modifyUser,
2019-01-23 11:55:57 +01:00
deleteUser,
isModifyUserPending,
getModifyUserFailure,
2019-01-23 11:55:57 +01:00
modifyUserReset,
isDeleteUserPending,
getDeleteUserFailure
} from "../modules/users";
import type { History } from "history";
import { ErrorNotification } from "@scm-manager/ui-components";
type Props = {
loading: boolean,
error: Error,
// dispatch functions
modifyUser: (user: User, callback?: () => void) => void,
modifyUserReset: User => void,
2019-01-23 11:55:57 +01:00
deleteUser: (user: User, callback?: () => void) => void,
// context objects
user: User,
history: History
};
2019-01-23 09:17:53 +01:00
class GeneralUser extends React.Component<Props> {
componentDidMount() {
const { modifyUserReset, user } = this.props;
modifyUserReset(user);
}
2019-01-23 11:55:57 +01:00
userModified = (user: User) => () => {
this.props.history.push(`/user/${user.name}`);
};
modifyUser = (user: User) => {
this.props.modifyUser(user, this.userModified(user));
};
2019-01-23 11:55:57 +01:00
userDeleted = () => {
this.props.history.push("/users");
};
deleteUser = (user: User) => {
this.props.deleteUser(user, this.userDeleted);
};
render() {
const { user, loading, error } = this.props;
return (
<div>
<ErrorNotification error={error} />
<UserForm
submitForm={user => this.modifyUser(user)}
user={user}
loading={loading}
/>
<hr />
2019-01-23 11:55:57 +01:00
<DeleteUser user={user} deleteUser={this.deleteUser} />
</div>
);
}
}
2019-01-23 10:08:15 +01:00
const mapStateToProps = (state, ownProps) => {
2019-01-23 11:55:57 +01:00
const loading = isModifyUserPending(state, ownProps.user.name) || isDeleteUserPending(state, ownProps.user.name);
const error = getModifyUserFailure(state, ownProps.user.name) || getDeleteUserFailure(state, ownProps.user.name);
2019-01-23 10:08:15 +01:00
return {
loading,
error
};
};
const mapDispatchToProps = dispatch => {
return {
modifyUser: (user: User, callback?: () => void) => {
dispatch(modifyUser(user, callback));
},
modifyUserReset: (user: User) => {
dispatch(modifyUserReset(user));
2019-01-23 11:55:57 +01:00
},
deleteUser: (user: User, callback?: () => void) => {
dispatch(deleteUser(user, callback));
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
2019-01-23 09:17:53 +01:00
)(withRouter(GeneralUser));