// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import React from 'react';
import PropTypes from 'prop-types';
import {FormattedDate, FormattedMessage, FormattedHTMLMessage} from 'react-intl';
import Banner from 'components/admin_console/banner.jsx';
import LoadingScreen from 'components/loading_screen.jsx';
import AnalyticsStore from 'stores/analytics_store.jsx';
import BrowserStore from 'stores/browser_store.jsx';
import * as AdminActions from 'actions/admin_actions.jsx';
import {StatTypes} from 'utils/constants.jsx';
import LineChart from 'components/analytics/line_chart.jsx';
import StatisticCount from 'components/analytics/statistic_count.jsx';
import TableChart from 'components/analytics/table_chart.jsx';
import {formatPostsPerDayData, formatUsersWithPostsPerDayData} from 'components/analytics/system_analytics.jsx';
const LAST_ANALYTICS_TEAM = 'last_analytics_team';
export default class TeamAnalytics extends React.Component {
static propTypes = {
/*
* Array of team objects
*/
teams: PropTypes.arrayOf(PropTypes.object).isRequired,
/*
* Initial team to load analytics for
*/
initialTeam: PropTypes.object,
actions: PropTypes.shape({
/*
* Function to get teams
*/
getTeams: PropTypes.func.isRequired
}).isRequired
}
constructor(props) {
super(props);
const teamId = props.initialTeam ? props.initialTeam.id : '';
this.state = {
team: props.initialTeam,
stats: AnalyticsStore.getAllTeam(teamId)
};
}
componentDidMount() {
AnalyticsStore.addChangeListener(this.onChange);
if (this.state.team) {
this.getData(this.state.team.id);
}
this.props.actions.getTeams(0, 1000);
}
componentWillUpdate(nextProps, nextState) {
if (nextState.team && nextState.team !== this.state.team) {
this.getData(nextState.team.id);
}
}
getData(id) {
AdminActions.getStandardAnalytics(id);
AdminActions.getPostsPerDayAnalytics(id);
AdminActions.getUsersPerDayAnalytics(id);
}
componentWillUnmount() {
AnalyticsStore.removeChangeListener(this.onChange);
}
onChange = () => {
const teamId = this.state.team ? this.state.team.id : '';
this.setState({
stats: AnalyticsStore.getAllTeam(teamId)
});
}
handleTeamChange = (e) => {
const teamId = e.target.value;
let team;
this.props.teams.forEach((t) => {
if (t.id === teamId) {
team = t;
}
});
this.setState({
team
});
BrowserStore.setGlobalItem(LAST_ANALYTICS_TEAM, teamId);
}
render() {
if (this.props.teams.length === 0 || !this.state.team || !this.state.stats) {
return