var AttendeeConstants = require('../constants/AttendeeConstants'); var ErrorActions = require('./ErrorActions'); var Attendee = models.Attendee; var PopularAttendee = models.PopularAttendee; 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 removeAttendee() { return { type: AttendeeConstants.REMOVE_ATTENDEE } } function attendeeRemoved(attendeeId) { return { type: AttendeeConstants.ATTENDEE_REMOVED, attendeeId: attendeeId } } 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()); $.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.popularattendees.map(function(json) { var a = new PopularAttendee(); a.fromJSON(json); return a; }))); } }, error: function(jqXHR, status, error) { ErrorActions.ajaxError(e); } }); }; } function remove(attendee) { return function(dispatch) { dispatch(removeAttendee()); $.ajax({ type: "DELETE", dataType: "json", url: "attendee/"+attendee.AttendeeId+"/", success: function(data, status, jqXHR) { var e = new Error(); e.fromJSON(data); if (e.isError()) { ErrorActions.serverError(e); } else { dispatch(attendeeRemoved(attendee.AttendeeId)); } }, error: function(jqXHR, status, error) { ErrorActions.ajaxError(e); } }); }; } module.exports = { fetchAll: fetchAll, create: create, remove: remove, fetchPopular: fetchPopular };