UNPKG

mattermost-redux

Version:

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

61 lines (51 loc) 1.38 kB
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import {WebsocketEvents} from '../../constants'; import {Typing} from 'types/typing'; import {GenericAction} from 'types/actions'; export default function typing(state: Typing = {}, action: GenericAction): Typing { const { data, type, } = action; switch (type) { case WebsocketEvents.TYPING: { const { id, userId, now, } = data; if (id && userId) { return { ...state, [id]: { ...(state[id] || {}), [userId]: now, }, }; } return state; } case WebsocketEvents.STOP_TYPING: { const { id, userId, now, } = data; if (state[id] && state[id][userId] <= now) { const nextState: Typing = { ...state, [id]: {...state[id]}, }; Reflect.deleteProperty(nextState[id], userId); if (Object.keys(nextState[id]).length === 0) { Reflect.deleteProperty(nextState, id); } return nextState; } return state; } default: return state; } }