simplification by using derived data

pull/1770/head
Brice-W 4 years ago
parent e3aa500ba0
commit 75a20e5de7

@ -90,13 +90,10 @@ export class SessionInboxView extends React.Component<any, State> {
const fullFilledConversations = await Promise.all(filledConversations);
const pinnedConversations = conversations.filter( (conversation) => conversation.isPinned).length;
const initialState: StateType = {
conversations: {
conversationLookup: makeLookup(fullFilledConversations, 'id'),
messages: [],
pinnedConversations: pinnedConversations,
},
user: {
ourNumber: UserUtils.getOurPubKeyStrFromCache(),

@ -1,7 +1,6 @@
import React from 'react';
import { getNumberOfPinnedConversations } from '../../../state/selectors/conversations';
import { conversationPinned, conversationUnpinned } from '../../../state/ducks/conversations';
import { NotificationForConvoOption, TimerOption } from '../../conversation/ConversationHeader';
import { Item, Submenu } from 'react-contexify';
import { ConversationNotificationSettingType } from '../../../models/conversation';
@ -141,12 +140,8 @@ export const MenuItemPinConversation = (
const nbOfAlreadyPinnedConvos = useSelector(getNumberOfPinnedConversations);
const togglePinConversation = async () => {
if (!isPinned && nbOfAlreadyPinnedConvos < maxNumberOfPinnedConversations) {
if ((!isPinned && nbOfAlreadyPinnedConvos < maxNumberOfPinnedConversations) || isPinned) {
await conversation.setIsPinned(!isPinned);
dispatch(conversationPinned());
} else if (isPinned) {
await conversation.setIsPinned(!isPinned);
dispatch(conversationUnpinned());
} else {
ToastUtils.pushToastWarning(
'pinConversationLimitToast',

@ -93,7 +93,6 @@ export type ConversationsStateType = {
conversationLookup: ConversationLookupType;
selectedConversation?: string;
messages: Array<MessageTypeInConvo>;
pinnedConversations: number;
};
async function getMessages(
@ -244,14 +243,6 @@ export type SelectedConversationChangedActionType = {
messageId?: string;
};
};
export type ConversationPinnedActionType = {
type: 'CONVERSATION_PINNED';
payload: null;
};
export type ConversationUnpinnedActionType = {
type: 'CONVERSATION_UNPINNED';
payload: null;
};
export type FetchMessagesForConversationType = {
type: 'messages/fetchByConversationKey/fulfilled';
@ -274,8 +265,6 @@ export type ConversationActionType =
| MessagesChangedActionType
| SelectedConversationChangedActionType
| SelectedConversationChangedActionType
| ConversationPinnedActionType
| ConversationUnpinnedActionType
| FetchMessagesForConversationType;
// Action Creators
@ -293,8 +282,6 @@ export const actions = {
messagesChanged,
fetchMessagesForConversation,
openConversationExternal,
conversationPinned,
conversationUnpinned,
};
function conversationAdded(id: string, data: ConversationType): ConversationAddedActionType {
@ -419,20 +406,6 @@ export function openConversationExternal(
};
}
export function conversationPinned(): ConversationPinnedActionType {
return {
type: 'CONVERSATION_PINNED',
payload: null,
};
}
export function conversationUnpinned(): ConversationUnpinnedActionType {
return {
type: 'CONVERSATION_UNPINNED',
payload: null,
};
}
// Reducer
const toPickFromMessageModel = [
@ -464,7 +437,6 @@ function getEmptyState(): ConversationsStateType {
return {
conversationLookup: {},
messages: [],
pinnedConversations: 0,
};
}
@ -611,30 +583,6 @@ function handleConversationReset(
return state;
}
function handleConversationPinned(
state: ConversationsStateType,
action: ConversationPinnedActionType
) {
const { pinnedConversations } = state;
return {
...state,
pinnedConversations: pinnedConversations + 1,
};
}
function handleConversationUnpinned(
state: ConversationsStateType,
action: ConversationUnpinnedActionType
) {
const { pinnedConversations } = state;
return {
...state,
pinnedConversations: (pinnedConversations > 0) ? pinnedConversations - 1 : 0,
};
}
// tslint:disable: cyclomatic-complexity
// tslint:disable: max-func-body-length
export function reducer(
@ -743,13 +691,5 @@ export function reducer(
return handleConversationReset(state, action);
}
if (action.type === 'CONVERSATION_PINNED') {
return handleConversationPinned(state, action);
}
if (action.type === 'CONVERSATION_UNPINNED') {
return handleConversationUnpinned(state, action);
}
return state;
}

@ -215,5 +215,6 @@ export const getUnreadMessageCount = createSelector(getLeftPaneLists, (state): n
});
export const getNumberOfPinnedConversations = createSelector(getConversations, (state): number => {
return state.pinnedConversations;
const values = Object.values(state.conversationLookup);
return values.filter(conversation => conversation.isPinned).length;
});

Loading…
Cancel
Save