2018-07-18 17:40:05 +02:00
|
|
|
//@flow
|
|
|
|
|
import React from "react";
|
|
|
|
|
import { connect } from "react-redux";
|
2018-08-01 13:40:54 +02:00
|
|
|
import UserForm from "../components/UserForm";
|
2018-09-05 14:32:49 +02:00
|
|
|
import type { User } from "@scm-manager/ui-types";
|
2018-07-25 13:21:49 +02:00
|
|
|
import type { History } from "history";
|
2018-07-30 13:38:15 +02:00
|
|
|
import {
|
|
|
|
|
createUser,
|
|
|
|
|
createUserReset,
|
|
|
|
|
isCreateUserPending,
|
|
|
|
|
getCreateUserFailure
|
|
|
|
|
} from "../modules/users";
|
2018-09-05 14:32:49 +02:00
|
|
|
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";
|
2018-07-18 17:40:05 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2018-07-25 13:21:49 +02:00
|
|
|
loading?: boolean,
|
|
|
|
|
error?: Error,
|
2018-10-09 16:40:44 +02:00
|
|
|
usersLink: string,
|
2018-07-30 13:38:15 +02:00
|
|
|
|
|
|
|
|
// dispatcher functions
|
2018-10-09 16:40:44 +02:00
|
|
|
addUser: (link: string, user: User, callback?: () => void) => void,
|
2018-07-30 13:38:15 +02:00
|
|
|
resetForm: () => void,
|
|
|
|
|
|
|
|
|
|
// context objects
|
|
|
|
|
t: string => string,
|
|
|
|
|
history: History
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
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
|
2019-01-23 11:14:30 +01:00
|
|
|
title={t("addUser.title")}
|
|
|
|
|
subtitle={t("addUser.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
|
|
|
>
|
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-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());
|
2018-07-18 17:40:05 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
2018-07-30 13:38:15 +02:00
|
|
|
const loading = isCreateUserPending(state);
|
|
|
|
|
const error = getCreateUserFailure(state);
|
2018-10-09 16:40:44 +02:00
|
|
|
const usersLink = getUsersLink(state);
|
2018-07-30 13:38:15 +02:00
|
|
|
return {
|
2018-10-09 16:40:44 +02:00
|
|
|
usersLink,
|
2018-07-30 13:38:15 +02:00
|
|
|
loading,
|
|
|
|
|
error
|
|
|
|
|
};
|
2018-07-18 17:40:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
2018-07-26 10:19:26 +02:00
|
|
|
)(translate("users")(AddUser));
|