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');
|
|
|
|
|
2016-12-28 09:25:20 -05:00
|
|
|
var models = require('../models');
|
2016-12-24 10:00:21 -05:00
|
|
|
var Suggestion = models.Suggestion;
|
2016-12-28 09:25:20 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 08:29:18 -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,
|
2016-12-26 08:29:18 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 08:29:18 -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,
|
2016-12-26 08:29:18 -05:00
|
|
|
suggestion: suggestion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-01 08:10:58 -05:00
|
|
|
function removeSuggestion() {
|
|
|
|
return {
|
|
|
|
type: SuggestionConstants.REMOVE_SUGGESTION
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function suggestionRemoved(suggestionId) {
|
|
|
|
return {
|
|
|
|
type: SuggestionConstants.SUGGESTION_REMOVED,
|
|
|
|
suggestionId: suggestionId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 08:29:18 -05:00
|
|
|
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",
|
2016-12-26 08:29:18 -05:00
|
|
|
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 {
|
2016-12-26 08:29:18 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-26 08:29:18 -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",
|
2016-12-26 08:29:18 -05:00
|
|
|
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);
|
2016-12-26 08:29:18 -05:00
|
|
|
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 {
|
2016-12-28 09:25:20 -05:00
|
|
|
dispatch(popularSuggestionsFetched(data.popularsuggestions.map(function(json) {
|
|
|
|
var a = new PopularSuggestion();
|
2016-12-26 08:29:18 -05:00
|
|
|
a.fromJSON(json);
|
|
|
|
return a;
|
|
|
|
})));
|
2016-12-23 20:38:59 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function(jqXHR, status, error) {
|
|
|
|
ErrorActions.ajaxError(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-01-01 08:10:58 -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()) {
|
|
|
|
ErrorActions.serverError(e);
|
|
|
|
} else {
|
|
|
|
dispatch(suggestionRemoved(suggestion.SuggestionId));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function(jqXHR, status, error) {
|
|
|
|
ErrorActions.ajaxError(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-23 20:38:59 -05:00
|
|
|
module.exports = {
|
|
|
|
fetchAll: fetchAll,
|
2016-12-26 08:29:18 -05:00
|
|
|
create: create,
|
2017-01-01 08:10:58 -05:00
|
|
|
remove: remove,
|
2016-12-26 08:29:18 -05:00
|
|
|
fetchPopular: fetchPopular
|
2016-12-23 20:38:59 -05:00
|
|
|
};
|