UNPKG

mattermost-redux

Version:

Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client

133 lines (132 loc) 5.3 kB
"use strict"; // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. Object.defineProperty(exports, "__esModule", { value: true }); exports.getClientConfig = getClientConfig; exports.getLicenseConfig = getLicenseConfig; exports.getCustomProfileAttributeFields = getCustomProfileAttributeFields; exports.logClientError = logClientError; exports.setServerVersion = setServerVersion; exports.setUrl = setUrl; exports.setFirstAdminVisitMarketplaceStatus = setFirstAdminVisitMarketplaceStatus; exports.getFirstAdminSetupComplete = getFirstAdminSetupComplete; exports.checkCWSAvailability = checkCWSAvailability; const redux_batched_actions_1 = require("redux-batched-actions"); const client4_1 = require("@mattermost/types/client4"); const action_types_1 = require("mattermost-redux/action_types"); const client_1 = require("mattermost-redux/client"); const errors_1 = require("./errors"); const helpers_1 = require("./helpers"); const roles_1 = require("./roles"); function getClientConfig() { return async (dispatch, getState) => { let data; try { data = await client_1.Client4.getClientConfig(); } catch (error) { (0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState); return { error }; } client_1.Client4.setEnableLogging(data.EnableDeveloper === 'true'); client_1.Client4.setDiagnosticId(data.DiagnosticId); const type = data.AppsPluginEnabled === 'true' ? action_types_1.AppsTypes.APPS_PLUGIN_ENABLED : action_types_1.AppsTypes.APPS_PLUGIN_DISABLED; const actions = [{ type: action_types_1.GeneralTypes.CLIENT_CONFIG_RECEIVED, data }, { type }]; dispatch((0, redux_batched_actions_1.batchActions)(actions)); return { data }; }; } function getLicenseConfig() { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getClientLicenseOld, onSuccess: [action_types_1.GeneralTypes.CLIENT_LICENSE_RECEIVED], }); } function getCustomProfileAttributeFields() { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.getCustomProfileAttributeFields, onSuccess: [action_types_1.GeneralTypes.CUSTOM_PROFILE_ATTRIBUTE_FIELDS_RECEIVED], }); } function logClientError(message, level = client4_1.LogLevel.Error) { return (0, helpers_1.bindClientFunc)({ clientFunc: client_1.Client4.logClientError, onRequest: action_types_1.GeneralTypes.LOG_CLIENT_ERROR_REQUEST, onSuccess: action_types_1.GeneralTypes.LOG_CLIENT_ERROR_SUCCESS, onFailure: action_types_1.GeneralTypes.LOG_CLIENT_ERROR_FAILURE, params: [ message, level, ], }); } function setServerVersion(serverVersion) { return async (dispatch) => { dispatch({ type: action_types_1.GeneralTypes.RECEIVED_SERVER_VERSION, data: serverVersion }); dispatch((0, roles_1.loadRolesIfNeeded)([])); return { data: true }; }; } function setUrl(url) { client_1.Client4.setUrl(url); return true; } function setFirstAdminVisitMarketplaceStatus() { return async (dispatch) => { try { await client_1.Client4.setFirstAdminVisitMarketplaceStatus(); } catch (e) { dispatch((0, errors_1.logError)(e)); return { error: e.message }; } dispatch({ type: action_types_1.GeneralTypes.FIRST_ADMIN_VISIT_MARKETPLACE_STATUS_RECEIVED, data: true }); return { data: true }; }; } // accompanying "set" happens as part of Client4.completeSetup function getFirstAdminSetupComplete() { return async (dispatch, getState) => { let data; try { data = await client_1.Client4.getFirstAdminSetupComplete(); } catch (error) { (0, helpers_1.forceLogoutIfNecessary)(error, dispatch, getState); return { error }; } data = JSON.parse(data.value); dispatch({ type: action_types_1.GeneralTypes.FIRST_ADMIN_COMPLETE_SETUP_RECEIVED, data }); return { data }; }; } function checkCWSAvailability() { return async (dispatch, getState) => { const state = getState(); const config = state.entities.general.config; const isEnterpriseReady = config.BuildEnterpriseReady === 'true'; if (!isEnterpriseReady) { dispatch({ type: action_types_1.GeneralTypes.CWS_AVAILABILITY_CHECK_SUCCESS, data: 'not_applicable' }); return { data: 'not_applicable' }; } dispatch({ type: action_types_1.GeneralTypes.CWS_AVAILABILITY_CHECK_REQUEST }); try { const response = await client_1.Client4.cwsAvailabilityCheck(); const status = response.status; dispatch({ type: action_types_1.GeneralTypes.CWS_AVAILABILITY_CHECK_SUCCESS, data: status }); return { data: status }; } catch (error) { dispatch({ type: action_types_1.GeneralTypes.CWS_AVAILABILITY_CHECK_FAILURE }); return { data: 'unavailable' }; } }; } exports.default = { getClientConfig, getLicenseConfig, getCustomProfileAttributeFields, logClientError, setServerVersion, setUrl, };