lunch/js/actions/SuggestionActions.js

175 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-12-24 10:00:21 -05:00
var SuggestionConstants = require('../constants/SuggestionConstants');
2016-12-23 20:38:59 -05:00
var ErrorActions = require('./ErrorActions');
var models = require('../models');
2016-12-24 10:00:21 -05:00
var Suggestion = models.Suggestion;
var PopularSuggestion = models.PopularSuggestion;
2016-12-23 20:38:59 -05:00
var Error = models.Error;
2016-12-24 10:00:21 -05:00
function fetchSuggestions() {
2016-12-23 20:38:59 -05:00
return {
2016-12-24 10:00:21 -05:00
type: SuggestionConstants.FETCH_SUGGESTIONS
2016-12-23 20:38:59 -05:00
}
}
function suggestionsFetched(suggestions) {
2016-12-23 20:38:59 -05:00
return {
2016-12-24 10:00:21 -05:00
type: SuggestionConstants.SUGGESTIONS_FETCHED,
suggestions: suggestions
2016-12-23 20:38:59 -05:00
}
}
2016-12-24 10:00:21 -05:00
function createSuggestion() {
2016-12-23 20:38:59 -05:00
return {
2016-12-24 10:00:21 -05:00
type: SuggestionConstants.CREATE_SUGGESTION
2016-12-23 20:38:59 -05:00
}
}
function suggestionCreated(suggestion) {
2016-12-23 20:38:59 -05:00
return {
2016-12-24 10:00:21 -05:00
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
2016-12-23 20:38:59 -05:00
}
}
function fetchAll() {
return function (dispatch) {
2016-12-24 10:00:21 -05:00
dispatch(fetchSuggestions());
2016-12-23 20:38:59 -05:00
$.ajax({
type: "GET",
dataType: "json",
url: "suggestion/",
2016-12-23 20:38:59 -05:00
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
dispatch(ErrorActions.serverError(e));
2016-12-23 20:38:59 -05:00
} else {
dispatch(suggestionsFetched(data.suggestions.map(function(json) {
2016-12-24 10:00:21 -05:00
var a = new Suggestion();
2016-12-23 20:38:59 -05:00
a.fromJSON(json);
return a;
})));
}
},
error: function(jqXHR, status, error) {
dispatch(ErrorActions.ajaxError(e));
2016-12-23 20:38:59 -05:00
}
});
};
}
function create(suggestion) {
2016-12-23 20:38:59 -05:00
return function (dispatch) {
2016-12-24 10:00:21 -05:00
dispatch(createSuggestion());
2016-12-23 20:38:59 -05:00
$.ajax({
type: "POST",
dataType: "json",
url: "suggestion/",
data: {suggestion: suggestion.toJSON()},
2016-12-23 20:38:59 -05:00
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
dispatch(ErrorActions.serverError(e));
2016-12-23 20:38:59 -05:00
} else {
2016-12-24 10:00:21 -05:00
var a = new Suggestion();
2016-12-23 20:38:59 -05:00
a.fromJSON(data);
dispatch(suggestionCreated(a));
}
},
error: function(jqXHR, status, error) {
dispatch(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()) {
dispatch(ErrorActions.serverError(e));
} else {
dispatch(popularSuggestionsFetched(data.popularsuggestions.map(function(json) {
var a = new PopularSuggestion();
a.fromJSON(json);
return a;
})));
2016-12-23 20:38:59 -05:00
}
},
error: function(jqXHR, status, error) {
dispatch(ErrorActions.ajaxError(e));
2016-12-23 20:38:59 -05:00
}
});
};
}
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(e));
}
});
};
}
2016-12-23 20:38:59 -05:00
module.exports = {
fetchAll: fetchAll,
create: create,
remove: remove,
fetchPopular: fetchPopular
2016-12-23 20:38:59 -05:00
};