lunch/js/actions/SuggestionActions.js

136 lines
2.7 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.js');
2016-12-24 10:00:21 -05:00
var Suggestion = models.Suggestion;
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 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()) {
ErrorActions.serverError(e);
} 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) {
ErrorActions.ajaxError(e);
}
});
};
}
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()) {
ErrorActions.serverError(e);
} 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) {
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()) {
ErrorActions.serverError(e);
} else {
dispatch(popularSuggestionsFetched(data.suggestions.map(function(json) {
var a = new Suggestion();
a.fromJSON(json);
return a;
})));
2016-12-23 20:38:59 -05:00
}
},
error: function(jqXHR, status, error) {
ErrorActions.ajaxError(e);
}
});
};
}
module.exports = {
fetchAll: fetchAll,
create: create,
fetchPopular: fetchPopular
2016-12-23 20:38:59 -05:00
};