UNPKG

mattermost-redux

Version:

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

264 lines (263 loc) 9.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 syncables(state = {}, action) { switch (action.type) { case action_types_1.GroupTypes.RECEIVED_GROUP_TEAMS: { return { ...state, [action.group_id]: { ...state[action.group_id], teams: action.data, }, }; } case action_types_1.GroupTypes.RECEIVED_GROUP_CHANNELS: { return { ...state, [action.group_id]: { ...state[action.group_id], channels: action.data, }, }; } case action_types_1.GroupTypes.PATCHED_GROUP_TEAM: case action_types_1.GroupTypes.LINKED_GROUP_TEAM: { let nextGroupTeams = []; if (!state[action.data.group_id] || !state[action.data.group_id].teams || state[action.data.group_id].teams.length === 0) { nextGroupTeams = [action.data]; } else { nextGroupTeams = { ...state }[action.data.group_id].teams.slice(); for (let i = 0, len = nextGroupTeams.length; i < len; i++) { if (nextGroupTeams[i].team_id === action.data.team_id) { nextGroupTeams[i] = action.data; } } } return { ...state, [action.data.group_id]: { ...state[action.data.group_id], teams: nextGroupTeams, }, }; } case action_types_1.GroupTypes.PATCHED_GROUP_CHANNEL: case action_types_1.GroupTypes.LINKED_GROUP_CHANNEL: { let nextGroupChannels = []; if (!state[action.data.group_id] || !state[action.data.group_id].channels) { nextGroupChannels = [action.data]; } else { nextGroupChannels = { ...state }[action.data.group_id].channels.slice(); for (let i = 0, len = nextGroupChannels.length; i < len; i++) { if (nextGroupChannels[i].channel_id === action.data.channel_id) { nextGroupChannels[i] = action.data; } } } return { ...state, [action.data.group_id]: { ...state[action.data.group_id], channels: nextGroupChannels, }, }; } case action_types_1.GroupTypes.UNLINKED_GROUP_TEAM: { if (!state[action.data.group_id]) { return state; } let nextTeams = []; let nextChannels = []; if (state[action.data.group_id].teams?.length > 0) { nextTeams = state[action.data.group_id].teams.slice(); const index = nextTeams.findIndex((groupTeam) => { return groupTeam.team_id === action.data.syncable_id; }); if (index !== -1) { nextTeams.splice(index, 1); } } // When we remove a team we also need to remove all channels in that team if (state[action.data.group_id].channels?.length > 0) { nextChannels = state[action.data.group_id].channels.slice(); const index = nextChannels.findIndex((groupChannel) => { return groupChannel.team_id === action.data.syncable_id; }); if (index !== -1) { nextChannels.splice(index, 1); } } return { ...state, [action.data.group_id]: { ...state[action.data.group_id], teams: nextTeams, channels: nextChannels, }, }; } case action_types_1.GroupTypes.UNLINKED_GROUP_CHANNEL: { if (!state[action.data.group_id]) { return state; } const nextChannels = state[action.data.group_id].channels.slice(); const index = nextChannels.findIndex((groupChannel) => { return groupChannel.channel_id === action.data.syncable_id; }); if (index !== -1) { nextChannels.splice(index, 1); } return { ...state, [action.data.group_id]: { ...state[action.data.group_id], channels: nextChannels, }, }; } default: return state; } } function myGroups(state = [], action) { switch (action.type) { case action_types_1.GroupTypes.ADD_MY_GROUP: { const groupId = action.id; const nextState = [...state]; if (state.indexOf(groupId) === -1) { nextState.push(groupId); } return nextState; } case action_types_1.GroupTypes.RECEIVED_MY_GROUPS: { const groups = action.data; const nextState = [...state]; groups.forEach((group) => { const index = state.indexOf(group.id); if (index === -1) { nextState.push(group.id); } }); return nextState; } case action_types_1.GroupTypes.REMOVE_MY_GROUP: { const groupId = action.id; const index = state.indexOf(groupId); if (index === -1) { // There's nothing to remove return state; } // Remove the group ID from my groups list const nextState = [...state]; nextState.splice(index, 1); return nextState; } default: return state; } } function stats(state = {}, action) { switch (action.type) { case action_types_1.GroupTypes.RECEIVED_GROUP_STATS: { const stat = action.data; return { ...state, [stat.group_id]: stat, }; } default: return state; } } function groups(state = {}, action) { switch (action.type) { case action_types_1.GroupTypes.CREATE_GROUP_SUCCESS: case action_types_1.GroupTypes.PATCHED_GROUP: case action_types_1.GroupTypes.RESTORED_GROUP: case action_types_1.GroupTypes.ARCHIVED_GROUP: case action_types_1.GroupTypes.RECEIVED_GROUP: { return { ...state, [action.data.id]: action.data, }; } case action_types_1.GroupTypes.RECEIVED_MY_GROUPS: case action_types_1.GroupTypes.RECEIVED_GROUPS: { const nextState = { ...state }; for (const group of action.data) { nextState[group.id] = group; } return nextState; } case action_types_1.GroupTypes.RECEIVED_ALL_GROUPS_ASSOCIATED_TO_CHANNELS_IN_TEAM: { const nextState = { ...state }; const { groupsByChannelId } = action.data; for (const channelID of Object.keys(groupsByChannelId)) { if (groupsByChannelId[channelID]) { for (const group of groupsByChannelId[channelID]) { nextState[group.id] = group; } } } return nextState; } case action_types_1.GroupTypes.RECEIVED_GROUPS_ASSOCIATED_TO_TEAM: case action_types_1.GroupTypes.RECEIVED_ALL_GROUPS_ASSOCIATED_TO_TEAM: case action_types_1.GroupTypes.RECEIVED_ALL_GROUPS_ASSOCIATED_TO_CHANNEL: case action_types_1.GroupTypes.RECEIVED_GROUPS_ASSOCIATED_TO_CHANNEL: { const nextState = { ...state }; for (const group of action.data.groups) { nextState[group.id] = group; } return nextState; } case action_types_1.GroupTypes.RECEIVED_MEMBER_TO_REMOVE_FROM_GROUP: { const dataInfo = action.data; const group = state[dataInfo.group_id]; if (Array.isArray(group?.member_ids)) { const newMemberIds = new Set(group.member_ids); newMemberIds.delete(dataInfo.user_id); const newGroup = { ...group, member_ids: [...newMemberIds], member_count: newMemberIds.size, }; return { ...state, [group.id]: newGroup, }; } return state; } case action_types_1.GroupTypes.RECEIVED_MEMBER_TO_ADD_TO_GROUP: { const { group_id: groupId, user_id: userId } = action.data; const group = state[groupId]; if (Array.isArray(group?.member_ids)) { const newMemberIds = new Set(group.member_ids); newMemberIds.add(userId); const newGroup = { ...group, member_ids: [...newMemberIds], member_count: newMemberIds.size, }; return { ...state, [group.id]: newGroup, }; } return state; } default: return state; } } exports.default = (0, redux_1.combineReducers)({ syncables, groups, stats, myGroups, });