1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-11-01 00:10:06 -04:00
moneygo/js/actions/AccountActions.js

183 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-10-03 19:49:15 -04:00
var AccountConstants = require('../constants/AccountConstants');
2016-10-05 13:36:47 -04:00
var ErrorActions = require('./ErrorActions');
2016-10-03 19:49:15 -04:00
var models = require('../models.js');
var Account = models.Account;
var Error = models.Error;
function fetchAccounts() {
return {
type: AccountConstants.FETCH_ACCOUNTS
}
}
function accountsFetched(accounts) {
return {
type: AccountConstants.ACCOUNTS_FETCHED,
accounts: accounts
}
}
function createAccount() {
return {
type: AccountConstants.CREATE_ACCOUNT
}
}
function accountCreated(account) {
return {
type: AccountConstants.ACCOUNT_CREATED,
account: account
}
}
function updateAccount() {
return {
type: AccountConstants.UPDATE_ACCOUNT
}
}
function accountUpdated(account) {
return {
type: AccountConstants.ACCOUNT_UPDATED,
account: account
}
}
function removeAccount() {
return {
type: AccountConstants.REMOVE_ACCOUNT
}
}
function accountRemoved(accountId) {
return {
type: AccountConstants.ACCOUNT_REMOVED,
accountId: accountId
}
}
function accountSelected(accountId) {
return {
type: AccountConstants.ACCOUNT_SELECTED,
accountId: accountId
}
}
function fetchAll() {
return function (dispatch) {
dispatch(fetchAccounts());
$.ajax({
type: "GET",
dataType: "json",
url: "v1/accounts/",
2016-10-03 19:49:15 -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-03 19:49:15 -04:00
} else {
dispatch(accountsFetched(data.accounts.map(function(json) {
var a = new Account();
a.fromJSON(json);
return a;
})));
}
},
error: function(jqXHR, status, error) {
2017-01-09 21:22:13 -05:00
dispatch(ErrorActions.ajaxError(error));
2016-10-03 19:49:15 -04:00
}
});
};
}
function create(account) {
return function (dispatch) {
dispatch(createAccount());
$.ajax({
type: "POST",
2017-11-13 20:49:26 -05:00
contentType: "application/json",
2016-10-03 19:49:15 -04:00
dataType: "json",
url: "v1/accounts/",
data: account.toJSON(),
2016-10-03 19:49:15 -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-03 19:49:15 -04:00
} else {
var a = new Account();
a.fromJSON(data);
dispatch(accountCreated(a));
}
},
error: function(jqXHR, status, error) {
2017-01-09 21:22:13 -05:00
dispatch(ErrorActions.ajaxError(error));
2016-10-03 19:49:15 -04:00
}
});
};
}
function update(account) {
return function (dispatch) {
dispatch(updateAccount());
$.ajax({
type: "PUT",
2017-11-13 20:49:26 -05:00
contentType: "application/json",
2016-10-03 19:49:15 -04:00
dataType: "json",
url: "v1/accounts/"+account.AccountId+"/",
data: account.toJSON(),
2016-10-03 19:49:15 -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-03 19:49:15 -04:00
} else {
var a = new Account();
a.fromJSON(data);
dispatch(accountUpdated(a));
}
},
error: function(jqXHR, status, error) {
2017-01-09 21:22:13 -05:00
dispatch(ErrorActions.ajaxError(error));
2016-10-03 19:49:15 -04:00
}
});
};
}
function remove(account) {
return function(dispatch) {
dispatch(removeAccount());
$.ajax({
type: "DELETE",
dataType: "json",
url: "v1/accounts/"+account.AccountId+"/",
2016-10-03 19:49:15 -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-03 19:49:15 -04:00
} else {
dispatch(accountRemoved(account.AccountId));
}
},
error: function(jqXHR, status, error) {
2017-01-09 21:22:13 -05:00
dispatch(ErrorActions.ajaxError(error));
2016-10-03 19:49:15 -04:00
}
});
};
}
module.exports = {
fetchAll: fetchAll,
create: create,
update: update,
remove: remove,
select: accountSelected
};