UNPKG

mattermost-redux

Version:

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

102 lines (101 loc) 3.6 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.customEmoji = customEmoji; const redux_1 = require("redux"); const action_types_1 = require("mattermost-redux/action_types"); function customEmoji(state = {}, action) { switch (action.type) { case action_types_1.EmojiTypes.RECEIVED_CUSTOM_EMOJI: { const emoji = action.data; return storeEmoji(state, emoji); } case action_types_1.EmojiTypes.RECEIVED_CUSTOM_EMOJIS: { const emojis = action.data; return emojis.reduce(storeEmoji, state); } case action_types_1.EmojiTypes.DELETED_CUSTOM_EMOJI: { const emoji = action.data; if (!state[emoji.id]) { return state; } const nextState = { ...state }; Reflect.deleteProperty(nextState, emoji.id); return nextState; } case action_types_1.EmojiTypes.CLEAR_CUSTOM_EMOJIS: case action_types_1.UserTypes.LOGOUT_SUCCESS: return {}; case action_types_1.PostTypes.RECEIVED_NEW_POST: case action_types_1.PostTypes.RECEIVED_POST: { const post = action.data; return storeEmojisForPost(state, post); } case action_types_1.PostTypes.RECEIVED_POSTS: { const posts = Object.values(action.data.posts); return posts.reduce(storeEmojisForPost, state); } default: return state; } } function storeEmoji(state, emoji) { if (state[emoji.id]) { // Emoji is already in the store return state; } return { ...state, [emoji.id]: emoji, }; } function storeEmojisForPost(state, post) { if (!post.metadata || !post.metadata.emojis) { return state; } return post.metadata.emojis.reduce(storeEmoji, state); } function nonExistentEmoji(state = new Set(), action) { switch (action.type) { case action_types_1.EmojiTypes.CUSTOM_EMOJI_DOES_NOT_EXIST: { if (!state.has(action.data)) { const nextState = new Set(state); nextState.add(action.data); return nextState; } return state; } case action_types_1.EmojiTypes.RECEIVED_CUSTOM_EMOJI: { if (action.data && state.has(action.data.name)) { const nextState = new Set(state); nextState.delete(action.data.name); return nextState; } return state; } case action_types_1.EmojiTypes.RECEIVED_CUSTOM_EMOJIS: { const data = action.data || []; const nextState = new Set(state); let changed = false; for (const emoji of data) { if (emoji && nextState.has(emoji.name)) { nextState.delete(emoji.name); changed = true; } } return changed ? nextState : state; } case action_types_1.EmojiTypes.CLEAR_CUSTOM_EMOJIS: case action_types_1.UserTypes.LOGOUT_SUCCESS: return new Set(); default: return state; } } exports.default = (0, redux_1.combineReducers)({ // object where every key is the custom emoji id and has an object with the custom emoji details customEmoji, // set containing custom emoji names that do not exist nonExistentEmoji, });