lunch/js/actions/SuggestionActions.js

175 lines
3.6 KiB
JavaScript

var SuggestionConstants = require('../constants/SuggestionConstants');
var ErrorActions = require('./ErrorActions');
var models = require('../models');
var Suggestion = models.Suggestion;
var PopularSuggestion = models.PopularSuggestion;
var Error = models.Error;
function fetchSuggestions() {
return {
type: SuggestionConstants.FETCH_SUGGESTIONS
}
}
function suggestionsFetched(suggestions) {
return {
type: SuggestionConstants.SUGGESTIONS_FETCHED,
suggestions: suggestions
}
}
function createSuggestion() {
return {
type: SuggestionConstants.CREATE_SUGGESTION
}
}
function suggestionCreated(suggestion) {
return {
type: SuggestionConstants.SUGGESTION_CREATED,
suggestion: suggestion
}
}
function removeSuggestion() {
return {
type: SuggestionConstants.REMOVE_SUGGESTION
}
}
function suggestionRemoved(suggestionId) {
return {
type: SuggestionConstants.SUGGESTION_REMOVED,
suggestionId: suggestionId
}
}
function fetchPopularSuggestions() {
return {
type: SuggestionConstants.FETCH_POPULAR_SUGGESTIONS
}
}
function popularSuggestionsFetched(suggestions) {
return {
type: SuggestionConstants.POPULAR_SUGGESTIONS_FETCHED,
suggestions: suggestions
}
}
function fetchAll() {
return function (dispatch) {
dispatch(fetchSuggestions());
$.ajax({
type: "GET",
dataType: "json",
url: "suggestion/",
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
dispatch(ErrorActions.serverError(e));
} else {
dispatch(suggestionsFetched(data.suggestions.map(function(json) {
var a = new Suggestion();
a.fromJSON(json);
return a;
})));
}
},
error: function(jqXHR, status, error) {
dispatch(ErrorActions.ajaxError(error));
}
});
};
}
function create(suggestion) {
return function (dispatch) {
dispatch(createSuggestion());
$.ajax({
type: "POST",
dataType: "json",
url: "suggestion/",
data: {suggestion: suggestion.toJSON()},
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
dispatch(ErrorActions.serverError(e));
} else {
var a = new Suggestion();
a.fromJSON(data);
dispatch(suggestionCreated(a));
}
},
error: function(jqXHR, status, error) {
dispatch(ErrorActions.ajaxError(error));
}
});
};
}
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()) {
dispatch(ErrorActions.serverError(e));
} else {
dispatch(popularSuggestionsFetched(data.popularsuggestions.map(function(json) {
var a = new PopularSuggestion();
a.fromJSON(json);
return a;
})));
}
},
error: function(jqXHR, status, error) {
dispatch(ErrorActions.ajaxError(error));
}
});
};
}
function remove(suggestion) {
return function(dispatch) {
dispatch(removeSuggestion());
$.ajax({
type: "DELETE",
dataType: "json",
url: "suggestion/"+suggestion.SuggestionId+"/",
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
dispatch(ErrorActions.serverError(e));
} else {
dispatch(suggestionRemoved(suggestion.SuggestionId));
}
},
error: function(jqXHR, status, error) {
dispatch(ErrorActions.ajaxError(error));
}
});
};
}
module.exports = {
fetchAll: fetchAll,
create: create,
remove: remove,
fetchPopular: fetchPopular
};