From 49409f84d2449d9e70f46c24697954e3ac70844e Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Thu, 29 Dec 2016 20:15:39 -0500 Subject: [PATCH] Add ability to add suggestions to UI --- attendees.go | 4 +- js/components/NewSuggestion.js | 117 +++++++++++++++++++++++++++++++++ js/components/RecordLunch.js | 46 +++++++++++-- suggestions.go | 2 +- 4 files changed, 160 insertions(+), 9 deletions(-) create mode 100644 js/components/NewSuggestion.js diff --git a/attendees.go b/attendees.go index 8b599c0..361fb27 100644 --- a/attendees.go +++ b/attendees.go @@ -140,6 +140,8 @@ func DeleteAttendee(a *Attendee) error { return err } + // TODO ensure attendee isn't used in any suggestions + count, err := transaction.Delete(a) if err != nil { transaction.Rollback() @@ -166,7 +168,7 @@ func AttendeeHandler(w http.ResponseWriter, r *http.Request) { return } - today := time.Now().Truncate(time.Hour * 24) + today := time.Now().UTC().Truncate(time.Hour * 24) if r.Method == "POST" { attendee_json := r.PostFormValue("attendee") diff --git a/js/components/NewSuggestion.js b/js/components/NewSuggestion.js new file mode 100644 index 0000000..918a251 --- /dev/null +++ b/js/components/NewSuggestion.js @@ -0,0 +1,117 @@ +var React = require('react'); + +var ReactBootstrap = require('react-bootstrap'); +var Grid = ReactBootstrap.Grid; +var Row = ReactBootstrap.Row; +var Col = ReactBootstrap.Col; +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 + if (this.state.attendee == null || + !props.attendees.hasOwnProperty(this.state.attendee.AttendeeId)) { + if (Object.keys(props.attendees).length >= 1) { + this.setState({ + attendee: props.attendees[Object.keys(props.attendees)[0]] + }); + } else { + this.setState({ + attendee: null + }); + } + } + }, + getAttendeeList: function() { + var attendeeList = []; + for (var attendeeId in this.props.attendees) { + attendeeList.push(this.props.attendees[attendeeId]); + } + return attendeeList; + }, + getLastSuggestion: function() { + var lastSuggestion = -1; + for (var suggestionId in this.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; + suggestion.VetoingId = this.getLastSuggestion(); + this.props.createSuggestion(suggestion); + }, + render: function() { + var attendeeList = this.getAttendeeList(); + var buttonDisabled = this.state.attendee == null || !this.state.suggestion; + return ( + + + + + + + + + + + + + + + + ); + } +}); diff --git a/js/components/RecordLunch.js b/js/components/RecordLunch.js index 3a0f21f..8b962f6 100644 --- a/js/components/RecordLunch.js +++ b/js/components/RecordLunch.js @@ -3,18 +3,19 @@ var React = require('react'); var ReactBootstrap = require('react-bootstrap'); var FormGroup = ReactBootstrap.FormGroup; var ControlLabel = ReactBootstrap.ControlLabel; +var Grid = ReactBootstrap.Grid; +var Row = ReactBootstrap.Row; +var Col = ReactBootstrap.Col; var Multiselect = require('react-widgets').Multiselect; -var models = require('../models') -var Attendee = models.Attendee +var models = require('../models'); +var Attendee = models.Attendee; + +var NewSuggestion = require('./NewSuggestion'); module.exports = React.createClass({ displayName: "RecordLunch", - getInitialState: function() { - return { - }; - }, getAttendeeList: function() { var attendeeList = []; for (var attendeeId in this.props.attendees) { @@ -52,9 +53,25 @@ module.exports = React.createClass({ }, render: function() { var attendeeList = this.getAttendeeList(); + + var suggestionIds = Object.keys(this.props.suggestions); + suggestionIds.sort(); + var suggestions = []; + for (var i in suggestionIds) { + var suggestion = this.props.suggestions[suggestionIds[i]]; + suggestions.push(( + + {this.props.attendees[suggestion.AttendeeId].Name} + + {suggestion.RestaurantName} + + )); + } + return (
- + + Attendees + + + Suggested By: + + Restaurant Name: + + {suggestions} + + + +
); } diff --git a/suggestions.go b/suggestions.go index 77d1262..1d9e7bd 100644 --- a/suggestions.go +++ b/suggestions.go @@ -131,7 +131,7 @@ func SuggestionHandler(w http.ResponseWriter, r *http.Request) { return } - today := time.Now().Truncate(time.Hour * 24) + today := time.Now().UTC().Truncate(time.Hour * 24) if r.Method == "POST" { suggestion_json := r.PostFormValue("suggestion")