Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reports list auto refresh #293

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/components/Analytics/FiltersPanel.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import PropTypes from "prop-types";
import _ from "lodash";
import moment from "moment";
import { Button, IconButton } from "superdesk-ui-framework/react";
import MultiSelect from "../UI/MultiSelect";

Expand Down
42 changes: 36 additions & 6 deletions client/components/Analytics/Reports/Reports.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@ class Reports extends React.Component {
constructor(props) {
super(props);

this.timer = null;

this.state = {
reports: [],
page: 0,
totalPages: 1,
loading: false
loading: false,
};
}

componentDidMount() {
this.loadReports();
this.timer = setInterval(() => {
this.refreshReports();
}, 15000);
}

componentWillUnmount() {
clearInterval(this.timer);
}

loadReports = () => {
Expand All @@ -28,19 +37,40 @@ class Reports extends React.Component {
let params = {
"sorting[createdAt]": "desc",
limit: 10,
page: this.state.page + 1
page: this.state.page + 1,
};

this.props.publisher.getAnalyticsReports(params).then(response =>
this.props.publisher.getAnalyticsReports(params).then((response) =>
this.setState({
reports: [...this.state.reports, ...response._embedded._items],
loading: false,
totalPages: response.pages,
page: this.state.page + 1
page: this.state.page + 1,
})
);
};

refreshReports = () => {
let params = {
"sorting[createdAt]": "desc",
limit: 10 * this.state.page,
page: 1,
};

this.props.publisher.getAnalyticsReports(params).then((response) => {
const currentReports = [...this.state.reports];
const newItems = response._embedded._items;

let reports = currentReports.map((item) =>
newItems.find((newItem) => item.id === newItem.id)
);

this.setState({
reports: reports,
});
});
};

render() {
return (
<div className="sd-column-box__main-column" id="scrollableDiv">
Expand All @@ -54,7 +84,7 @@ class Reports extends React.Component {
scrollableTarget="scrollableDiv"
>
{this.state.reports.map((item, index) => (
<Item item={item} key={"report" + index} />
<Item item={item} key={"report" + item.id} />
))}
</InfiniteScroll>
</div>
Expand All @@ -64,7 +94,7 @@ class Reports extends React.Component {
}

Reports.propTypes = {
publisher: PropTypes.object.isRequired
publisher: PropTypes.object.isRequired,
};

export default Reports;