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-25 13:21:49 +02:00
|
|
|
import type { History } from "history";
|
2018-07-23 17:00:33 +02:00
|
|
|
import { createUser } from "../modules/users";
|
2018-07-25 14:29:43 +02:00
|
|
|
import { Page } from "../../components/layout";
|
2018-07-18 17:40:05 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2018-07-25 13:21:49 +02:00
|
|
|
addUser: (user: User, callback?: () => void) => void,
|
|
|
|
|
loading?: boolean,
|
|
|
|
|
error?: Error,
|
|
|
|
|
history: History
|
2018-07-18 17:40:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AddUser extends React.Component<Props> {
|
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-25 13:21:49 +02:00
|
|
|
const { loading, error } = this.props;
|
2018-07-18 17:40:05 +02:00
|
|
|
|
2018-07-25 13:21:49 +02:00
|
|
|
// TODO i18n
|
2018-07-18 17:40:05 +02:00
|
|
|
return (
|
2018-07-25 13:21:49 +02:00
|
|
|
<Page
|
|
|
|
|
title="Create User"
|
|
|
|
|
subtitle="Create a new user"
|
|
|
|
|
loading={loading}
|
|
|
|
|
error={error}
|
|
|
|
|
>
|
|
|
|
|
<UserForm submitForm={user => this.createUser(user)} />
|
|
|
|
|
</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-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
|
|
|
|
|
)(AddUser);
|