mattermost-mobile/app/mm-redux/actions/preferences.ts
Miguel Alatzar 9a0bce5b6d [MM-23698] [MM-19559] Remove redux-offline and updated redux-persist (#4120)
* Remove redux-offline and configure redux-persist

* Fix typo

* Fix configure store

* Upgrade to redux-persist 6.0.0

* Add migration from redux-persist v4 to v6

* Replace AsyncStorage with MMKVStorage to boost storage speed

* Mock RNFastStorage

* Fix reactions test

* Fix clearing the store on logout

* Remove the need for LOGOUT_SUCCESS

* No need to pass persistConfig to middlewares()

* Remove unused imports

* Export connection

Accidentally removed this export.

* Add batch action name

Co-Authored-By: Elias Nahum <nahumhbl@gmail.com>

* Add batch action name

Co-Authored-By: Elias Nahum <nahumhbl@gmail.com>

* Add batch action name

Co-Authored-By: Elias Nahum <nahumhbl@gmail.com>

* Add batch action name

Co-Authored-By: Elias Nahum <nahumhbl@gmail.com>

* Fix delete post

* Fix leave channel

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2020-04-17 22:30:27 -07:00

156 lines
5.6 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Client4} from '@mm-redux/client';
import {Preferences} from '../constants';
import {PreferenceTypes} from '@mm-redux/action_types';
import {getMyPreferences as getMyPreferencesSelector, makeGetCategory} from '@mm-redux/selectors/entities/preferences';
import {getCurrentUserId} from '@mm-redux/selectors/entities/users';
import {getPreferenceKey} from '@mm-redux/utils/preference_utils';
import {GetStateFunc, DispatchFunc, ActionFunc} from '@mm-redux/types/actions';
import {PreferenceType} from '@mm-redux/types/preferences';
import {bindClientFunc} from './helpers';
import {getProfilesByIds, getProfilesInChannel} from './users';
import {getChannelAndMyMember, getMyChannelMember} from './channels';
export function deletePreferences(userId: string, preferences: Array<PreferenceType>): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
const state = getState();
const myPreferences = getMyPreferencesSelector(state);
const currentPreferences = preferences.map((pref) => myPreferences[getPreferenceKey(pref.category, pref.name)]);
try {
dispatch({
type: PreferenceTypes.DELETED_PREFERENCES,
data: preferences,
});
await Client4.deletePreferences(userId, preferences);
} catch {
dispatch({
type: PreferenceTypes.RECEIVED_PREFERENCES,
data: currentPreferences,
});
}
return {data: true};
};
}
export function getMyPreferences(): ActionFunc {
return bindClientFunc({
clientFunc: Client4.getMyPreferences,
onSuccess: PreferenceTypes.RECEIVED_ALL_PREFERENCES,
});
}
export function makeDirectChannelVisibleIfNecessary(otherUserId: string): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
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',
};
getProfilesByIds([otherUserId])(dispatch, getState);
savePreferences(currentUserId, [preference])(dispatch);
}
return {data: true};
};
}
export function makeGroupMessageVisibleIfNecessary(channelId: string): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
const state = getState();
const myPreferences = getMyPreferencesSelector(state);
const currentUserId = getCurrentUserId(state);
const {channels} = state.entities.channels;
let preference = myPreferences[getPreferenceKey(Preferences.CATEGORY_GROUP_CHANNEL_SHOW, channelId)];
if (!preference || preference.value === 'false') {
preference = {
user_id: currentUserId,
category: Preferences.CATEGORY_GROUP_CHANNEL_SHOW,
name: channelId,
value: 'true',
};
if (channels[channelId]) {
getMyChannelMember(channelId)(dispatch, getState);
} else {
getChannelAndMyMember(channelId)(dispatch, getState);
}
getProfilesInChannel(channelId, 0)(dispatch, getState);
savePreferences(currentUserId, [preference])(dispatch);
}
return {data: true};
};
}
export function savePreferences(userId: string, preferences: Array<PreferenceType>) {
return async (dispatch: DispatchFunc) => {
try {
// Optimistic action
dispatch({
type: PreferenceTypes.RECEIVED_PREFERENCES,
data: preferences,
});
await Client4.savePreferences(userId, preferences);
} catch (error) {
dispatch({
type: PreferenceTypes.DELETED_PREFERENCES,
data: preferences,
});
return {error};
}
return {data: true};
};
}
export function saveTheme(teamId: string, theme: {}): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
const state = getState();
const currentUserId = getCurrentUserId(state);
const preference: PreferenceType = {
user_id: currentUserId,
category: Preferences.CATEGORY_THEME,
name: teamId || '',
value: JSON.stringify(theme),
};
await savePreferences(currentUserId, [preference])(dispatch);
return {data: true};
};
}
export function deleteTeamSpecificThemes(): ActionFunc {
return async (dispatch: DispatchFunc, getState: GetStateFunc) => {
const state = getState();
const getCategory: (state: any, preferenceId: string) => Array<PreferenceType> = makeGetCategory();
const themePreferences: Array<PreferenceType> = getCategory(state, Preferences.CATEGORY_THEME);
const currentUserId = getCurrentUserId(state);
const toDelete = themePreferences.filter((pref) => pref.name !== '');
if (toDelete.length > 0) {
await deletePreferences(currentUserId, toDelete)(dispatch, getState);
}
return {data: true};
};
}