Découpage des sagas par domaine
This commit is contained in:
parent
4d4a4a8744
commit
dbff21db9d
@ -3,15 +3,11 @@ import { logout } from '../actions/auth.actions';
|
||||
import { connect } from 'react-redux';
|
||||
import { ConnectedPage as Page } from './page';
|
||||
|
||||
export function LogoutPage({ dispatch, isLoggedIn, history }) {
|
||||
export function LogoutPage({ dispatch }) {
|
||||
useEffect(() => {
|
||||
dispatch(logout());
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isLoggedIn) history.push("/login");
|
||||
}, [isLoggedIn]);
|
||||
|
||||
return (
|
||||
<Page title="Déconnexion">
|
||||
<div className="message is-info">
|
||||
@ -23,8 +19,4 @@ export function LogoutPage({ dispatch, isLoggedIn, history }) {
|
||||
)
|
||||
}
|
||||
|
||||
function mapStateToProps({ session }) {
|
||||
return { isLoggedIn: session.isLoggedIn };
|
||||
}
|
||||
|
||||
export const ConnectedLogoutPage = connect(mapStateToProps)(LogoutPage);
|
||||
export const ConnectedLogoutPage = connect()(LogoutPage);
|
@ -1,54 +0,0 @@
|
||||
import { call, put } from 'redux-saga/effects';
|
||||
import { loginFailure, loginSuccess, logoutFailure, logoutSuccess } from '../actions/auth.actions';
|
||||
import { addMessage } from '../actions/message.actions';
|
||||
import { APIClient } from '../services/api-client.service.js';
|
||||
import { UnauthorizedError } from '../errors/unauthorized.error.js';
|
||||
|
||||
|
||||
export function* loginSaga(action) {
|
||||
const client = new APIClient();
|
||||
let result;
|
||||
try {
|
||||
result = yield call(client.login, action.username, action.password);
|
||||
} catch(err) {
|
||||
if (err instanceof UnauthorizedError) {
|
||||
yield put(addMessage('warning', "Identifiants invalides."));
|
||||
return;
|
||||
}
|
||||
|
||||
yield put(loginFailure(action.username, err));
|
||||
yield put(addMessage('danger', "Une erreur inconnue bloque le fonctionnement normal de l'application. Veuillez réessayer plus tard."));
|
||||
return
|
||||
}
|
||||
|
||||
if ('error' in result) {
|
||||
yield put(loginFailure(action.username, result.error));
|
||||
const message = result.error.message ? result.error.message : result.error.toString();
|
||||
yield put(addMessage('danger', message));
|
||||
return
|
||||
}
|
||||
|
||||
yield put(loginSuccess(action.username));
|
||||
}
|
||||
|
||||
|
||||
export function* logoutSaga(action) {
|
||||
const client = new APIClient();
|
||||
let result;
|
||||
try {
|
||||
result = yield call(client.logout);
|
||||
} catch(err) {
|
||||
yield put(logoutFailure(err));
|
||||
yield put(addMessage('danger', "Une erreur inconnue bloque le fonctionnement normal de l'application. Veuillez réessayer plus tard."));
|
||||
return;
|
||||
}
|
||||
|
||||
if ('error' in result) {
|
||||
yield put(logoutFailure(action.username, result.error));
|
||||
const message = result.error.message ? result.error.message : result.error.toString();
|
||||
yield put(addMessage('danger', message));
|
||||
return
|
||||
}
|
||||
|
||||
yield put(logoutSuccess(action.username));
|
||||
}
|
31
frontend/src/sagas/auth/login.saga.js
Normal file
31
frontend/src/sagas/auth/login.saga.js
Normal file
@ -0,0 +1,31 @@
|
||||
import { call, put } from 'redux-saga/effects';
|
||||
import { loginFailure, loginSuccess } from '../../actions/auth.actions';
|
||||
import { addMessage } from '../../actions/message.actions';
|
||||
import { APIClient } from '../../services/api-client.service.js';
|
||||
import { UnauthorizedError } from '../../errors/unauthorized.error.js';
|
||||
|
||||
export function* loginSaga(action) {
|
||||
const client = new APIClient();
|
||||
let result;
|
||||
try {
|
||||
result = yield call(client.login, action.username, action.password);
|
||||
} catch(err) {
|
||||
if (err instanceof UnauthorizedError) {
|
||||
yield put(addMessage('warning', "Identifiants invalides."));
|
||||
return;
|
||||
}
|
||||
|
||||
yield put(loginFailure(action.username, err));
|
||||
yield put(addMessage('danger', "Une erreur inconnue bloque le fonctionnement normal de l'application. Veuillez réessayer plus tard."));
|
||||
return
|
||||
}
|
||||
|
||||
if ('error' in result) {
|
||||
yield put(loginFailure(action.username, result.error));
|
||||
const message = result.error.message ? result.error.message : result.error.toString();
|
||||
yield put(addMessage('danger', message));
|
||||
return
|
||||
}
|
||||
|
||||
yield put(loginSuccess(action.username));
|
||||
}
|
8
frontend/src/sagas/auth/logout-redirect.saga.js
Normal file
8
frontend/src/sagas/auth/logout-redirect.saga.js
Normal file
@ -0,0 +1,8 @@
|
||||
import { history } from "../../util/history";
|
||||
import { addMessage } from '../../actions/message.actions';
|
||||
import { put } from 'redux-saga/effects';
|
||||
|
||||
export function* logoutRedirectSaga() {
|
||||
yield put(addMessage("success", "Vous êtes déconnecté."));
|
||||
history.push('/login');
|
||||
}
|
25
frontend/src/sagas/auth/logout.saga.js
Normal file
25
frontend/src/sagas/auth/logout.saga.js
Normal file
@ -0,0 +1,25 @@
|
||||
import { APIClient } from "../../services/api-client.service";
|
||||
import { logoutFailure, logoutSuccess } from "../../actions/auth.actions";
|
||||
import { addMessage } from "../../actions/message.actions";
|
||||
import { call, put } from 'redux-saga/effects';
|
||||
|
||||
export function* logoutSaga() {
|
||||
const client = new APIClient();
|
||||
let result;
|
||||
try {
|
||||
result = yield call(client.logout);
|
||||
} catch(err) {
|
||||
yield put(logoutFailure(err));
|
||||
yield put(addMessage('danger', "Une erreur inconnue bloque le fonctionnement normal de l'application. Veuillez réessayer plus tard."));
|
||||
return;
|
||||
}
|
||||
|
||||
if ('error' in result) {
|
||||
yield put(logoutFailure(result.error));
|
||||
const message = result.error.message ? result.error.message : result.error.toString();
|
||||
yield put(addMessage('danger', message));
|
||||
return
|
||||
}
|
||||
|
||||
yield put(logoutSuccess());
|
||||
}
|
14
frontend/src/sagas/auth/root.saga.js
Normal file
14
frontend/src/sagas/auth/root.saga.js
Normal file
@ -0,0 +1,14 @@
|
||||
import { LOGIN_REQUEST, LOGOUT_REQUEST, LOGOUT_SUCCESS } from "../../actions/auth.actions";
|
||||
import { loginSaga } from "./login.saga";
|
||||
import { logoutSaga } from "./logout.saga";
|
||||
import { logoutRedirectSaga } from "./logout-redirect.saga";
|
||||
import { all, takeLatest } from 'redux-saga/effects';
|
||||
|
||||
export function * rootSaga() {
|
||||
yield all([
|
||||
takeLatest(LOGIN_REQUEST, loginSaga),
|
||||
takeLatest(LOGOUT_REQUEST, logoutSaga),
|
||||
// Redirect after logout success
|
||||
takeLatest(LOGOUT_SUCCESS, logoutRedirectSaga),
|
||||
]);
|
||||
}
|
@ -1,10 +1,9 @@
|
||||
import { put } from 'redux-saga/effects';
|
||||
import { UnauthorizedError } from '../errors/unauthorized.error.js';
|
||||
import { addMessage } from '../actions/message.actions.js';
|
||||
import { logout } from '../actions/auth.actions.js';
|
||||
import { UnauthorizedError } from '../../errors/unauthorized.error.js';
|
||||
import { addMessage } from '../../actions/message.actions.js';
|
||||
import { logout } from '../../actions/auth.actions.js';
|
||||
|
||||
export function* failureActionSaga({ error }) {
|
||||
console.error(error);
|
||||
if (error instanceof UnauthorizedError) {
|
||||
yield put(addMessage('danger', 'Vous avez été déconnecté.'));
|
||||
yield put(logout());
|
8
frontend/src/sagas/failure/root.saga.js
Normal file
8
frontend/src/sagas/failure/root.saga.js
Normal file
@ -0,0 +1,8 @@
|
||||
import { failureActionSaga } from "./failure.saga";
|
||||
import { takeLatest, all } from "redux-saga/effects";
|
||||
|
||||
export function * rootSaga(action) {
|
||||
yield all([
|
||||
takeLatest(action => /_FAILURE$/g.test(action.type), failureActionSaga),
|
||||
]);
|
||||
}
|
0
frontend/src/sagas/project/root.saga.js
Normal file
0
frontend/src/sagas/project/root.saga.js
Normal file
@ -1,16 +1,10 @@
|
||||
import { all, takeLatest } from 'redux-saga/effects';
|
||||
import { LOGIN_REQUEST, LOGOUT_REQUEST } from '../actions/auth.actions';
|
||||
import { loginSaga, logoutSaga } from './auth.sagas';
|
||||
import { PROJECT_USER_LIST, PROJECT_LIST } from '../actions/project';
|
||||
import { projectUserListSaga, projectListSaga } from './project';
|
||||
import { failureActionSaga } from './failure.sagas';
|
||||
import { all } from 'redux-saga/effects';
|
||||
import { rootSaga as authRootSaga } from './auth/root.saga';
|
||||
import { rootSaga as failureRootSaga } from './failure/root.saga';
|
||||
|
||||
export default function* rootSaga() {
|
||||
yield all([
|
||||
takeLatest(LOGIN_REQUEST, loginSaga),
|
||||
takeLatest(LOGOUT_REQUEST, logoutSaga),
|
||||
takeLatest(PROJECT_USER_LIST, projectUserListSaga),
|
||||
takeLatest(PROJECT_LIST, projectListSaga),
|
||||
takeLatest(action => /_FAILURE$/g.test(action.type), failureActionSaga),
|
||||
authRootSaga(),
|
||||
failureRootSaga(),
|
||||
]);
|
||||
}
|
||||
|
@ -1,3 +1,3 @@
|
||||
import { createBrowserHistory } from 'history';
|
||||
import { createHashHistory } from 'history';
|
||||
|
||||
export const history = createBrowserHistory();
|
||||
export const history = createHashHistory();
|
||||
|
Loading…
Reference in New Issue
Block a user