1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-11-17 03:50:05 -05:00
moneygo/js/actions/SecurityActions.js

183 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-10-05 13:36:47 -04:00
var SecurityConstants = require('../constants/SecurityConstants');
var ErrorActions = require('./ErrorActions');
var models = require('../models.js');
var Security = models.Security;
var Error = models.Error;
function fetchSecurities() {
return {
type: SecurityConstants.FETCH_SECURITIES
}
}
function securitiesFetched(securities) {
return {
type: SecurityConstants.SECURITIES_FETCHED,
securities: securities
}
}
function createSecurity() {
return {
type: SecurityConstants.CREATE_SECURITY
}
}
function securityCreated(security) {
return {
type: SecurityConstants.SECURITY_CREATED,
security: security
}
}
function updateSecurity() {
return {
type: SecurityConstants.UPDATE_SECURITY
}
}
function securityUpdated(security) {
return {
type: SecurityConstants.SECURITY_UPDATED,
security: security
}
}
function removeSecurity() {
return {
type: SecurityConstants.REMOVE_SECURITY
}
}
function securityRemoved(securityId) {
return {
type: SecurityConstants.SECURITY_REMOVED,
securityId: securityId
}
}
function securitySelected(securityId) {
return {
type: SecurityConstants.SECURITY_SELECTED,
securityId: securityId
}
}
2016-10-05 13:36:47 -04:00
function fetchAll() {
return function (dispatch) {
dispatch(fetchSecurities());
$.ajax({
type: "GET",
dataType: "json",
url: "v1/securities/",
2016-10-05 13:36:47 -04:00
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
2017-01-03 05:32:19 -05:00
dispatch(ErrorActions.serverError(e));
2016-10-05 13:36:47 -04:00
} else {
dispatch(securitiesFetched(data.securities.map(function(json) {
var s = new Security();
s.fromJSON(json);
return s;
})));
}
},
error: function(jqXHR, status, error) {
2017-01-09 21:22:13 -05:00
dispatch(ErrorActions.ajaxError(error));
2016-10-05 13:36:47 -04:00
}
});
};
}
function create(security) {
return function (dispatch) {
dispatch(createSecurity());
$.ajax({
type: "POST",
2017-11-13 20:49:26 -05:00
contentType: "application/json",
dataType: "json",
url: "v1/securities/",
data: security.toJSON(),
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
2017-01-03 05:32:19 -05:00
dispatch(ErrorActions.serverError(e));
} else {
var s = new Security();
s.fromJSON(data);
dispatch(securityCreated(s));
}
},
error: function(jqXHR, status, error) {
2017-01-09 21:22:13 -05:00
dispatch(ErrorActions.ajaxError(error));
}
});
};
}
function update(security) {
return function (dispatch) {
dispatch(updateSecurity());
$.ajax({
type: "PUT",
2017-11-13 20:49:26 -05:00
contentType: "application/json",
dataType: "json",
url: "v1/securities/"+security.SecurityId+"/",
data: security.toJSON(),
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
2017-01-03 05:32:19 -05:00
dispatch(ErrorActions.serverError(e));
} else {
var s = new Security();
s.fromJSON(data);
dispatch(securityUpdated(s));
}
},
error: function(jqXHR, status, error) {
2017-01-09 21:22:13 -05:00
dispatch(ErrorActions.ajaxError(error));
}
});
};
}
function remove(security) {
return function(dispatch) {
dispatch(removeSecurity());
$.ajax({
type: "DELETE",
dataType: "json",
url: "v1/securities/"+security.SecurityId+"/",
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
2017-01-03 05:32:19 -05:00
dispatch(ErrorActions.serverError(e));
} else {
dispatch(securityRemoved(security.SecurityId));
}
},
error: function(jqXHR, status, error) {
2017-01-09 21:22:13 -05:00
dispatch(ErrorActions.ajaxError(error));
}
});
};
}
2016-10-05 13:36:47 -04:00
module.exports = {
fetchAll: fetchAll,
create: create,
update: update,
remove: remove,
select: securitySelected
2016-10-05 13:36:47 -04:00
};