Add attendees and suggestions (except for UI)

This commit is contained in:
2016-12-26 08:29:18 -05:00
parent c4f07d3b3e
commit c8232050da
11 changed files with 283 additions and 14 deletions

View File

@ -32,6 +32,19 @@ function attendeeCreated(attendee) {
}
}
function fetchPopularAttendees() {
return {
type: AttendeeConstants.FETCH_POPULAR_ATTENDEES
}
}
function popularAttendeesFetched(attendees) {
return {
type: AttendeeConstants.POPULAR_ATTENDEES_FETCHED,
attendees: attendees
}
}
function fetchAll() {
return function (dispatch) {
dispatch(fetchAttendees());
@ -87,7 +100,36 @@ function create(attendee) {
};
}
function fetchPopular() {
return function (dispatch) {
dispatch(fetchPopularAttendees());
$.ajax({
type: "GET",
dataType: "json",
url: "popularattendees/",
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
ErrorActions.serverError(e);
} else {
dispatch(popularAttendeesFetched(data.attendees.map(function(json) {
var a = new Attendee();
a.fromJSON(json);
return a;
})));
}
},
error: function(jqXHR, status, error) {
ErrorActions.ajaxError(e);
}
});
};
}
module.exports = {
fetchAll: fetchAll,
create: create
create: create,
fetchPopular: fetchPopular
};

View File

@ -12,10 +12,10 @@ function fetchSuggestions() {
}
}
function restaurantsFetched(restaurants) {
function suggestionsFetched(suggestions) {
return {
type: SuggestionConstants.SUGGESTIONS_FETCHED,
restaurants: restaurants
suggestions: suggestions
}
}
@ -25,10 +25,23 @@ function createSuggestion() {
}
}
function restaurantCreated(restaurant) {
function suggestionCreated(suggestion) {
return {
type: SuggestionConstants.SUGGESTION_CREATED,
restaurant: restaurant
suggestion: suggestion
}
}
function fetchPopularSuggestions() {
return {
type: SuggestionConstants.FETCH_POPULAR_SUGGESTIONS
}
}
function popularSuggestionsFetched(suggestions) {
return {
type: SuggestionConstants.POPULAR_SUGGESTIONS_FETCHED,
suggestions: suggestions
}
}
@ -39,14 +52,14 @@ function fetchAll() {
$.ajax({
type: "GET",
dataType: "json",
url: "restaurant/",
url: "suggestion/",
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
ErrorActions.serverError(e);
} else {
dispatch(restaurantsFetched(data.restaurants.map(function(json) {
dispatch(suggestionsFetched(data.suggestions.map(function(json) {
var a = new Suggestion();
a.fromJSON(json);
return a;
@ -60,15 +73,15 @@ function fetchAll() {
};
}
function create(restaurant) {
function create(suggestion) {
return function (dispatch) {
dispatch(createSuggestion());
$.ajax({
type: "POST",
dataType: "json",
url: "restaurant/",
data: {restaurant: restaurant.toJSON()},
url: "suggestion/",
data: {suggestion: suggestion.toJSON()},
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
@ -77,7 +90,35 @@ function create(restaurant) {
} else {
var a = new Suggestion();
a.fromJSON(data);
dispatch(restaurantCreated(a));
dispatch(suggestionCreated(a));
}
},
error: function(jqXHR, status, error) {
ErrorActions.ajaxError(e);
}
});
};
}
function fetchPopular() {
return function (dispatch) {
dispatch(fetchPopularSuggestions());
$.ajax({
type: "GET",
dataType: "json",
url: "popularsuggestions/",
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
ErrorActions.serverError(e);
} else {
dispatch(popularSuggestionsFetched(data.suggestions.map(function(json) {
var a = new Suggestion();
a.fromJSON(json);
return a;
})));
}
},
error: function(jqXHR, status, error) {
@ -89,5 +130,6 @@ function create(restaurant) {
module.exports = {
fetchAll: fetchAll,
create: create
create: create,
fetchPopular: fetchPopular
};

View File

@ -93,6 +93,8 @@ function initializeSession(dispatch, session) {
dispatch(fetch(session.UserId));
dispatch(AttendeeActions.fetchAll());
dispatch(SuggestionActions.fetchAll());
dispatch(AttendeeActions.fetchPopular());
dispatch(SuggestionActions.fetchPopular());
}
function login(user) {

View File

@ -4,5 +4,7 @@ module.exports = keyMirror({
FETCH_ATTENDEES: null,
ATTENDEES_FETCHED: null,
CREATE_ATTENDEE: null,
ATTENDEE_CREATED: null
ATTENDEE_CREATED: null,
FETCH_POPULAR_ATTENDEES: null,
POPULAR_ATTENDEES_FETCHED: null
});

View File

@ -4,5 +4,7 @@ module.exports = keyMirror({
FETCH_SUGGESTIONS: null,
SUGGESTIONS_FETCHED: null,
CREATE_SUGGESTION: null,
SUGGESTION_CREATED: null
SUGGESTION_CREATED: null,
FETCH_POPULAR_SUGGESTIONS: null,
POPULAR_SUGGESTIONS_FETCHED: null,
});

View File

@ -3,13 +3,17 @@ var Redux = require('redux');
var UserReducer = require('./UserReducer');
var SessionReducer = require('./SessionReducer');
var AttendeeReducer = require('./AttendeeReducer');
var PopularAttendeeReducer = require('./PopularAttendeeReducer');
var SuggestionReducer = require('./SuggestionReducer');
var PopularSuggestionReducer = require('./PopularSuggestionReducer');
var ErrorReducer = require('./ErrorReducer');
module.exports = Redux.combineReducers({
user: UserReducer,
session: SessionReducer,
attendees: AttendeeReducer,
popularAttendees: PopularAttendeeReducer,
suggestions: SuggestionReducer,
popularSuggestions: PopularSuggestionReducer,
error: ErrorReducer
});

View File

@ -0,0 +1,20 @@
var assign = require('object-assign');
var AttendeeConstants = require('../constants/AttendeeConstants');
var UserConstants = require('../constants/UserConstants');
module.exports = function(state = {}, action) {
switch (action.type) {
case AttendeeConstants.POPULAR_ATTENDEES_FETCHED:
var attendees = {};
for (var i = 0; i < action.attendees.length; i++) {
var attendee = action.attendees[i];
attendees[attendee.AttendeeId] = attendee;
}
return attendees;
case UserConstants.USER_LOGGEDOUT:
return {};
default:
return state;
}
};

View File

@ -0,0 +1,20 @@
var assign = require('object-assign');
var SuggestionConstants = require('../constants/SuggestionConstants');
var UserConstants = require('../constants/UserConstants');
module.exports = function(state = {}, action) {
switch (action.type) {
case SuggestionConstants.POPULAR_SUGGESTIONS_FETCHED:
var suggestions = {};
for (var i = 0; i < action.suggestions.length; i++) {
var suggestion = action.suggestions[i];
suggestions[suggestion.SuggestionId] = suggestion;
}
return suggestions;
case UserConstants.USER_LOGGEDOUT:
return {};
default:
return state;
}
};