var React = require('react'); var ReactBootstrap = require('react-bootstrap'); var Grid = ReactBootstrap.Grid; var Row = ReactBootstrap.Row; var Col = ReactBootstrap.Col; var FormGroup = ReactBootstrap.FormGroup; var ControlLabel = ReactBootstrap.ControlLabel; var Button = ReactBootstrap.Button; var Combobox = require('react-widgets').Combobox; var models = require('../models'); var Suggestion = models.Suggestion; module.exports = React.createClass({ displayName: "NewSuggestion", getInitialState: function() { return { attendee: null, suggestion: null }; }, componentWillMount: function() { this.setState({ attendee: null, suggestion: null }); this.pickNewAttendee(this.props); }, componentWillReceiveProps: function(newProps) { this.pickNewAttendee(newProps); }, pickNewAttendee: function(props) { // Pick a new attendee if the current one can't be valid *and* we have // a list of valid possibilities // Find the last suggestion, if possible var lastSuggestion = null; var lastSuggestionId = this.getLastSuggestion(props); if (props.suggestions.hasOwnProperty(lastSuggestionId)) lastSuggestion = props.suggestions[lastSuggestionId]; if (this.state.attendee == null || !props.attendees.hasOwnProperty(this.state.attendee.AttendeeId) || (lastSuggestion && lastSuggestion.AttendeeId == this.state.attendee.AttendeeId)) { var attendeeList = this.getAttendeeList(props); if (attendeeList.length >= 1) { this.setState({ attendee: attendeeList[0] }); } else { this.setState({ attendee: null }); } } }, getAttendeeList: function(props) { if (!props) props = this.props; var attendeeList = []; var lastSuggestion = null; var lastSuggestionId = this.getLastSuggestion(props); if (props.suggestions.hasOwnProperty(lastSuggestionId)) lastSuggestion = props.suggestions[lastSuggestionId]; for (var attendeeId in props.attendees) { if (!lastSuggestion || lastSuggestion.AttendeeId != parseInt(attendeeId, 10)) attendeeList.push(props.attendees[attendeeId]); } return attendeeList; }, getLastSuggestion: function(props) { if (!props) props = this.props; var lastSuggestion = -1; for (var suggestionId in props.suggestions) { if (suggestionId > lastSuggestion) lastSuggestion = suggestionId; } return lastSuggestion; }, onChangeAttendee: function(attendee) { if (attendee.hasOwnProperty("AttendeeId")) { this.setState({ attendee: attendee }); } }, onChangeSuggestion: function(suggestion) { var suggestionString = suggestion; if (suggestion.hasOwnProperty('RestaurantName')) suggestionString = suggestion.RestaurantName; this.setState({ suggestion: suggestionString }); }, onAddSuggestion: function() { var suggestion = new Suggestion(); suggestion.AttendeeId = this.state.attendee.AttendeeId; suggestion.RestaurantName = this.state.suggestion; var lastSuggestionIndex = this.getLastSuggestion(); if (lastSuggestionIndex > -1) suggestion.VetoingId = this.props.suggestions[lastSuggestionIndex].SuggestionId; else suggestion.VetoingId = -1; this.props.createSuggestion(suggestion); this.setState({ attendee: null, suggestion: null }); }, unusedPopularSuggestions: function() { var props = this.props; return props.popularSuggestions.filter( function(suggestion){ for (var suggestionId in props.suggestions) { if (props.suggestions[suggestionId].RestaurantName == suggestion.RestaurantName) return false; } return true; }); }, render: function() { var attendeeList = this.getAttendeeList(); var buttonDisabled = this.state.attendee == null || !this.state.suggestion; var suggestionLabel = "Add Suggestion To:"; var byLabel = "Suggested By:" if (Object.keys(this.props.suggestions).length > 0) { suggestionLabel = "Veto With:"; byLabel = "Vetoed By:" } return ( {suggestionLabel} {byLabel} ); } });