2018-07-02 16:05:58 +02:00
|
|
|
// @flow
|
2018-07-10 08:38:38 +02:00
|
|
|
import React from "react";
|
|
|
|
|
import { connect } from "react-redux";
|
2018-07-02 16:05:58 +02:00
|
|
|
|
2018-07-18 17:40:05 +02:00
|
|
|
import { fetchUsers, deleteUser, getUsersFromState } from "../modules/users";
|
2018-07-18 09:35:16 +02:00
|
|
|
import Loading from "../../components/Loading";
|
2018-07-18 14:02:07 +02:00
|
|
|
import ErrorNotification from "../../components/ErrorNotification";
|
2018-07-11 17:02:38 +02:00
|
|
|
import UserTable from "./UserTable";
|
2018-07-11 12:02:53 +02:00
|
|
|
import type { User } from "../types/User";
|
2018-07-17 11:29:58 +02:00
|
|
|
import type { UserEntry } from "../types/UserEntry";
|
2018-07-12 16:02:37 +02:00
|
|
|
|
2018-07-02 16:05:58 +02:00
|
|
|
type Props = {
|
2018-07-02 16:22:24 +02:00
|
|
|
login: boolean,
|
2018-07-11 12:02:53 +02:00
|
|
|
error: Error,
|
2018-07-12 16:02:37 +02:00
|
|
|
userEntries: Array<UserEntry>,
|
2018-07-10 15:18:37 +02:00
|
|
|
fetchUsers: () => void,
|
2018-07-18 17:40:05 +02:00
|
|
|
deleteUser: User => void
|
2018-07-10 08:38:38 +02:00
|
|
|
};
|
2018-07-02 16:05:58 +02:00
|
|
|
|
2018-07-17 08:39:51 +02:00
|
|
|
class Users extends React.Component<Props, User> {
|
2018-07-11 12:02:53 +02:00
|
|
|
componentDidMount() {
|
|
|
|
|
this.props.fetchUsers();
|
2018-07-02 16:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2018-07-18 09:35:16 +02:00
|
|
|
return (
|
|
|
|
|
<section className="section">
|
|
|
|
|
<div className="container">
|
|
|
|
|
<h1 className="title">SCM</h1>
|
|
|
|
|
<h2 className="subtitle">Users</h2>
|
|
|
|
|
{this.renderContent()}
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderContent() {
|
2018-07-18 17:40:05 +02:00
|
|
|
const { userEntries, deleteUser, error } = this.props;
|
2018-07-12 16:02:37 +02:00
|
|
|
if (userEntries) {
|
2018-07-02 16:05:58 +02:00
|
|
|
return (
|
2018-07-18 09:35:16 +02:00
|
|
|
<div>
|
2018-07-18 14:02:07 +02:00
|
|
|
<ErrorNotification error={error} />
|
2018-07-18 17:40:05 +02:00
|
|
|
<UserTable entries={userEntries} deleteUser={deleteUser} />
|
2018-07-18 09:35:16 +02:00
|
|
|
</div>
|
2018-07-02 16:05:58 +02:00
|
|
|
);
|
2018-07-10 08:38:38 +02:00
|
|
|
} else {
|
2018-07-18 09:35:16 +02:00
|
|
|
return <Loading />;
|
2018-07-10 08:38:38 +02:00
|
|
|
}
|
2018-07-02 16:05:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 08:38:38 +02:00
|
|
|
const mapStateToProps = state => {
|
2018-07-12 16:02:37 +02:00
|
|
|
const userEntries = getUsersFromState(state);
|
2018-07-10 08:38:38 +02:00
|
|
|
return {
|
2018-07-17 08:39:51 +02:00
|
|
|
userEntries,
|
2018-07-18 14:02:07 +02:00
|
|
|
error: state.users.error
|
2018-07-10 08:38:38 +02:00
|
|
|
};
|
2018-07-02 16:05:58 +02:00
|
|
|
};
|
|
|
|
|
|
2018-07-10 08:38:38 +02:00
|
|
|
const mapDispatchToProps = dispatch => {
|
2018-07-02 16:05:58 +02:00
|
|
|
return {
|
2018-07-10 08:38:38 +02:00
|
|
|
fetchUsers: () => {
|
|
|
|
|
dispatch(fetchUsers());
|
2018-07-11 12:02:53 +02:00
|
|
|
},
|
2018-07-18 09:52:49 +02:00
|
|
|
deleteUser: (user: User) => {
|
|
|
|
|
dispatch(deleteUser(user));
|
2018-07-02 16:05:58 +02:00
|
|
|
}
|
2018-07-10 08:38:38 +02:00
|
|
|
};
|
2018-07-02 16:05:58 +02:00
|
|
|
};
|
|
|
|
|
|
2018-07-10 08:38:38 +02:00
|
|
|
export default connect(
|
|
|
|
|
mapStateToProps,
|
|
|
|
|
mapDispatchToProps
|
|
|
|
|
)(Users);
|