Add restaurants

This commit is contained in:
2016-12-23 20:38:59 -05:00
parent 545c74f214
commit 4c7b9310c0
13 changed files with 435 additions and 0 deletions

View File

@ -0,0 +1,93 @@
var AttendeeConstants = require('../constants/AttendeeConstants');
var ErrorActions = require('./ErrorActions');
var models = require('../models.js');
var Attendee = models.Attendee;
var Error = models.Error;
function fetchAttendees() {
return {
type: AttendeeConstants.FETCH_ATTENDEES
}
}
function attendeesFetched(attendees) {
return {
type: AttendeeConstants.ATTENDEES_FETCHED,
attendees: attendees
}
}
function createAttendee() {
return {
type: AttendeeConstants.CREATE_ATTENDEE
}
}
function attendeeCreated(attendee) {
return {
type: AttendeeConstants.ATTENDEE_CREATED,
attendee: attendee
}
}
function fetchAll() {
return function (dispatch) {
dispatch(fetchAttendees());
$.ajax({
type: "GET",
dataType: "json",
url: "attendee/",
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
ErrorActions.serverError(e);
} else {
dispatch(attendeesFetched(data.attendees.map(function(json) {
var a = new Attendee();
a.fromJSON(json);
return a;
})));
}
},
error: function(jqXHR, status, error) {
ErrorActions.ajaxError(e);
}
});
};
}
function create(attendee) {
return function (dispatch) {
dispatch(createAttendee());
$.ajax({
type: "POST",
dataType: "json",
url: "attendee/",
data: {attendee: attendee.toJSON()},
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
ErrorActions.serverError(e);
} else {
var a = new Attendee();
a.fromJSON(data);
dispatch(attendeeCreated(a));
}
},
error: function(jqXHR, status, error) {
ErrorActions.ajaxError(e);
}
});
};
}
module.exports = {
fetchAll: fetchAll,
create: create
};

View File

@ -0,0 +1,93 @@
var RestaurantConstants = require('../constants/RestaurantConstants');
var ErrorActions = require('./ErrorActions');
var models = require('../models.js');
var Restaurant = models.Restaurant;
var Error = models.Error;
function fetchRestaurants() {
return {
type: RestaurantConstants.FETCH_RESTAURANTS
}
}
function restaurantsFetched(restaurants) {
return {
type: RestaurantConstants.RESTAURANTS_FETCHED,
restaurants: restaurants
}
}
function createRestaurant() {
return {
type: RestaurantConstants.CREATE_RESTAURANT
}
}
function restaurantCreated(restaurant) {
return {
type: RestaurantConstants.RESTAURANT_CREATED,
restaurant: restaurant
}
}
function fetchAll() {
return function (dispatch) {
dispatch(fetchRestaurants());
$.ajax({
type: "GET",
dataType: "json",
url: "restaurant/",
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) {
var a = new Restaurant();
a.fromJSON(json);
return a;
})));
}
},
error: function(jqXHR, status, error) {
ErrorActions.ajaxError(e);
}
});
};
}
function create(restaurant) {
return function (dispatch) {
dispatch(createRestaurant());
$.ajax({
type: "POST",
dataType: "json",
url: "restaurant/",
data: {restaurant: restaurant.toJSON()},
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
ErrorActions.serverError(e);
} else {
var a = new Restaurant();
a.fromJSON(data);
dispatch(restaurantCreated(a));
}
},
error: function(jqXHR, status, error) {
ErrorActions.ajaxError(e);
}
});
};
}
module.exports = {
fetchAll: fetchAll,
create: create
};

View File

@ -1,5 +1,7 @@
var UserConstants = require('../constants/UserConstants');
var AttendeeActions = require('./AttendeeActions');
var RestaurantActions = require('./RestaurantActions');
var ErrorActions = require('./ErrorActions');
var models = require('../models.js');
@ -89,6 +91,8 @@ function fetch(userId) {
function initializeSession(dispatch, session) {
dispatch(userLoggedIn(session));
dispatch(fetch(session.UserId));
dispatch(AttendeeActions.fetchAll());
dispatch(RestaurantActions.fetchAll());
}
function login(user) {