1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-10-30 15:50:04 -04:00
moneygo/js/actions/UserActions.js

250 lines
4.7 KiB
JavaScript
Raw Normal View History

2016-10-05 13:36:47 -04:00
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 createUser(user) {
return {
type: UserConstants.CREATE_USER,
user: user
}
}
function userCreated(user) {
return {
type: UserConstants.USER_CREATED,
user: user
}
}
2016-10-05 13:36:47 -04:00
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()) {
2017-01-03 05:32:19 -05:00
dispatch(ErrorActions.serverError(e));
2016-10-05 13:36:47 -04:00
} else {
var u = new User();
u.fromJSON(data);
dispatch(userFetched(u));
}
},
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 initializeSession(dispatch, session) {
dispatch(userLoggedIn(session));
dispatch(fetch(session.UserId));
dispatch(AccountActions.fetchAll());
dispatch(SecurityActions.fetchAll());
}
function create(user) {
return function(dispatch) {
dispatch(createUser(user));
$.ajax({
type: "POST",
dataType: "json",
url: "user/",
data: {user: user.toJSON()},
success: function(data, status, jqXHR) {
var e = new Error();
e.fromJSON(data);
if (e.isError()) {
dispatch(ErrorActions.serverError(e));
} else {
var u = new User();
u.fromJSON(data);
dispatch(userCreated(u));
}
},
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 login(user) {
return function(dispatch) {
2016-10-05 13:36:47 -04:00
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()) {
2017-01-03 05:32:19 -05:00
dispatch(ErrorActions.serverError(e));
2016-10-05 13:36:47 -04:00
} else {
var s = new Session();
s.fromJSON(data);
initializeSession(dispatch, 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 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*/)
2017-01-03 05:32:19 -05:00
dispatch(ErrorActions.serverError(e));
2016-10-05 13:36:47 -04:00
} else {
var s = new Session();
s.fromJSON(data);
dispatch(loginUser());
initializeSession(dispatch, 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 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()) {
2017-01-03 05:32:19 -05:00
dispatch(ErrorActions.serverError(e));
2016-10-05 13:36:47 -04:00
} else {
dispatch(userLoggedOut());
}
},
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 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()) {
2017-01-03 05:32:19 -05:00
dispatch(ErrorActions.serverError(e));
2016-10-05 13:36:47 -04:00
} else {
var u = new User();
u.fromJSON(data);
dispatch(userUpdated(u));
}
},
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 = {
create: create,
2016-10-05 13:36:47 -04:00
fetch: fetch,
login: login,
logout: logout,
update: update,
tryResumingSession: tryResumingSession
};