mattermost-mobile/service/actions/preferences.js
Chris Duarte e17eb6c237 PLT-5709 Global error handler (#187)
* WIP Global error handler

Adding components
Adding actions

* Add general errors where errors are caught

* Filter out non displayable errors

* Initial setup to email errors

* Add current user/team to error email

* Fix a few lints

* Ensure errors are cleared when reporting a problem

* Add clear all and internationalization

* Fix trailing comma issue

* fix lints

* Use Formatted Text component

* Move error list out of channel

* Add mobile prefix to i18n id for connection error

* Set shouldRender to true in RightMenuDrawerItem.defaultProps

* Update component name for RightMenuDrawerItem

* Clean up propTypes in RightMenuDrawerItem

* Clean up styles in RightMenuDrawerItem

* Use selector to filter displayable errors

* Clarify code for constructing wrapper style in ErrorList

* Avoid indenting email body for error report

* Remove extraneous params from errors actions

* Rename action type for logging errors

* Rename action for logging errors

* Set shouldRender to true in RightMenuDrawerItem.defaultProps

* Make logged errors displayable by default

* Don't log error when websocket connection is closed

* Hide button to dismiss all errors if ShowErrorsList isn't enabled

* Display only last error in alert bar when ShowErrorsList is disabled

* Add error logging where missing in actions

* Always show button to report problem

* Only include errors in report problem email if any have been logged

* Clarify subject of report problem email

* Set ShowErrorsList to false in default config.json

* Remove unused component + translation for connection errors

* Capture entire error object not just message

* Display fallback if error.message is blank

* Log errors in login actions

* Fix construction of errors email body
2017-03-12 14:42:02 -03:00

97 lines
3.2 KiB
JavaScript

// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import {batchActions} from 'redux-batched-actions';
import Client from 'service/client';
import {Preferences, PreferencesTypes} from 'service/constants';
import {getMyPreferences as getMyPreferencesSelector} from 'service/selectors/entities/preferences';
import {getCurrentUserId} from 'service/selectors/entities/users';
import {getPreferenceKey} from 'service/utils/preference_utils';
import {bindClientFunc, forceLogoutIfNecessary} from './helpers';
import {getLogErrorAction} from './errors';
export function getMyPreferences() {
return bindClientFunc(
Client.getMyPreferences,
PreferencesTypes.MY_PREFERENCES_REQUEST,
[PreferencesTypes.RECEIVED_PREFERENCES, PreferencesTypes.MY_PREFERENCES_SUCCESS],
PreferencesTypes.MY_PREFERENCES_FAILURE
);
}
export function savePreferences(preferences) {
return async (dispatch, getState) => {
dispatch({type: PreferencesTypes.SAVE_PREFERENCES_REQUEST}, getState);
try {
await Client.savePreferences(preferences);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch(batchActions([
{type: PreferencesTypes.SAVE_PREFERENCES_FAILURE, error},
getLogErrorAction(error)
]), getState);
return;
}
dispatch(batchActions([
{
type: PreferencesTypes.RECEIVED_PREFERENCES,
data: preferences
},
{
type: PreferencesTypes.SAVE_PREFERENCES_SUCCESS
}
]), getState);
};
}
export function deletePreferences(preferences) {
return async (dispatch, getState) => {
dispatch({type: PreferencesTypes.DELETE_PREFERENCES_REQUEST}, getState);
try {
await Client.deletePreferences(preferences);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch(batchActions([
{type: PreferencesTypes.DELETE_PREFERENCES_FAILURE, error},
getLogErrorAction(error)
]), getState);
return;
}
dispatch(batchActions([
{
type: PreferencesTypes.DELETED_PREFERENCES,
data: preferences
},
{
type: PreferencesTypes.DELETE_PREFERENCES_SUCCESS
}
]), getState);
};
}
export function makeDirectChannelVisibleIfNecessary(otherUserId) {
return async (dispatch, getState) => {
const state = getState();
const myPreferences = getMyPreferencesSelector(state);
const currentUserId = getCurrentUserId(state);
let preference = myPreferences[getPreferenceKey(Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, otherUserId)];
if (!preference || preference.value === 'false') {
preference = {
user_id: currentUserId,
category: Preferences.CATEGORY_DIRECT_CHANNEL_SHOW,
name: otherUserId,
value: 'true'
};
await savePreferences([preference])(dispatch, getState);
}
};
}