// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import $ from 'jquery';
import UserStore from 'stores/user_store.jsx';
import Client from 'utils/web_client.jsx';
import * as AsyncClient from 'utils/async_client.jsx';
import {Modal} from 'react-bootstrap';
import LoadingScreen from './loading_screen.jsx';
import * as Utils from 'utils/utils.jsx';
import {FormattedMessage, FormattedTime, FormattedDate} from 'react-intl';
import React from 'react';
export default class ActivityLogModal extends React.Component {
constructor(props) {
super(props);
this.submitRevoke = this.submitRevoke.bind(this);
this.onListenerChange = this.onListenerChange.bind(this);
this.handleMoreInfo = this.handleMoreInfo.bind(this);
this.onHide = this.onHide.bind(this);
this.onShow = this.onShow.bind(this);
let state = this.getStateFromStores();
state.moreInfo = [];
this.state = state;
}
getStateFromStores() {
return {
sessions: UserStore.getSessions(),
serverError: null,
clientError: null
};
}
submitRevoke(altId, e) {
e.preventDefault();
var modalContent = $(e.target).closest('.modal-content');
modalContent.addClass('animation--highlight');
setTimeout(() => {
modalContent.removeClass('animation--highlight');
}, 1500);
Client.revokeSession(altId,
function handleRevokeSuccess() {
AsyncClient.getSessions();
},
function handleRevokeError(err) {
let state = this.getStateFromStores();
state.serverError = err;
this.setState(state);
}.bind(this)
);
}
onShow() {
AsyncClient.getSessions();
if (!Utils.isMobile()) {
$('.modal-body').perfectScrollbar();
}
}
onHide() {
this.setState({moreInfo: []});
this.props.onHide();
}
componentDidMount() {
UserStore.addSessionsChangeListener(this.onListenerChange);
if (this.props.show) {
this.onShow();
}
}
componentDidUpdate(prevProps) {
if (this.props.show && !prevProps.show) {
this.onShow();
}
}
componentWillUnmount() {
UserStore.removeSessionsChangeListener(this.onListenerChange);
}
onListenerChange() {
const newState = this.getStateFromStores();
if (!Utils.areObjectsEqual(newState.sessions, this.state.sessions)) {
this.setState(newState);
}
}
handleMoreInfo(index) {
let newMoreInfo = this.state.moreInfo;
newMoreInfo[index] = true;
this.setState({moreInfo: newMoreInfo});
}
render() {
let activityList = [];
for (let i = 0; i < this.state.sessions.length; i++) {
const currentSession = this.state.sessions[i];
const lastAccessTime = new Date(currentSession.last_activity_at);
const firstAccessTime = new Date(currentSession.create_at);
let devicePlatform = currentSession.props.platform;
let devicePicture = '';
if (currentSession.props.platform === 'Windows') {
devicePicture = 'fa fa-windows';
} else if (currentSession.device_id && currentSession.device_id.indexOf('apple:') === 0) {
devicePicture = 'fa fa-apple';
devicePlatform = (