UNPKG

mattermost-redux

Version:

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

315 lines (314 loc) 10.9 kB
"use strict"; // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. Object.defineProperty(exports, "__esModule", { value: true }); const redux_1 = require("redux"); const action_types_1 = require("mattermost-redux/action_types"); function incomingHooks(state = {}, action) { switch (action.type) { case action_types_1.IntegrationTypes.RECEIVED_INCOMING_HOOK: { const nextState = { ...state }; nextState[action.data.id] = action.data; return nextState; } case action_types_1.IntegrationTypes.RECEIVED_INCOMING_HOOKS: { const nextState = { ...state }; for (const hook of action.data) { nextState[hook.id] = hook; } return nextState; } case action_types_1.IntegrationTypes.DELETED_INCOMING_HOOK: { const nextState = { ...state }; Reflect.deleteProperty(nextState, action.data.id); return nextState; } case action_types_1.ChannelTypes.RECEIVED_CHANNEL_DELETED: { const nextState = { ...state }; let deleted = false; Object.keys(nextState).forEach((id) => { if (nextState[id].channel_id === action.data.id) { deleted = true; Reflect.deleteProperty(nextState, id); } }); if (deleted) { return nextState; } return state; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function incomingHooksTotalCount(state = 0, action) { switch (action.type) { case action_types_1.IntegrationTypes.RECEIVED_INCOMING_HOOKS_TOTAL_COUNT: { return action.data; } case action_types_1.IntegrationTypes.DELETED_INCOMING_HOOK: { return Math.max(state - 1, 0); } default: return state; } } function outgoingHooks(state = {}, action) { switch (action.type) { case action_types_1.IntegrationTypes.RECEIVED_OUTGOING_HOOK: { const nextState = { ...state }; nextState[action.data.id] = action.data; return nextState; } case action_types_1.IntegrationTypes.RECEIVED_OUTGOING_HOOKS: { const nextState = { ...state }; for (const hook of action.data) { nextState[hook.id] = hook; } return nextState; } case action_types_1.IntegrationTypes.DELETED_OUTGOING_HOOK: { const nextState = { ...state }; Reflect.deleteProperty(nextState, action.data.id); return nextState; } case action_types_1.ChannelTypes.RECEIVED_CHANNEL_DELETED: { const nextState = { ...state }; let deleted = false; Object.keys(nextState).forEach((id) => { if (nextState[id].channel_id === action.data.id) { deleted = true; Reflect.deleteProperty(nextState, id); } }); if (deleted) { return nextState; } return state; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function commands(state = {}, action) { switch (action.type) { case action_types_1.IntegrationTypes.RECEIVED_COMMANDS: case action_types_1.IntegrationTypes.RECEIVED_CUSTOM_TEAM_COMMANDS: { const nextState = { ...state }; for (const command of action.data) { if (command.id) { const id = command.id; nextState[id] = command; } } return nextState; } case action_types_1.IntegrationTypes.RECEIVED_COMMAND: if (action.data.id) { return { ...state, [action.data.id]: action.data, }; } return state; case action_types_1.IntegrationTypes.RECEIVED_COMMAND_TOKEN: { const { id, token } = action.data; return { ...state, [id]: { ...state[id], token, }, }; } case action_types_1.IntegrationTypes.DELETED_COMMAND: { const nextState = { ...state }; Reflect.deleteProperty(nextState, action.data.id); return nextState; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function systemCommands(state = {}, action) { switch (action.type) { case action_types_1.IntegrationTypes.RECEIVED_COMMANDS: { const nextCommands = {}; for (const command of action.data) { if (!command.id) { nextCommands[command.trigger] = command; } } return nextCommands; } case action_types_1.IntegrationTypes.RECEIVED_COMMAND: if (!action.data.id) { return { ...state, [action.data.trigger]: action.data, }; } return state; case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function oauthApps(state = {}, action) { switch (action.type) { case action_types_1.IntegrationTypes.RECEIVED_OAUTH_APPS: { const nextState = { ...state }; for (const app of action.data) { nextState[app.id] = app; } return nextState; } case action_types_1.IntegrationTypes.RECEIVED_OAUTH_APP: return { ...state, [action.data.id]: action.data, }; case action_types_1.IntegrationTypes.DELETED_OAUTH_APP: { const nextState = { ...state }; Reflect.deleteProperty(nextState, action.data.id); return nextState; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function appsOAuthAppIDs(state = [], action) { switch (action.type) { case action_types_1.IntegrationTypes.RECEIVED_APPS_OAUTH_APP_IDS: { if (state.length === 0 && action.data.length === 0) { return state; } if (state.length !== action.data.length) { return action.data; } const orderedState = state.concat().sort(); const orderedData = action.data.concat().sort(); for (let i = 0; i < state.length; i++) { if (orderedState[i] !== orderedData[i]) { return orderedData; } } return state; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return []; default: return state; } } function outgoingOAuthConnections(state = {}, action) { switch (action.type) { case action_types_1.IntegrationTypes.RECEIVED_OUTGOING_OAUTH_CONNECTIONS: { const nextState = { ...state }; for (const connection of action.data) { nextState[connection.id] = connection; } return nextState; } case action_types_1.IntegrationTypes.RECEIVED_OUTGOING_OAUTH_CONNECTION: return { ...state, [action.data.id]: action.data, }; case action_types_1.IntegrationTypes.DELETED_OUTGOING_OAUTH_CONNECTION: { const nextState = { ...state }; Reflect.deleteProperty(nextState, action.data.id); return nextState; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; default: return state; } } function appsBotIDs(state = [], action) { switch (action.type) { case action_types_1.IntegrationTypes.RECEIVED_APPS_BOT_IDS: { if (!action.data) { return state; } if (state.length === 0 && action.data.length === 0) { return state; } if (state.length !== action.data.length) { return action.data; } const orderedState = state.concat().sort(); const orderedData = action.data.concat().sort(); for (let i = 0; i < state.length; i++) { if (orderedState[i] !== orderedData[i]) { return orderedData; } } return state; } case action_types_1.UserTypes.LOGOUT_SUCCESS: return []; default: return state; } } function dialogArguments(state = null, action) { switch (action.type) { case action_types_1.IntegrationTypes.RECEIVED_DIALOG_ARGUMENTS: return action.data; default: return state; } } function dialogTriggerId(state = '', action) { switch (action.type) { case action_types_1.IntegrationTypes.RECEIVED_DIALOG_TRIGGER_ID: return action.data; default: return state; } } function dialog(state = '', action) { switch (action.type) { case action_types_1.IntegrationTypes.RECEIVED_DIALOG: return action.data; default: return state; } } exports.default = (0, redux_1.combineReducers)({ // object where every key is the hook id and has an object with the incoming hook details incomingHooks, // object to represent total amount of incoming hooks incomingHooksTotalCount, // object where every key is the hook id and has an object with the outgoing hook details outgoingHooks, // object to represent installed slash commands for a current team commands, // object to represent registered oauth apps with app id as the key oauthApps, // object to represent the list of ids for oauth apps associated to apps appsOAuthAppIDs, // object to represent the list of ids for bots associated to apps appsBotIDs, // object to represent registered outgoing oauth connections with connection id as the key outgoingOAuthConnections, // object to represent built-in slash commands systemCommands, // object containing arguments for interactive dialog dialogArguments, // trigger ID for interactive dialogs dialogTriggerId, // data for an interactive dialog to display dialog, });