mirror of
https://github.com/aclindsa/moneygo.git
synced 2025-06-13 13:39:23 -04:00
Initial plumbing for moving to Redux
This commit is contained in:
181
js/actions/AccountActions.js
Normal file
181
js/actions/AccountActions.js
Normal file
@ -0,0 +1,181 @@
|
||||
var AccountConstants = require('../constants/AccountConstants');
|
||||
|
||||
var ErrorActions = require('ErrorActions');
|
||||
|
||||
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: "account/",
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
var accounts = [];
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
ErrorActions.serverError(e);
|
||||
} else {
|
||||
dispatch(accountsFetched(data.accounts.map(function(json) {
|
||||
var a = new Account();
|
||||
a.fromJSON(json);
|
||||
return a;
|
||||
})));
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, status, error) {
|
||||
ErrorActions.ajaxError(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function create(account) {
|
||||
return function (dispatch) {
|
||||
dispatch(createAccount());
|
||||
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
url: "account/",
|
||||
data: {account: account.toJSON()},
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
ErrorActions.serverError(e);
|
||||
} else {
|
||||
var a = new Account();
|
||||
a.fromJSON(data);
|
||||
dispatch(accountCreated(a));
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, status, error) {
|
||||
ErrorActions.ajaxError(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function update(account) {
|
||||
return function (dispatch) {
|
||||
dispatch(updateAccount());
|
||||
|
||||
$.ajax({
|
||||
type: "PUT",
|
||||
dataType: "json",
|
||||
url: "account/"+account.AccountId+"/",
|
||||
data: {account: account.toJSON()},
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
ErrorActions.serverError(e);
|
||||
} else {
|
||||
var a = new Account();
|
||||
a.fromJSON(data);
|
||||
dispatch(accountUpdated(a));
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, status, error) {
|
||||
ErrorActions.ajaxError(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function remove(account) {
|
||||
return function(dispatch) {
|
||||
dispatch(removeAccount());
|
||||
|
||||
$.ajax({
|
||||
type: "DELETE",
|
||||
dataType: "json",
|
||||
url: "account/"+account.AccountId+"/",
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
ErrorActions.serverError(e);
|
||||
} else {
|
||||
dispatch(accountRemoved(account.AccountId));
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, status, error) {
|
||||
ErrorActions.ajaxError(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fetchAll: fetchAll,
|
||||
create: create,
|
||||
update: update,
|
||||
remove: remove,
|
||||
select: accountSelected
|
||||
};
|
27
js/actions/ErrorActions.js
Normal file
27
js/actions/ErrorActions.js
Normal file
@ -0,0 +1,27 @@
|
||||
var ErrorConstants = require('../constants/ErrorConstants');
|
||||
|
||||
var models = require('../models.js');
|
||||
var Error = models.Error;
|
||||
|
||||
function serverError(error) {
|
||||
return {
|
||||
type: ErrorConstants.ERROR_SERVER,
|
||||
error: error
|
||||
};
|
||||
}
|
||||
|
||||
function ajaxError(error) {
|
||||
var e = new Error();
|
||||
e.ErrorId = 5;
|
||||
e.ErrorString = "Request Failed: " + status + error;
|
||||
|
||||
return {
|
||||
type: ErrorConstants.ERROR_AJAX,
|
||||
error: e
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
serverError: serverError,
|
||||
ajaxError: ajaxError
|
||||
};
|
Reference in New Issue
Block a user