lunch/js/actions/AttendeeActions.js

136 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-12-23 20:38:59 -05:00
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 fetchPopularAttendees() {
return {
type: AttendeeConstants.FETCH_POPULAR_ATTENDEES
}
}
function popularAttendeesFetched(attendees) {
return {
type: AttendeeConstants.POPULAR_ATTENDEES_FETCHED,
attendees: attendees
}
}
2016-12-23 20:38:59 -05:00
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);
}
});
};
}
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);
}
});
};
}
2016-12-23 20:38:59 -05:00
module.exports = {
fetchAll: fetchAll,
create: create,
fetchPopular: fetchPopular
2016-12-23 20:38:59 -05:00
};