scinote-web/app/javascript/packs/shared/navigation/components/GlobalActivitiesModal.jsx

153 lines
4.1 KiB
React
Raw Normal View History

2017-08-02 23:27:05 +08:00
import React, { Component } from "react";
import { connect } from "react-redux";
import PropTypes from "prop-types";
import { FormattedMessage } from "react-intl";
import { Modal, Button } from "react-bootstrap";
import _ from "lodash";
2017-08-22 19:20:06 +08:00
import styled from "styled-components";
2017-08-02 23:27:05 +08:00
import { getActivities } from "../../actions/ActivitiesActions";
import ActivityElement from "./ActivityElement";
import ActivityDateElement from "./ActivityDateElement";
2017-08-22 19:20:06 +08:00
import {
WHITE_COLOR,
COLOR_CONCRETE,
COLOR_MINE_SHAFT,
COLOR_GRAY_LIGHT_YADCF
} from "../../../app/constants/colors";
const StyledBottom = styled(Button)`
display: inline-block;
margin-bottom: 0;
font-weight: normal;
text-align: center;
vertical-align: middle;
touch-action: manipulation;
cursor: pointer;
background-image: none;
border: 1px solid transparent;
white-space: nowrap;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
border-radius: 4px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
color: ${COLOR_MINE_SHAFT};
background-color: ${WHITE_COLOR};
border-color: ${COLOR_GRAY_LIGHT_YADCF};
`;
const StyledModalBody = styled(Modal.Body)`
background-color: ${COLOR_CONCRETE};
color: ${COLOR_MINE_SHAFT};;
`;
2017-08-02 23:27:05 +08:00
class GlobalActivitiesModal extends Component {
constructor(props) {
super(props);
this.displayActivities = this.displayActivities.bind(this);
this.addMoreActivities = this.addMoreActivities.bind(this);
}
displayActivities() {
2017-08-03 17:17:09 +08:00
if (this.props.activities.length === 0) {
2017-08-02 23:27:05 +08:00
return (
<li>
<FormattedMessage id="activities.no_data" />
</li>
);
}
2017-08-03 17:17:09 +08:00
return this.props.activities.map((activity, i, arr) => {
const newDate = new Date(activity.created_at);
2017-08-02 23:27:05 +08:00
if (i > 0) {
const prevDate = new Date(arr[i - 1].created_at);
2017-08-02 23:27:05 +08:00
if (prevDate < newDate) {
return [
<ActivityDateElement key={newDate} date={newDate} />,
<ActivityElement key={activity.id} activity={activity} />
];
}
} else {
return [
<ActivityDateElement key={newDate} date={newDate} />,
<ActivityElement key={activity.id} activity={activity} />
];
}
return <ActivityElement key={activity.id} activity={activity} />;
});
}
addMoreActivities() {
const lastId = _.last(this.props.activities).id;
this.props.fetchActivities(lastId);
2017-08-02 23:27:05 +08:00
}
addMoreButton() {
2017-08-03 17:17:09 +08:00
if (this.props.more) {
2017-08-02 23:27:05 +08:00
return (
2017-08-22 19:20:06 +08:00
<li className="text-center">
<StyledBottom onClick={this.addMoreActivities}>
2017-08-02 23:27:05 +08:00
<FormattedMessage id="activities.more_activities" />
2017-08-22 19:20:06 +08:00
</StyledBottom>
2017-08-02 23:27:05 +08:00
</li>
);
}
2017-08-22 19:20:06 +08:00
return "";
2017-08-02 23:27:05 +08:00
}
render() {
return (
<Modal show={this.props.showModal} onHide={this.props.onCloseModal}>
<Modal.Header closeButton>
<Modal.Title>
<FormattedMessage id="activities.modal_title" />
</Modal.Title>
</Modal.Header>
2017-08-22 19:20:06 +08:00
<StyledModalBody>
<ul className="list-unstyled">
2017-08-02 23:27:05 +08:00
{this.displayActivities()}
{this.addMoreButton()}
</ul>
2017-08-22 19:20:06 +08:00
</StyledModalBody>
2017-08-02 23:27:05 +08:00
<Modal.Footer>
<Button onClick={this.props.onCloseModal}>
<FormattedMessage id="general.close" />
</Button>
</Modal.Footer>
</Modal>
);
}
}
GlobalActivitiesModal.propTypes = {
showModal: PropTypes.bool.isRequired,
onCloseModal: PropTypes.func.isRequired,
fetchActivities: PropTypes.func.isRequired,
2017-08-03 17:17:09 +08:00
more: PropTypes.bool.isRequired,
activities: PropTypes.arrayOf(
2017-08-02 23:27:05 +08:00
PropTypes.shape({
id: PropTypes.number.isRequired,
message: PropTypes.string.isRequired,
created_at: PropTypes.string.isRequired
})
).isRequired
};
2017-08-03 17:17:09 +08:00
const mapStateToProps = ({ global_activities }) => {
const { activities, more } = global_activities;
2017-08-03 17:17:09 +08:00
return { activities, more };
2017-08-02 23:27:05 +08:00
};
const mapDispatchToProps = dispatch => ({
fetchActivities(lastId) {
dispatch(getActivities(lastId));
2017-08-02 23:27:05 +08:00
}
});
export default connect(mapStateToProps, mapDispatchToProps)(
GlobalActivitiesModal
);