Commit 8286bc05 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov

code refactoring and cleanup

parent 1686c8ef
......@@ -2,7 +2,7 @@ import * as cookies from 'es-cookie';
import * as jwt from 'jsonwebtoken';
import { call, put, select, takeEvery } from 'redux-saga/effects';
// actions
import { loginUser } from '@/user/action/public';
import { loginUserSuccess } from '@/user/action/public';
// constants
import { LOGIN_APP, LOGIN_APP_SAGA } from '@/core/constants/login';
import { ROLE_CLIENT } from '@/core/constants/userRoles';
......@@ -14,7 +14,7 @@ import { log } from '@/core/utilities/debug';
import { configureBackendApi } from '@/core/utilities/requestUtils';
export const loginSagas = [
takeEvery(LOGIN_APP_SAGA, loginAppAction),
takeEvery(LOGIN_APP_SAGA, loginAppSaga),
];
export function checkAccessTokens(dispatch, getState) {
......@@ -38,7 +38,7 @@ export function checkAccessTokens(dispatch, getState) {
if (cookieToken) {
try {
const parsedTokenData = jwt.decode(cookieToken);
dispatch(loginUser({ access_token: cookieToken, ...parsedTokenData }));
dispatch(loginUserSuccess({ access_token: cookieToken, ...parsedTokenData }));
return Promise.resolve();
} catch (e) {
console.log('Error while parsing token: ', e.message);
......@@ -50,11 +50,13 @@ export function checkAccessTokens(dispatch, getState) {
return applicationLogin();
}
}
const loginAppRequest = () => (dispatch) => ({
// #loginApp
const loginAppAction = () => (dispatch) => ({
type: LOGIN_APP_SAGA,
});
function* loginAppAction() {
function* loginAppSaga() {
const clientId = yield select((state) => state.applicationConfig.clientId);
const appToken = yield call(LoginService.loginApp, clientId);
......@@ -79,7 +81,7 @@ const refreshToken = () => (dispatch, getState) => {
return LoginService.refreshAccessToken(refreshToken)
.then((accessToken) => {
// console.log('Refreshed access_token', accessToken);
dispatch(loginUser({ ...accessToken }));
dispatch(loginUserSuccess({ ...accessToken }));
// console.log('Setting access_token expire in ', timeout, 'ms');
saveCookies({ ...accessToken, ...jwt.decode(accessToken.access_token) }, accessToken.exp * 1000 || new Date().getTime() + accessToken.expires_in * 1000, getState().applicationConfig.apiUrl);
......@@ -104,5 +106,5 @@ const refreshAuthTokenAuto = () => (dispatch, getState) => {
};
export {
loginAppRequest, loginApp, refreshToken, refreshAuthTokenAuto,
loginAppAction, loginApp, refreshToken, refreshAuthTokenAuto,
};
......@@ -3,9 +3,12 @@ import { call, put, select, takeEvery } from 'redux-saga/effects';
// actions
import navigateTo from '@/core/action/navigation';
import { loginAppRequest } from '@/core/action/login';
import { loginAppAction } from '@/core/action/login';
// Constants
import { RECEIVE_USER, RECEIVE_USERS, LOGIN_USER, API_LOGIN_USER, API_LOGOUT_USER } from '@/user/constants';
import {
RECEIVE_USER, RECEIVE_USERS, LOGIN_USER,
SAGA_RECEIVE_USER, SAGA_RECEIVE_USERS, SAGA_LOGIN_USER, SAGA_LOGOUT_USER,
} from '@/user/constants';
// Model
import User from '@/core/model/user/User';
// Service
......@@ -16,12 +19,15 @@ import { clearCookies, saveCookies } from '@/core/utilities';
export const userPublicSagas = [
takeEvery(API_LOGIN_USER, loginAction),
takeEvery(API_LOGOUT_USER, logoutAction),
takeEvery(SAGA_LOGIN_USER, loginUserSaga),
takeEvery(SAGA_LOGOUT_USER, logoutUserSaga),
takeEvery(SAGA_RECEIVE_USER, getUserSaga),
takeEvery(SAGA_RECEIVE_USERS, listUsersSaga),
];
export const loginUserRequest = (username, password, needRedirect = false) => ({
type: API_LOGIN_USER,
// #loginUser
export const loginUserAction = (username, password, needRedirect = false) => ({
type: SAGA_LOGIN_USER,
payload: {
username,
password,
......@@ -29,14 +35,8 @@ export const loginUserRequest = (username, password, needRedirect = false) => ({
},
});
export const logoutUserRequest = () => ({
type: API_LOGOUT_USER,
});
function* loginAction(action) {
function* loginUserSaga(action) {
const { username, password, needRedirect } = action.payload;
// log('Trying login', username);
try {
yield call(loginRequest, username, password);
......@@ -60,44 +60,65 @@ function* loginRequest(username, password) {
userData.exp * 1000 || new Date().getTime() + userData.expires_in * 1000,
apiUrl,
);
return put(loginUser({ ...userData, ...tokenData }));
return put(loginUserSuccess({ ...userData, ...tokenData }));
}
function* logoutAction() {
export const loginUserSuccess = (payload) => ({
type: LOGIN_USER,
...payload,
});
// #logoutUser
export const logoutUserAction = () => ({
type: SAGA_LOGOUT_USER,
});
function* logoutUserSaga() {
const token = yield select((state) => state.login.access_token);
yield call(LoginService.logout, token);
clearCookies();
const appTokenData = yield call(loginAppRequest);
const appTokenData = yield call(loginAppAction);
const apiUrl = yield select((state) => state.applicationConfig.apiUrl);
saveCookies(appTokenData, appTokenData.exp * 1000 || new Date().getTime() + appTokenData.expires_in * 1000, apiUrl);
window.location.replace('/');
}
const receiveUserAction = (user: User) => ({
// #getUser
export const getUserAction = (uuid) => ({
type: SAGA_RECEIVE_USER,
payload: {
uuid,
},
});
function* getUserSaga(action) {
const { uuid } = action.payload;
const user = yield call(UserService.getUser, uuid);
yield put(receiveUserSuccess(user));
}
const receiveUserSuccess = (user: User) => ({
type: RECEIVE_USER,
payload: { user },
});
const receiveUsersAction = (users: User[]) => ({
// #listUsers
export const listUsersAction = (uuid) => ({
type: SAGA_RECEIVE_USERS,
payload: {
uuid,
},
});
function * listUsersSaga() {
const users = yield call(UserService.listUsers);
yield put(receiveUsersSuccess(users));
}
const receiveUsersSuccess = (users: User[]) => ({
type: RECEIVE_USERS,
payload: { users },
});
export const getUser = (uuid: string) => (dispatch) => {
return UserService.getUser(uuid)
.then((user) => dispatch(receiveUserAction(user)));
};
export const listUsers = () => (dispatch) => {
return UserService.listUsers()
.then((users) => dispatch(receiveUsersAction(users)));
};
export const loginUser = (payload) => {
return {
type: LOGIN_USER,
...payload,
};
};
......@@ -6,7 +6,7 @@ export const RECEIVE_USERS = 'user/public/RECEIVE_USERS';
export const LOGIN_USER = 'user/public/LOGIN_USER';
export const API_RECEIVE_USER = 'api/user/public/RECEIVE_USER';
export const API_RECEIVE_USERS = 'api/user/public/RECEIVE_USERS';
export const API_LOGIN_USER = 'api/user/public/LOGIN_USER';
export const API_LOGOUT_USER = 'api/user/public/LOGOUT_USER';
export const SAGA_RECEIVE_USER = 'saga/user/public/RECEIVE_USER';
export const SAGA_RECEIVE_USERS = 'saga/user/public/RECEIVE_USERS';
export const SAGA_LOGIN_USER = 'saga/user/public/LOGIN_USER';
export const SAGA_LOGOUT_USER = 'saga/user/public/LOGOUT_USER';
......@@ -3,15 +3,15 @@ import { bindActionCreators, compose } from 'redux';
import { connect } from 'react-redux';
import { WithTranslation, withTranslation } from 'react-i18next';
// action
import { loginUserRequest, logoutUserRequest } from '@/user/action/public';
import { loginUserAction, logoutUserAction } from '@/user/action/public';
// ui
import LoginForm from '@/user/ui/c/form/LoginForm';
interface ILoginPageProps extends React.ClassAttributes<any>, WithTranslation {
login: any;
loginUserRequest: (username, password, needRedirect?) => void;
logoutUserRequest: () => void;
loginUserAction: (username, password, needRedirect?) => void;
logoutUserAction: () => void;
getUser: (uuid) => void;
uuid: string;
}
......@@ -19,13 +19,13 @@ interface ILoginPageProps extends React.ClassAttributes<any>, WithTranslation {
class UserDetailsPage extends React.Component<ILoginPageProps> {
protected handleLogin = (value) => {
const { loginUserRequest } = this.props;
loginUserRequest(value.username, value.password, false);
const { loginUserAction } = this.props;
loginUserAction(value.username, value.password, false);
};
protected logout = () => {
const { logoutUserRequest } = this.props;
logoutUserRequest()
const { logoutUserAction } = this.props;
logoutUserAction()
}
public render() {
......@@ -51,8 +51,8 @@ const mapStateToProps = (state) => ({
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
loginUserRequest,
logoutUserRequest,
loginUserAction,
logoutUserAction,
}, dispatch);
export default compose<ILoginPageProps>(
......
......@@ -3,22 +3,22 @@ import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { Link } from 'react-router-dom';
// Action
import { listUsers } from '@/user/action/public';
import { listUsersAction } from '@/user/action/public';
// Model
import User from '@/core/model/user/User';
interface IBrowsePageProps extends React.ClassAttributes<any> {
listUsers: () => void;
listUsersAction: () => void;
users: User[];
}
class UserBrowsePage extends React.Component<IBrowsePageProps> {
public componentWillMount(): void {
const { users, listUsers } = this.props;
const { users, listUsersAction } = this.props;
if (!users || users.length === 0) {
listUsers();
listUsersAction();
}
}
......@@ -40,11 +40,11 @@ class UserBrowsePage extends React.Component<IBrowsePageProps> {
const mapStateToProps = (state, ownProps) => ({
users: state.user.users,
users: state.user.public.users,
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
listUsers,
listUsersAction,
}, dispatch);
......
......@@ -3,24 +3,24 @@ import { bindActionCreators, compose } from 'redux';
import { connect } from 'react-redux';
import { WithTranslation, withTranslation } from 'react-i18next';
// Action
import { getUser } from '@/user/action/public';
import { getUserAction } from '@/user/action/public';
// Model
import User from '@/core/model/user/User';
interface IUserDetailsPage extends React.ClassAttributes<any>, WithTranslation {
user: User;
getUser: (uuid) => void;
getUserAction: (uuid) => void;
uuid: string;
}
class UserDetailsPage extends React.Component<IUserDetailsPage> {
public componentWillMount(): void {
const { uuid, user, getUser } = this.props;
const { uuid, user, getUserAction } = this.props;
if (!user || user.uuid !== uuid) {
getUser(uuid);
getUserAction(uuid);
}
}
......@@ -36,12 +36,12 @@ class UserDetailsPage extends React.Component<IUserDetailsPage> {
}
const mapStateToProps = (state, ownProps) => ({
user: state.user.user,
user: state.user.public.user,
uuid: ownProps.match.params.uuid,
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
getUser,
getUserAction,
}, dispatch);
......
......@@ -157,7 +157,6 @@ const prerenderer = (html, errHtml) => (req, res) => {
// res.status(500).set('Content-Type', 'text/html').send(errFilledHtml);
})
.then(() => {
console.log('-=-=-=-=-=-=-=-=-');
return sagaReady.toPromise().then(() => {
console.log('Fetched all component data');
return renderView();
......
......@@ -4,7 +4,7 @@ import * as jwt from 'jsonwebtoken';
import config from 'config';
// action
import { loginAppRequest } from '@grin-global/ui-core/core/action/login';
import { loginUser } from '@grin-global/ui-core/user/action/public';
import { loginUserSuccess } from '@grin-global/ui-core/user/action/public';
// model
import ApiError from '@grin-global/ui-core/core/model/common/ApiError';
......@@ -20,7 +20,7 @@ function checkAuthTokenRequest(req, dispatch) {
accessToken = config.access_token.access_token;
parsedToken = jwt.decode(config.access_token.access_token);
}
dispatch(loginUser({ access_token: accessToken, ...parsedToken, refresh_token: req.cookies.refresh_token }));
dispatch(loginUserSuccess({ access_token: accessToken, ...parsedToken, refresh_token: req.cookies.refresh_token }));
return Promise.resolve({ access_token: accessToken, ...parsedToken });
} catch (e) {
console.log('Error while parsing token:', e.message);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment