mattermost-redux
Version:
Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client
81 lines (80 loc) • 3.02 kB
JavaScript
;
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.receivedChannelRemotes = receivedChannelRemotes;
exports.receivedRemoteClusterInfo = receivedRemoteClusterInfo;
exports.fetchChannelRemotes = fetchChannelRemotes;
exports.fetchRemoteClusterInfo = fetchRemoteClusterInfo;
const errors_1 = require("mattermost-redux/actions/errors");
const helpers_1 = require("mattermost-redux/actions/helpers");
const client_1 = require("mattermost-redux/client");
const shared_channels_1 = __importDefault(require("../action_types/shared_channels"));
function receivedChannelRemotes(channelId, remotes) {
return {
type: shared_channels_1.default.RECEIVED_CHANNEL_REMOTES,
data: {
channelId,
remotes,
},
};
}
function receivedRemoteClusterInfo(remoteId, remoteInfo) {
return {
type: shared_channels_1.default.RECEIVED_REMOTE_CLUSTER_INFO,
data: {
remoteId,
remoteInfo,
},
};
}
function fetchChannelRemotes(channelId, forceRefresh = false) {
return async (dispatch, getState) => {
// Check if we already have the data in the Redux store
const state = getState();
const remotes = state.entities?.sharedChannels?.remotes?.[channelId];
// If we already have the data and no refresh is requested, use the cached data
if (!forceRefresh && remotes && remotes.length > 0) {
return { data: remotes };
}
let data;
try {
data = await client_1.Client4.getSharedChannelRemoteInfos(channelId);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
dispatch((0, errors_1.logError)(error));
return { error };
}
if (data) {
dispatch(receivedChannelRemotes(channelId, data));
}
return { data };
};
}
function fetchRemoteClusterInfo(remoteId, forceRefresh = false) {
return async (dispatch, getState) => {
// Check if we already have the remote info cached
const state = getState();
const cachedRemote = state.entities?.sharedChannels?.remotesByRemoteId?.[remoteId];
if (!forceRefresh && cachedRemote) {
return { data: cachedRemote };
}
let data;
try {
data = await client_1.Client4.getRemoteClusterInfo(remoteId);
}
catch (error) {
(0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState);
dispatch((0, errors_1.logError)(error));
return { error };
}
if (data) {
dispatch(receivedRemoteClusterInfo(remoteId, data));
}
return { data };
};
}