2018-07-18 17:40:05 +02:00
|
|
|
//@flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
import UserForm from "./UserForm";
|
|
|
|
|
import type { User } from "../types/User";
|
2018-07-23 08:16:06 +02:00
|
|
|
import type { UserEntry } from "../types/UserEntry";
|
2018-07-19 12:05:50 +02:00
|
|
|
import Loading from "../../components/Loading";
|
2018-07-18 17:40:05 +02:00
|
|
|
|
2018-07-23 17:00:33 +02:00
|
|
|
import { modifyUser, fetchUser } from "../modules/users";
|
2018-07-18 17:40:05 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
name: string,
|
|
|
|
|
fetchUser: string => void,
|
2018-07-19 12:05:50 +02:00
|
|
|
userEntry?: UserEntry,
|
|
|
|
|
updateUser: User => void,
|
|
|
|
|
loading: boolean
|
2018-07-18 17:40:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class EditUser extends React.Component<Props> {
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.props.fetchUser(this.props.name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const submitUser = this.props.updateUser;
|
|
|
|
|
|
2018-07-19 12:05:50 +02:00
|
|
|
const { userEntry } = this.props;
|
2018-07-18 17:40:05 +02:00
|
|
|
|
2018-07-19 12:05:50 +02:00
|
|
|
if (!userEntry || userEntry.loading) {
|
|
|
|
|
return <Loading />;
|
2018-07-18 17:40:05 +02:00
|
|
|
} else {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
2018-07-19 12:05:50 +02:00
|
|
|
<UserForm
|
|
|
|
|
submitForm={user => submitUser(user)}
|
|
|
|
|
user={userEntry.entry}
|
|
|
|
|
loading={userEntry.loading}
|
|
|
|
|
/>
|
2018-07-18 17:40:05 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
|
return {
|
|
|
|
|
fetchUser: (name: string) => {
|
|
|
|
|
dispatch(fetchUser(name));
|
|
|
|
|
},
|
|
|
|
|
updateUser: (user: User) => {
|
2018-07-23 17:00:33 +02:00
|
|
|
dispatch(modifyUser(user));
|
2018-07-18 17:40:05 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
2018-07-19 12:05:50 +02:00
|
|
|
const name = ownProps.match.params.name;
|
|
|
|
|
let userEntry;
|
|
|
|
|
if (state.users && state.users.usersByNames) {
|
|
|
|
|
userEntry = state.users.usersByNames[name];
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 17:40:05 +02:00
|
|
|
return {
|
2018-07-19 12:05:50 +02:00
|
|
|
name,
|
|
|
|
|
userEntry
|
2018-07-18 17:40:05 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
|
|
|
|
)(EditUser);
|