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

92 lines
2.0 KiB
JavaScript
Raw Normal View History

//@flow
import React from "react";
import { connect } from "react-redux";
2018-08-01 13:40:54 +02:00
import UserForm from "../components/UserForm";
import type { User } from "@scm-manager/ui-types";
2018-07-25 13:21:49 +02:00
import type { History } from "history";
import {
createUser,
createUserReset,
isCreateUserPending,
getCreateUserFailure
} from "../modules/users";
import { Page } from "@scm-manager/ui-components";
2018-07-26 10:19:26 +02:00
import { translate } from "react-i18next";
2019-01-22 10:50:45 +01:00
import { getUsersLink } from "../../modules/indexResource";
type Props = {
2018-07-25 13:21:49 +02:00
loading?: boolean,
error?: Error,
2018-10-09 16:40:44 +02:00
usersLink: string,
// dispatcher functions
2018-10-09 16:40:44 +02:00
addUser: (link: string, user: User, callback?: () => void) => void,
resetForm: () => void,
// context objects
t: string => string,
history: History
};
2019-04-24 09:59:29 +02:00
class CreateUser extends React.Component<Props> {
2018-07-27 10:38:35 +02:00
componentDidMount() {
this.props.resetForm();
}
2019-01-22 10:50:45 +01:00
userCreated = (user: User) => {
2018-07-25 13:21:49 +02:00
const { history } = this.props;
2019-01-22 10:50:45 +01:00
history.push("/user/" + user.name);
2018-07-25 13:21:49 +02:00
};
createUser = (user: User) => {
2019-01-22 10:50:45 +01:00
this.props.addUser(this.props.usersLink, user, () =>
this.userCreated(user)
);
2018-07-25 13:21:49 +02:00
};
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
2019-04-24 09:59:29 +02:00
title={t("createUser.title")}
subtitle={t("createUser.subtitle")}
2018-07-25 13:21:49 +02:00
error={error}
2018-08-03 08:37:09 +02:00
showContentOnError={true}
2018-07-25 13:21:49 +02:00
>
<UserForm
submitForm={user => this.createUser(user)}
loading={loading}
/>
2018-07-25 13:21:49 +02:00
</Page>
);
}
}
const mapDispatchToProps = dispatch => {
return {
2018-10-09 16:40:44 +02:00
addUser: (link: string, user: User, callback?: () => void) => {
dispatch(createUser(link, 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);
2018-10-09 16:40:44 +02:00
const usersLink = getUsersLink(state);
return {
2018-10-09 16:40:44 +02:00
usersLink,
loading,
error
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
2019-04-24 09:59:29 +02:00
)(translate("users")(CreateUser));