Advertisement
Guest User

Untitled

a guest
Jun 19th, 2015
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var React = require('React');
  2. var ReactRouter = require('react-router');
  3. var Link = require('react-router').Link;
  4. var Loader = require('halogen/PulseLoader');
  5.  
  6. var Modal = require('./ModalComponent.jsx');
  7.  
  8. /**
  9.  * The TeamUserRow displays information pertaining to a single
  10.  * Mock User and displays buttons for actions pertaining to that
  11.  * user.
  12.  */
  13. var TeamUserRow = React.createClass({
  14.  
  15.     getInitialState: function() {
  16.         return {filtered: false};
  17.     },
  18.  
  19.     componentWillReceiveProps: function(nextProps) {
  20.         this.updateFiltered(nextProps.filter);
  21.     },
  22.  
  23.     render: function() {
  24.         console.log(this.context);
  25.         return(
  26.             <tr hidden={this.state.filtered}>
  27.                 <td>{ this.props.user.mock_user }</td>
  28.                 <td>{ this.props.user.url }</td>
  29.                 <td>{ this.props.user.dynamic_mode.toUpperCase() }</td>
  30.                 <td>
  31.                     <Link to="endpoint" params={{teamId: this.props.teamName, profileName: this.props.user.mock_user}}>
  32.                         <button className="fa fa-eye"></button>
  33.                     </Link>
  34.                     <button className="fa fa-plus-circle"></button>
  35.                     <button className="fa fa-files-o"></button>
  36.                     <button className="fa fa-trash-o"></button>
  37.                 </td>
  38.             </tr>
  39.         );
  40.     },
  41.  
  42.     /**
  43.      * Applies a filter string and determines if this row
  44.      * should be hidden.
  45.      * @param newFilter the new filter to apply
  46.      */
  47.     updateFiltered: function(newFilter) {
  48.         // Creates a fuzzy regex to see if we need to show this row
  49.         // e.g. "hey" -> "h.*?e.*?y.*"
  50.         var fuzzyRegex = newFilter.toLowerCase().split("").join(".*?") + ".*";
  51.         var re = new RegExp(fuzzyRegex);
  52.         var matches = re.test(this.props.user.mock_user.toLowerCase());
  53.  
  54.         this.setState({filtered: !matches});
  55.     }
  56.  
  57. });
  58.  
  59. /**
  60.  * The TeamNavigationBar component has the controls
  61.  * to add a user, to import Mock Data, and to filter
  62.  * the list of users.
  63.  */
  64. var TeamNavigationBar = React.createClass({
  65.    render: function() {
  66.        return (
  67.            <header className="team-navigation-bar" role="banner">
  68.                <input type="search" placeholder="Filter Users" onChange={this.props.onFilterUsers} />
  69.                <button onClick={this.props.createUserButtonClicked}>Create User</button>
  70.            </header>
  71.        );
  72.    }
  73. });
  74.  
  75. /**
  76.  * The TeamPage component shows a list of users that exist
  77.  * for the current team and some information about them.
  78.  * It also allows the creation of new mock users and the ability
  79.  * to filter the list using a search bar.
  80.  */
  81. var TeamPage = React.createClass({
  82.  
  83.     /**
  84.      * The initial state has no users and is loading.
  85.      * This is updated on an ajax callback once we have data.
  86.      * We also initialize with no filter to match, showing all users.
  87.      * @returns {{users: null, loading: boolean, filter: string}}
  88.      */
  89.     getInitialState: function() {
  90.         return {users: null, loading: true, filter: "", isCreatingUser: false};
  91.     },
  92.  
  93.     /**
  94.      * As soon as the component as mounted we want to make
  95.      * an AJAX call and fetch the user data. Once we have it
  96.      * we exit the loading state.
  97.      */
  98.     componentDidMount: function () {
  99.         $.ajax({
  100.             url: "/api/teams/" + this.props.params.teamID,
  101.             dataType: "json",
  102.             success: function (data) {
  103.                 this.setState({users: data, loading: false});
  104.             }.bind(this),
  105.             error: function (xhr, status, err) {
  106.                 console.error(status, err.toString());
  107.             }
  108.         });
  109.     },
  110.  
  111.     /**
  112.      * Updates our state from the TeamNavigationBar
  113.      * so that we can show and hide our user rows accordingly.
  114.      * @param event the input change event containing the new filter
  115.      */
  116.     onFilterUsers: function (event) {
  117.         this.setState({filter: event.target.value});
  118.     },
  119.  
  120.    render: function() {
  121.        console.log(this.context);
  122.        // Display a loading message if we are in loading mode
  123.        // if we aren't loading, create a table of data
  124.        var tableData;
  125.        var loader;
  126.        if (this.state.loading) {
  127.            loader = <tr><Loader color="#26A65B" size="16px" margin="4px"/></tr>;
  128.        } else {
  129.            tableData = [];
  130.            console.log(this.navigateToEndpointPage);
  131.            for (var i = 0; i < this.state.users.length; i++) {
  132.                var user = this.state.users[i];
  133.                tableData.push(<TeamUserRow key={i} user={user}
  134.                                            filter={this.state.filter} team={this.props.params.teamID} />);
  135.            }
  136.        }
  137.  
  138.        return (
  139.          <div id="team-page">
  140.              <TeamNavigationBar teamName={this.props.params.teamID}
  141.                                 createUserButtonClicked={this.showCreateUserModal}
  142.                                 onFilterUsers={this.onFilterUsers} />
  143.              {loader}
  144.              <table>
  145.                  <tr>
  146.                      <th>User</th>
  147.                      <th>Endpoint URL</th>
  148.                      <th>Mode</th>
  149.                      <th>Actions</th>
  150.                  </tr>
  151.                  <tbody>
  152.                     {tableData}
  153.                  </tbody>
  154.              </table>
  155.  
  156.              <Modal hidden={!this.state.isCreatingUser}
  157.                     message="Enter New User Info"
  158.                     fields={["Name", "Endpoint URL"]}
  159.                     onConfirm={this.handleCreateUser}
  160.                     onCancel={this.dismissCreateUserModal} />
  161.          </div>
  162.        );
  163.    },
  164.  
  165.    /////////////////////////////////////
  166.    // Create User Modal Functionality //
  167.    /////////////////////////////////////
  168.  
  169.    handleCreateUser: function(userData) {
  170.        $.ajax({
  171.           type: "POST",
  172.           url: "/api/users/" + this.props.params.teamID,
  173.           data: {mock_name: userData["Name"], url: userData["Endpoint URL"]},
  174.           success: function(data) {
  175.               var newUsers = this.state.users;
  176.               newUsers.push(data.user);
  177.               this.setState({users: newUsers});
  178.           }.bind(this)
  179.        });
  180.  
  181.        this.dismissCreateUserModal();
  182.    },
  183.  
  184.    showCreateUserModal: function() {
  185.        this.setState({isCreatingUser: true});
  186.    },
  187.  
  188.    dismissCreateUserModal: function() {
  189.        this.setState({isCreatingUser: false});
  190.    }
  191.  
  192. });
  193.  
  194. module.exports = TeamPage;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement