// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. // See License.txt for license information. var UserStore = require('../stores/user_store.jsx'); var Client = require('../utils/client.jsx'); var AsyncClient = require('../utils/async_client.jsx'); var LoadingScreen = require('./loading_screen.jsx'); var utils = require('../utils/utils.jsx'); function getStateFromStoresForSessions() { return { sessions: UserStore.getSessions(), serverError: null, clientError: null }; } module.exports = React.createClass({ displayName: 'ActivityLogModal', submitRevoke: function(altId) { Client.revokeSession(altId, function(data) { AsyncClient.getSessions(); }.bind(this), function(err) { var state = getStateFromStoresForSessions(); state.serverError = err; this.setState(state); }.bind(this) ); }, componentDidMount: function() { UserStore.addSessionsChangeListener(this.onListenerChange); $(this.refs.modal.getDOMNode()).on('shown.bs.modal', function (e) { AsyncClient.getSessions(); }); var self = this; $(this.refs.modal.getDOMNode()).on('hidden.bs.modal', function(e) { $('#user_settings').modal('show'); self.setState({moreInfo: []}); }); }, componentWillUnmount: function() { UserStore.removeSessionsChangeListener(this.onListenerChange); }, onListenerChange: function() { var newState = getStateFromStoresForSessions(); if (!utils.areStatesEqual(newState.sessions, this.state.sessions)) { this.setState(newState); } }, handleMoreInfo: function(index) { var newMoreInfo = this.state.moreInfo; newMoreInfo[index] = true; this.setState({moreInfo: newMoreInfo}); }, getInitialState: function() { var initialState = getStateFromStoresForSessions(); initialState.moreInfo = []; return initialState; }, render: function() { var activityList = []; var serverError = this.state.serverError; // Squash any false-y value for server error into null if (!serverError) { serverError = null; } for (var i = 0; i < this.state.sessions.length; i++) { var currentSession = this.state.sessions[i]; var lastAccessTime = new Date(currentSession.last_activity_at); var firstAccessTime = new Date(currentSession.create_at); var devicePicture = ''; if (currentSession.props.platform === 'Windows') { devicePicture = 'fa fa-windows'; } else if (currentSession.props.platform === 'Macintosh' || currentSession.props.platform === 'iPhone') { devicePicture = 'fa fa-apple'; } else if (currentSession.props.platform === 'Linux') { devicePicture = 'fa fa-linux'; } var moreInfo; if (this.state.moreInfo[i]) { moreInfo = (
{'First time active: ' + firstAccessTime.toDateString() + ', ' + lastAccessTime.toLocaleTimeString()}
{'OS: ' + currentSession.props.os}
{'Browser: ' + currentSession.props.browser}
{'Session ID: ' + currentSession.alt_id}
); } else { moreInfo = (More info); } activityList[i] = (
{currentSession.props.platform}
{'Last activity: ' + lastAccessTime.toDateString() + ', ' + lastAccessTime.toLocaleTimeString()}
{moreInfo}
); } var content; if (this.state.sessions.loading) { content = (); } else { content = (
{activityList}
); } return (
); } });