2018-07-18 17:40:05 +02:00
|
|
|
//@flow
|
|
|
|
|
import React from "react";
|
2018-07-30 13:38:15 +02:00
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
import { withRouter } from "react-router-dom";
|
2018-07-25 15:04:19 +02:00
|
|
|
import UserForm from "./../components/UserForm";
|
2018-07-30 13:38:15 +02:00
|
|
|
import type { User } from "../types/User";
|
|
|
|
|
import {
|
|
|
|
|
modifyUser,
|
|
|
|
|
isModifyUserPending,
|
|
|
|
|
getModifyUserFailure
|
|
|
|
|
} from "../modules/users";
|
|
|
|
|
import type { History } from "history";
|
|
|
|
|
import ErrorNotification from "../../components/ErrorNotification";
|
2018-07-18 17:40:05 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2018-07-26 08:51:05 +02:00
|
|
|
loading: boolean,
|
2018-07-30 13:38:15 +02:00
|
|
|
error: Error,
|
|
|
|
|
|
|
|
|
|
// dispatch functions
|
|
|
|
|
modifyUser: (user: User, callback?: () => void) => void,
|
|
|
|
|
|
|
|
|
|
// context objects
|
|
|
|
|
user: User,
|
2018-07-26 08:51:05 +02:00
|
|
|
history: History
|
2018-07-18 17:40:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class EditUser extends React.Component<Props> {
|
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} />
|
|
|
|
|
<UserForm
|
|
|
|
|
submitForm={user => this.modifyUser(user)}
|
|
|
|
|
user={user}
|
|
|
|
|
loading={loading}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2018-07-18 17:40:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
|
return {
|
2018-07-26 08:51:05 +02:00
|
|
|
modifyUser: (user: User, callback?: () => void) => {
|
|
|
|
|
dispatch(modifyUser(user, callback));
|
2018-07-18 17:40:05 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
2018-07-30 13:38:15 +02:00
|
|
|
const loading = isModifyUserPending(state, ownProps.user.name);
|
|
|
|
|
const error = getModifyUserFailure(state, ownProps.user.name);
|
|
|
|
|
return {
|
|
|
|
|
loading,
|
|
|
|
|
error
|
|
|
|
|
};
|
2018-07-18 17:40:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
2018-07-26 08:51:05 +02:00
|
|
|
)(withRouter(EditUser));
|