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

73 lines
1.5 KiB
JavaScript
Raw Normal View History

//@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-23 17:00:33 +02:00
import { modifyUser, fetchUser } from "../modules/users";
type Props = {
name: string,
fetchUser: string => void,
2018-07-19 12:05:50 +02:00
userEntry?: UserEntry,
updateUser: User => void,
loading: boolean
};
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-19 12:05:50 +02:00
if (!userEntry || userEntry.loading) {
return <Loading />;
} else {
return (
<div>
2018-07-19 12:05:50 +02:00
<UserForm
submitForm={user => submitUser(user)}
user={userEntry.entry}
loading={userEntry.loading}
/>
</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));
}
};
};
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];
}
return {
2018-07-19 12:05:50 +02:00
name,
userEntry
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(EditUser);