diff --git a/src/actions/AppActions.js b/src/actions/AppActions.js
index 852d2d30c..2178a0519 100644
--- a/src/actions/AppActions.js
+++ b/src/actions/AppActions.js
@@ -1,40 +1,32 @@
import * as types from '../constants/ActionTypes'
import axios from 'axios';
import {replace} from 'react-router-redux';
-import SockJs from 'sockjs-client';
-import Stomp from 'stompjs/lib/stomp.min.js';
-export function loginSuccess() {
+export function loginSuccess(auth) {
return dispatch => {
localStorage.setItem('isLogged', true);
dispatch(getNotificationsEndpoint()).then(topic => {
- let sock = new SockJs(config.WS_URL);
- let client = Stomp.Stomp.over(sock);
- client.debug = null;
-
- client.connect({}, () => {
- client.subscribe(topic.data, msg => {
- const notification = JSON.parse(msg.body);
-
- if(notification.eventType === 'Read'){
- dispatch(updateNotification(
- notification.notification, notification.unreadCount
- ));
- }else if(notification.eventType === 'New'){
- dispatch(newNotification(
- notification.notification, notification.unreadCount
- ));
- const notif = notification.notification;
- if(notif.important){
- dispatch(addNotification(
- 'Important notification', notif.message, 5000,
- 'primary'
- ))
- }
+ auth.initNotificationClient(topic, msg => {
+ const notification = JSON.parse(msg.body);
+
+ if(notification.eventType === 'Read'){
+ dispatch(updateNotification(
+ notification.notification, notification.unreadCount
+ ));
+ }else if(notification.eventType === 'New'){
+ dispatch(newNotification(
+ notification.notification, notification.unreadCount
+ ));
+ const notif = notification.notification;
+ if(notif.important){
+ dispatch(addNotification(
+ 'Important notification', notif.message, 5000,
+ 'primary'
+ ))
}
- });
- })
+ }
+ });
})
dispatch(getNotifications()).then(response => {
@@ -46,8 +38,9 @@ export function loginSuccess() {
}
}
-export function logoutSuccess() {
+export function logoutSuccess(auth) {
return () => {
+ auth.closeNotificationClient();
localStorage.removeItem('isLogged');
}
}
diff --git a/src/components/app/LoginForm.js b/src/components/app/LoginForm.js
index 311b0677a..9915aab8e 100644
--- a/src/components/app/LoginForm.js
+++ b/src/components/app/LoginForm.js
@@ -76,7 +76,7 @@ class LoginForm extends Component {
}
handleLogin = () => {
- const {dispatch} = this.props;
+ const {dispatch, auth} = this.props;
const {roleSelect, role} = this.state;
this.setState({
@@ -85,7 +85,7 @@ class LoginForm extends Component {
if(roleSelect){
return dispatch(loginCompletionRequest(role))
.then(() => {
- dispatch(loginSuccess());
+ dispatch(loginSuccess(auth));
this.handleSuccess();
})
}
diff --git a/src/containers/App.js b/src/containers/App.js
index a8b6fba33..3d650edb4 100644
--- a/src/containers/App.js
+++ b/src/containers/App.js
@@ -8,6 +8,8 @@ import { getRoutes } from '../routes.js';
import { syncHistoryWithStore, push } from 'react-router-redux';
import { Router, browserHistory } from 'react-router';
+import Auth from '../services/Auth';
+
import Moment from 'moment';
import NotificationHandler
@@ -29,68 +31,74 @@ import '../assets/css/styles.css';
const store = configureStore(browserHistory);
const history = syncHistoryWithStore(browserHistory, store);
-axios.defaults.withCredentials = true;
-
-axios.interceptors.response.use(function (response) {
- return response;
-}, function (error) {
- if(!error.response){
- store.dispatch(noConnection(true));
- }
-
- /*
- * Authorization error
- */
- if(error.response.status == 401){
- store.dispatch(setProcessSaved());
- store.dispatch(logoutSuccess());
- store.dispatch(push('/login?redirect=true'));
- }else if(error.response.status != 404){
- if(localStorage.isLogged){
- const errorMessenger = (code) => {
- switch(code){
- case 500:
- return 'Server error';
- case 400:
- return 'Client error';
- }
- }
- const {
- data, status
- } = error.response;
+export default class App extends Component {
+ constructor() {
+ super();
- const errorTitle = errorMessenger(status);
+ this.auth = new Auth();
- console.error(data.message);
+ axios.defaults.withCredentials = true;
- // Chart disabled notifications
- if(error.response.request.responseURL.includes('silentError=true')){
- return;
+ axios.interceptors.response.use(function (response) {
+ return response;
+ }, function (error) {
+ if(!error.response){
+ store.dispatch(noConnection(true));
}
- store.dispatch(addNotification(
- 'Error: ' + data.message.split(' ', 4).join(' ') + '...',
- data.message, 5000, 'error', errorTitle)
- );
- }
- }
- if(error.response.request.responseURL.includes('showError=true')){
- const {
- data
- } = error.response;
-
- store.dispatch(addNotification(
- 'Error: ' + data.message.split(' ', 4).join(' ') + '...',
- data.message, 5000, 'error', '')
- );
- } else {
- return Promise.reject(error);
- }
-});
+ /*
+ * Authorization error
+ */
+ if(error.response.status == 401){
+ store.dispatch(setProcessSaved());
+ store.dispatch(logoutSuccess(this.auth));
+ store.dispatch(push('/login?redirect=true'));
+ }else if(error.response.status != 404){
+ if(localStorage.isLogged){
+ const errorMessenger = (code) => {
+ switch(code){
+ case 500:
+ return 'Server error';
+ case 400:
+ return 'Client error';
+ }
+ }
+ const {
+ data, status
+ } = error.response;
+
+ const errorTitle = errorMessenger(status);
+
+ console.error(data.message);
+
+ // Chart disabled notifications
+ if(
+ error.response.request.responseURL
+ .includes('silentError=true')
+ ){
+ return;
+ }
+
+ store.dispatch(addNotification(
+ 'Error: ' + data.message.split(' ', 4).join(' ') +
+ '...', data.message, 5000, 'error', errorTitle)
+ );
+ }
+ }
-export default class App extends Component {
- constructor() {
- super();
+ if(error.response.request.responseURL.includes('showError=true')){
+ const {
+ data
+ } = error.response;
+
+ store.dispatch(addNotification(
+ 'Error: ' + data.message.split(' ', 4).join(' ') + '...',
+ data.message, 5000, 'error', '')
+ );
+ } else {
+ return Promise.reject(error);
+ }
+ }.bind(this));
store.dispatch(getAvailableLang()).then(response => {
const {defaultValue, values} = response.data;
@@ -110,7 +118,7 @@ export default class App extends Component {