mirror of
https://github.com/aclindsa/moneygo.git
synced 2025-06-13 13:39:23 -04:00
Hook (almost) everything up to Redux
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
var AccountConstants = require('../constants/AccountConstants');
|
||||
|
||||
var ErrorActions = require('ErrorActions');
|
||||
var ErrorActions = require('./ErrorActions');
|
||||
|
||||
var models = require('../models.js');
|
||||
var Account = models.Account;
|
||||
@ -75,7 +75,6 @@ function fetchAll() {
|
||||
url: "account/",
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
var accounts = [];
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
ErrorActions.serverError(e);
|
||||
|
@ -21,7 +21,14 @@ function ajaxError(error) {
|
||||
};
|
||||
}
|
||||
|
||||
function clearError() {
|
||||
return {
|
||||
type: ErrorConstants.CLEAR_ERROR,
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
serverError: serverError,
|
||||
ajaxError: ajaxError
|
||||
ajaxError: ajaxError,
|
||||
clearError: clearError
|
||||
};
|
||||
|
52
js/actions/SecurityActions.js
Normal file
52
js/actions/SecurityActions.js
Normal file
@ -0,0 +1,52 @@
|
||||
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 fetchAll() {
|
||||
return function (dispatch) {
|
||||
dispatch(fetchSecurities());
|
||||
|
||||
$.ajax({
|
||||
type: "GET",
|
||||
dataType: "json",
|
||||
url: "security/",
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
ErrorActions.serverError(e);
|
||||
} else {
|
||||
dispatch(securitiesFetched(data.securities.map(function(json) {
|
||||
var s = new Security();
|
||||
s.fromJSON(json);
|
||||
return s;
|
||||
})));
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, status, error) {
|
||||
ErrorActions.ajaxError(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fetchAll: fetchAll
|
||||
};
|
208
js/actions/UserActions.js
Normal file
208
js/actions/UserActions.js
Normal file
@ -0,0 +1,208 @@
|
||||
var UserConstants = require('../constants/UserConstants');
|
||||
|
||||
var AccountActions = require('./AccountActions');
|
||||
var SecurityActions = require('./SecurityActions');
|
||||
var ErrorActions = require('./ErrorActions');
|
||||
|
||||
var models = require('../models.js');
|
||||
var User = models.User;
|
||||
var Session = models.Session;
|
||||
var Error = models.Error;
|
||||
|
||||
function loginUser() {
|
||||
return {
|
||||
type: UserConstants.LOGIN_USER
|
||||
}
|
||||
}
|
||||
|
||||
function userLoggedIn(session) {
|
||||
return {
|
||||
type: UserConstants.USER_LOGGEDIN,
|
||||
session: session
|
||||
}
|
||||
}
|
||||
|
||||
function logoutUser() {
|
||||
return {
|
||||
type: UserConstants.LOGOUT_USER
|
||||
}
|
||||
}
|
||||
|
||||
function userLoggedOut() {
|
||||
return {
|
||||
type: UserConstants.USER_LOGGEDOUT
|
||||
}
|
||||
}
|
||||
|
||||
function fetchUser(userId) {
|
||||
return {
|
||||
type: UserConstants.FETCH_USER,
|
||||
userId: userId
|
||||
}
|
||||
}
|
||||
|
||||
function userFetched(user) {
|
||||
return {
|
||||
type: UserConstants.USER_FETCHED,
|
||||
user: user
|
||||
}
|
||||
}
|
||||
|
||||
function updateUser(user) {
|
||||
return {
|
||||
type: UserConstants.UPDATE_USER,
|
||||
user: user
|
||||
}
|
||||
}
|
||||
|
||||
function userUpdated(user) {
|
||||
return {
|
||||
type: UserConstants.USER_UPDATED,
|
||||
user: user
|
||||
}
|
||||
}
|
||||
|
||||
function fetch(userId) {
|
||||
return function (dispatch) {
|
||||
dispatch(fetchUser());
|
||||
|
||||
$.ajax({
|
||||
type: "GET",
|
||||
dataType: "json",
|
||||
url: "user/"+userId+"/",
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
ErrorActions.serverError(e);
|
||||
} else {
|
||||
var u = new User();
|
||||
u.fromJSON(data);
|
||||
dispatch(userFetched(u));
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, status, error) {
|
||||
ErrorActions.ajaxError(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function initializeSession(dispatch, session) {
|
||||
dispatch(userLoggedIn(session));
|
||||
dispatch(fetch(session.UserId));
|
||||
dispatch(AccountActions.fetchAll());
|
||||
dispatch(SecurityActions.fetchAll());
|
||||
}
|
||||
|
||||
function login(user) {
|
||||
return function (dispatch) {
|
||||
dispatch(loginUser());
|
||||
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
dataType: "json",
|
||||
url: "session/",
|
||||
data: {user: user.toJSON()},
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
ErrorActions.serverError(e);
|
||||
} else {
|
||||
var s = new Session();
|
||||
s.fromJSON(data);
|
||||
initializeSession(dispatch, s);
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, status, error) {
|
||||
ErrorActions.ajaxError(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function tryResumingSession() {
|
||||
return function (dispatch) {
|
||||
$.ajax({
|
||||
type: "GET",
|
||||
dataType: "json",
|
||||
url: "session/",
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
if (e.ErrorId != 1 /* Not Signed In*/)
|
||||
ErrorActions.serverError(e);
|
||||
} else {
|
||||
var s = new Session();
|
||||
s.fromJSON(data);
|
||||
dispatch(loginUser());
|
||||
initializeSession(dispatch, s);
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, status, error) {
|
||||
ErrorActions.ajaxError(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function logout() {
|
||||
return function (dispatch) {
|
||||
dispatch(logoutUser());
|
||||
|
||||
$.ajax({
|
||||
type: "DELETE",
|
||||
dataType: "json",
|
||||
url: "session/",
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
ErrorActions.serverError(e);
|
||||
} else {
|
||||
dispatch(userLoggedOut());
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, status, error) {
|
||||
ErrorActions.ajaxError(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function update(user) {
|
||||
return function (dispatch) {
|
||||
dispatch(updateUser());
|
||||
|
||||
$.ajax({
|
||||
type: "PUT",
|
||||
dataType: "json",
|
||||
url: "user/"+user.UserId+"/",
|
||||
data: {user: user.toJSON()},
|
||||
success: function(data, status, jqXHR) {
|
||||
var e = new Error();
|
||||
e.fromJSON(data);
|
||||
if (e.isError()) {
|
||||
ErrorActions.serverError(e);
|
||||
} else {
|
||||
var u = new User();
|
||||
u.fromJSON(data);
|
||||
dispatch(userUpdated(u));
|
||||
}
|
||||
},
|
||||
error: function(jqXHR, status, error) {
|
||||
ErrorActions.ajaxError(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fetch: fetch,
|
||||
login: login,
|
||||
logout: logout,
|
||||
update: update,
|
||||
tryResumingSession: tryResumingSession
|
||||
};
|
Reference in New Issue
Block a user