// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. // See License.txt for license information. const UserStore = require('../stores/user_store.jsx'); const Client = require('../utils/client.jsx'); const AsyncClient = require('../utils/async_client.jsx'); const LoadingScreen = require('./loading_screen.jsx'); const Utils = require('../utils/utils.jsx'); 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.state = this.getStateFromStores(); this.state.moreInfo = []; } getStateFromStores() { return { sessions: UserStore.getSessions(), serverError: null, clientError: null }; } submitRevoke(altId, e) { e.preventDefault(); Client.revokeSession(altId, function handleRevokeSuccess() { AsyncClient.getSessions(); }, function handleRevokeError(err) { let state = this.getStateFromStores(); state.serverError = err; this.setState(state); }.bind(this) ); } componentDidMount() { UserStore.addSessionsChangeListener(this.onListenerChange); $(React.findDOMNode(this.refs.modal)).on('shown.bs.modal', function handleShow() { AsyncClient.getSessions(); }); $(React.findDOMNode(this.refs.modal)).on('hidden.bs.modal', function handleHide() { $('#user_settings').modal('show'); this.setState({moreInfo: []}); }.bind(this)); } componentWillUnmount() { UserStore.removeSessionsChangeListener(this.onListenerChange); } onListenerChange() { const newState = this.getStateFromStores(); if (!Utils.areStatesEqual(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 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'; } let 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.id}`}
); } else { moreInfo = ( More info ); } activityList[i] = (
{currentSession.props.platform}
{`Last activity: ${lastAccessTime.toDateString()}, ${lastAccessTime.toLocaleTimeString()}`}
{moreInfo}
); } let content; if (this.state.sessions.loading) { content = ; } else { content =
{activityList}
; } return (
); } }