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";
import { getActivities } from "../../actions/ActivitiesActions";
import ActivityElement from "./ActivityElement";
import ActivityDateElement from "./ActivityDateElement";
class GlobalActivitiesModal extends Component {
constructor(props) {
super(props);
this.displayActivities = this.displayActivities.bind(this);
this.addMoreActivities = this.addMoreActivities.bind(this);
}
displayActivities() {
if (this.props.activities.length === 0) {
return (
);
}
return this.props.activities.map((activity, i, arr) => {
let newDate = new Date(activity.created_at);
if (i > 0) {
let prevDate = new Date(arr[i - 1].created_at);
if (prevDate < newDate) {
return [
,
];
}
} else {
return [
,
];
}
return ;
});
}
addMoreActivities() {
let last_id = _.last(this.props.activities).id;
this.props.fetchActivities(last_id);
}
addMoreButton() {
if (this.props.more) {
return (
);
}
}
render() {
return (
{this.displayActivities()}
{this.addMoreButton()}
);
}
}
GlobalActivitiesModal.propTypes = {
showModal: PropTypes.bool.isRequired,
onCloseModal: PropTypes.func.isRequired,
fetchActivities: PropTypes.func.isRequired,
more: PropTypes.bool.isRequired,
activities: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number.isRequired,
message: PropTypes.string.isRequired,
created_at: PropTypes.string.isRequired
})
).isRequired
};
const mapStateToProps = ({ global_activities }) => {
let { activities, more } = global_activities;
return { activities, more };
};
const mapDispatchToProps = dispatch => ({
fetchActivities(last_id) {
dispatch(getActivities(last_id));
}
});
export default connect(mapStateToProps, mapDispatchToProps)(
GlobalActivitiesModal
);