2016-12-29 20:15:39 -05:00
|
|
|
var React = require('react');
|
|
|
|
|
|
|
|
var ReactBootstrap = require('react-bootstrap');
|
|
|
|
var Grid = ReactBootstrap.Grid;
|
|
|
|
var Row = ReactBootstrap.Row;
|
|
|
|
var Col = ReactBootstrap.Col;
|
2017-01-01 08:10:58 -05:00
|
|
|
var FormGroup = ReactBootstrap.FormGroup;
|
2016-12-29 20:15:39 -05:00
|
|
|
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
|
2017-01-01 21:15:22 -05:00
|
|
|
|
|
|
|
// Find the last suggestion, if possible
|
|
|
|
var lastSuggestion = null;
|
2017-01-02 07:38:37 -05:00
|
|
|
var lastSuggestionId = this.getLastSuggestion(props);
|
|
|
|
if (props.suggestions.hasOwnProperty(lastSuggestionId))
|
|
|
|
lastSuggestion = props.suggestions[lastSuggestionId];
|
2017-01-01 21:15:22 -05:00
|
|
|
|
2016-12-29 20:15:39 -05:00
|
|
|
if (this.state.attendee == null ||
|
2017-01-01 21:15:22 -05:00
|
|
|
!props.attendees.hasOwnProperty(this.state.attendee.AttendeeId) ||
|
|
|
|
(lastSuggestion && lastSuggestion.AttendeeId == this.state.attendee.AttendeeId)) {
|
2017-01-02 07:38:37 -05:00
|
|
|
var attendeeList = this.getAttendeeList(props);
|
2017-01-01 21:15:22 -05:00
|
|
|
if (attendeeList.length >= 1) {
|
2016-12-29 20:15:39 -05:00
|
|
|
this.setState({
|
2017-01-01 21:15:22 -05:00
|
|
|
attendee: attendeeList[0]
|
2016-12-29 20:15:39 -05:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
attendee: null
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2017-01-02 07:38:37 -05:00
|
|
|
getAttendeeList: function(props) {
|
|
|
|
if (!props)
|
|
|
|
props = this.props;
|
|
|
|
|
2016-12-29 20:15:39 -05:00
|
|
|
var attendeeList = [];
|
2017-01-01 21:15:22 -05:00
|
|
|
var lastSuggestion = null;
|
|
|
|
|
2017-01-02 07:38:37 -05:00
|
|
|
var lastSuggestionId = this.getLastSuggestion(props);
|
|
|
|
if (props.suggestions.hasOwnProperty(lastSuggestionId))
|
|
|
|
lastSuggestion = props.suggestions[lastSuggestionId];
|
2017-01-01 21:15:22 -05:00
|
|
|
|
2017-01-02 07:38:37 -05:00
|
|
|
for (var attendeeId in props.attendees) {
|
2017-01-01 21:15:22 -05:00
|
|
|
if (!lastSuggestion || lastSuggestion.AttendeeId != parseInt(attendeeId, 10))
|
2017-01-02 07:38:37 -05:00
|
|
|
attendeeList.push(props.attendees[attendeeId]);
|
2016-12-29 20:15:39 -05:00
|
|
|
}
|
|
|
|
return attendeeList;
|
|
|
|
},
|
2017-01-02 07:38:37 -05:00
|
|
|
getLastSuggestion: function(props) {
|
|
|
|
if (!props)
|
|
|
|
props = this.props;
|
|
|
|
|
2016-12-29 20:15:39 -05:00
|
|
|
var lastSuggestion = -1;
|
2017-01-02 07:38:37 -05:00
|
|
|
for (var suggestionId in props.suggestions) {
|
2016-12-29 20:15:39 -05:00
|
|
|
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;
|
|
|
|
suggestion.VetoingId = this.getLastSuggestion();
|
|
|
|
this.props.createSuggestion(suggestion);
|
2016-12-29 21:22:00 -05:00
|
|
|
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;
|
|
|
|
});
|
2016-12-29 20:15:39 -05:00
|
|
|
},
|
|
|
|
render: function() {
|
|
|
|
var attendeeList = this.getAttendeeList();
|
|
|
|
var buttonDisabled = this.state.attendee == null || !this.state.suggestion;
|
2017-01-01 08:17:02 -05:00
|
|
|
var suggestionLabel = "Add Suggestion To:";
|
|
|
|
var byLabel = "Suggested By:"
|
|
|
|
if (Object.keys(this.props.suggestions).length > 0) {
|
|
|
|
suggestionLabel = "Veto With:";
|
|
|
|
byLabel = "Vetoed By:"
|
|
|
|
}
|
2016-12-29 20:15:39 -05:00
|
|
|
return (
|
2017-01-01 08:10:58 -05:00
|
|
|
<Row>
|
|
|
|
<Col xs={4}>
|
|
|
|
<FormGroup controlId="newsuggestionrestaurantname">
|
2017-01-01 08:17:02 -05:00
|
|
|
<ControlLabel>{suggestionLabel}</ControlLabel>
|
2017-01-01 08:10:58 -05:00
|
|
|
<Combobox
|
|
|
|
value={this.state.suggestion}
|
|
|
|
data={this.unusedPopularSuggestions()}
|
|
|
|
valueField='RestaurantName'
|
|
|
|
textField='RestaurantName'
|
|
|
|
onChange={this.onChangeSuggestion} />
|
|
|
|
</FormGroup>
|
|
|
|
</Col>
|
|
|
|
<Col xs={1}></Col>
|
|
|
|
<Col xs={4}>
|
|
|
|
<FormGroup controlId="newsuggestionby">
|
2017-01-01 08:17:02 -05:00
|
|
|
<ControlLabel>{byLabel}</ControlLabel>
|
2017-01-01 08:10:58 -05:00
|
|
|
<Combobox
|
|
|
|
value={this.state.attendee}
|
|
|
|
data={attendeeList}
|
|
|
|
valueField='AttendeeId'
|
|
|
|
textField='Name'
|
|
|
|
onChange={this.onChangeAttendee} />
|
|
|
|
</FormGroup>
|
|
|
|
</Col>
|
|
|
|
<Col xs={1}></Col>
|
|
|
|
<Col xs={2}>
|
|
|
|
<FormGroup controlId="addnewsuggestionbutton">
|
2017-01-01 20:13:26 -05:00
|
|
|
<Button bsClass="addsuggestionbutton btn" bsStyle="success" disabled={buttonDisabled} onClick={this.onAddSuggestion}>Add</Button>
|
2017-01-01 08:10:58 -05:00
|
|
|
</FormGroup>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2016-12-29 20:15:39 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|