2018-07-18 17:40:05 +02:00
|
|
|
//@flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
import { connect } from "react-redux";
|
2018-07-25 15:04:19 +02:00
|
|
|
import UserForm from "./../components/UserForm";
|
2018-07-18 17:40:05 +02:00
|
|
|
import type { User } from "../types/User";
|
2018-07-25 13:21:49 +02:00
|
|
|
import type { History } from "history";
|
2018-07-27 10:38:35 +02:00
|
|
|
import { createUser, createUserReset } 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";
|
2018-07-18 17:40:05 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2018-07-26 10:19:26 +02:00
|
|
|
t: string => string,
|
2018-07-25 13:21:49 +02:00
|
|
|
addUser: (user: User, callback?: () => void) => void,
|
|
|
|
|
loading?: boolean,
|
|
|
|
|
error?: Error,
|
2018-07-27 10:38:35 +02:00
|
|
|
history: History,
|
|
|
|
|
resetForm: () => void
|
2018-07-18 17:40:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-18 17:40:05 +02:00
|
|
|
render() {
|
2018-07-26 10:19:26 +02:00
|
|
|
const { t, loading, error } = this.props;
|
2018-07-18 17:40:05 +02:00
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
>
|
2018-07-27 10:54:49 +02:00
|
|
|
<UserForm
|
|
|
|
|
submitForm={user => this.createUser(user)}
|
|
|
|
|
loading={loading}
|
|
|
|
|
/>
|
2018-07-25 13:21:49 +02:00
|
|
|
</Page>
|
2018-07-18 17:40:05 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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());
|
2018-07-18 17:40:05 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
2018-07-25 14:34:00 +02:00
|
|
|
if (state.users && state.users.create) {
|
|
|
|
|
return state.users.create;
|
2018-07-19 12:05:50 +02:00
|
|
|
}
|
2018-07-18 17:40:05 +02:00
|
|
|
return {};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
2018-07-26 10:19:26 +02:00
|
|
|
)(translate("users")(AddUser));
|