// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. // See License.txt for license information. var UserStore = require('../stores/user_store.jsx'); var AsyncClient = require('../utils/async_client.jsx'); var LoadingScreen = require('./loading_screen.jsx'); var utils = require('../utils/utils.jsx'); function getStateFromStoresForAudits() { return { audits: UserStore.getAudits() }; } module.exports = React.createClass({ displayName: 'AccessHistoryModal', componentDidMount: function() { UserStore.addAuditsChangeListener(this.onListenerChange); $(this.refs.modal.getDOMNode()).on('shown.bs.modal', function() { AsyncClient.getAudits(); }); var self = this; $(this.refs.modal.getDOMNode()).on('hidden.bs.modal', function() { $('#user_settings').modal('show'); self.setState({moreInfo: []}); }); }, componentWillUnmount: function() { UserStore.removeAuditsChangeListener(this.onListenerChange); }, onListenerChange: function() { var newState = getStateFromStoresForAudits(); if (!utils.areStatesEqual(newState.audits, this.state.audits)) { this.setState(newState); } }, handleMoreInfo: function(index) { var newMoreInfo = this.state.moreInfo; newMoreInfo[index] = true; this.setState({moreInfo: newMoreInfo}); }, getInitialState: function() { var initialState = getStateFromStoresForAudits(); initialState.moreInfo = []; return initialState; }, render: function() { var accessList = []; var currentHistoryDate = null; for (var i = 0; i < this.state.audits.length; i++) { var currentAudit = this.state.audits[i]; var newHistoryDate = new Date(currentAudit.create_at); var newDate = null; if (!currentHistoryDate || currentHistoryDate.toLocaleDateString() !== newHistoryDate.toLocaleDateString()) { currentHistoryDate = newHistoryDate; newDate = (
{currentHistoryDate.toDateString()}
); } if (!currentAudit.session_id && currentAudit.action.search('/users/login') !== -1) { currentAudit.session_id = 'N/A (Login attempt)'; } var moreInfo = (More info); if (this.state.moreInfo[i]) { moreInfo = (
{'Session ID: ' + currentAudit.session_id}
{'URL: ' + currentAudit.action.replace(/\/api\/v[1-9]/, '')}
); } var divider = null; if (i < this.state.audits.length - 1) { divider = (
) } accessList[i] = (
{newDate}
{newHistoryDate.toLocaleTimeString(navigator.language, {hour: '2-digit', minute: '2-digit'})}
{'IP: ' + currentAudit.ip_address}
{moreInfo}
{divider}
); } var content; if (this.state.audits.loading) { content = (); } else { content = (
{accessList}
); } return (
); } });