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

85 lines
1.7 KiB
JavaScript
Raw Normal View History

//@flow
import React from "react";
import { connect } from "react-redux";
2018-07-25 15:04:19 +02:00
import UserForm from "./../components/UserForm";
import type { User } from "../types/User";
2018-07-25 13:21:49 +02:00
import type { History } from "history";
import {
createUser,
createUserReset,
isCreateUserPending,
getCreateUserFailure
} from "../modules/users";
2018-07-25 14:29:43 +02:00
import { Page } from "../../components/layout";
2018-07-26 10:19:26 +02:00
import { translate } from "react-i18next";
type Props = {
2018-07-25 13:21:49 +02:00
loading?: boolean,
error?: Error,
// dispatcher functions
addUser: (user: User, callback?: () => void) => void,
resetForm: () => void,
// context objects
t: string => string,
history: History
};
class AddUser extends React.Component<Props> {
2018-07-27 10:38:35 +02:00
componentDidMount() {
this.props.resetForm();
}
2018-07-25 13:21:49 +02:00
userCreated = () => {
const { history } = this.props;
history.push("/users");
};
createUser = (user: User) => {
this.props.addUser(user, this.userCreated);
};
render() {
2018-07-26 10:19:26 +02:00
const { t, loading, error } = this.props;
return (
2018-07-25 13:21:49 +02:00
<Page
2018-07-26 10:19:26 +02:00
title={t("add-user.title")}
subtitle={t("add-user.subtitle")}
2018-07-25 13:21:49 +02:00
error={error}
>
<UserForm
submitForm={user => this.createUser(user)}
loading={loading}
/>
2018-07-25 13:21:49 +02:00
</Page>
);
}
}
const mapDispatchToProps = dispatch => {
return {
2018-07-25 13:21:49 +02:00
addUser: (user: User, callback?: () => void) => {
dispatch(createUser(user, callback));
2018-07-27 10:38:35 +02:00
},
resetForm: () => {
dispatch(createUserReset());
}
};
};
const mapStateToProps = (state, ownProps) => {
const loading = isCreateUserPending(state);
const error = getCreateUserFailure(state);
return {
loading,
error
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
2018-07-26 10:19:26 +02:00
)(translate("users")(AddUser));