Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRS-289: make reply/reactions available in messageActions #547

Merged
merged 4 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/components/Message/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ const Message = (props) => {
onUserHoverHandler: propOnUserHover,
});
const { isMyMessage, isAdmin, isModerator, isOwner } = useUserRole(message);
const canReact = true;
const canReply = true;
const canEdit = isMyMessage || isModerator || isOwner || isAdmin;
const canDelete = canEdit;
const messageActionsHandler = useCallback(() => {
Expand All @@ -88,10 +90,21 @@ const Message = (props) => {
return getMessageActions(messageActions, {
canDelete,
canEdit,
canReply,
canReact,
canFlag: !isMyMessage,
canMute: !isMyMessage && !!channelConfig?.mutes,
});
}, [channelConfig, message, messageActions, canDelete, canEdit, isMyMessage]);
}, [
channelConfig,
message,
messageActions,
canDelete,
canEdit,
canReply,
canReact,
isMyMessage,
]);

const actionsEnabled =
message && message.type === 'regular' && message.status === 'received';
Expand Down
17 changes: 14 additions & 3 deletions src/components/Message/MessageOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { useContext } from 'react';
import { useUserRole, useOpenThreadHandler } from './hooks';
import { ChannelContext } from '../../context';
import { MessageActions } from '../MessageActions';
import { MESSAGE_ACTIONS } from './utils';
import { ThreadIcon, ReactionIcon } from './icons';

/**
Expand All @@ -28,9 +29,19 @@ const MessageOptionsComponent = (props) => {
*/
const { channel } = useContext(ChannelContext);
const channelConfig = channel?.getConfig();
const messageActions = props.getMessageActions();
const shouldShowReactions =
messageActions.indexOf(MESSAGE_ACTIONS.react) > -1 &&
channelConfig &&
channelConfig.reactions;

const shouldShowReplies =
displayReplies && !threadList && channelConfig && channelConfig.replies;
const shouldShowReactions = channelConfig && channelConfig.reactions;
messageActions.indexOf(MESSAGE_ACTIONS.reply) > -1 &&
displayReplies &&
!threadList &&
channelConfig &&
channelConfig.replies;

if (
!message ||
message.type === 'error' ||
Expand All @@ -53,7 +64,7 @@ const MessageOptionsComponent = (props) => {
<div
data-testid="thread-action"
onClick={propHandleOpenThread || handleOpenThread}
className={`str-chat__message-${theme} str-chat__message-${theme}__actions__action--thread`}
className={`str-chat__message-${theme}__actions__action str-chat__message-${theme}__actions__action--thread`}
>
<ThreadIcon />
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Message/__tests__/MessageCommerce.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function renderMessageCommerce(
>
<MessageCommerce
message={message}
getMessageActions={() => ['flag', 'mute']}
getMessageActions={() => ['flag', 'mute', 'react', 'reply']}
{...props}
/>
</ChannelContext.Provider>,
Expand Down
2 changes: 2 additions & 0 deletions src/components/Message/__tests__/MessageOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from 'mock-builders';
import { ChannelContext } from '../../../context';
import { MessageActions as MessageActionsMock } from '../../MessageActions';
import { MESSAGE_ACTIONS } from '../utils';
import MessageOptions from '../MessageOptions';

jest.mock('../../MessageActions', () => ({
Expand All @@ -22,6 +23,7 @@ const defaultProps = {
threadList: false,
messageWrapperRef: { current: document.createElement('div') },
onReactionListClick: () => {},
getMessageActions: () => Object.keys(MESSAGE_ACTIONS),
};

function generateAliceMessage(messageOptions) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/Message/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ describe('Message utils', () => {
canEdit: true,
canFlag: true,
canMute: true,
canReply: true,
canReact: true,
};
const actions = Object.values(MESSAGE_ACTIONS);

Expand Down
14 changes: 13 additions & 1 deletion src/components/Message/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const MESSAGE_ACTIONS = {
delete: 'delete',
flag: 'flag',
mute: 'mute',
react: 'react',
reply: 'reply',
};

/**
Expand All @@ -48,12 +50,14 @@ export const MESSAGE_ACTIONS = {
* canDelete?: boolean;
* canMute?: boolean;
* canFlag?: boolean;
* canReact?: boolean;
* canReply?: boolean;
* }} Capabilities
* @type {(actions: string[] | boolean, capabilities: Capabilities) => string[]} Typescript syntax
*/
export const getMessageActions = (
actions,
{ canDelete, canFlag, canEdit, canMute },
{ canDelete, canFlag, canEdit, canMute, canReact, canReply },
) => {
const messageActionsAfterPermission = [];
let messageActions = [];
Expand Down Expand Up @@ -83,6 +87,14 @@ export const getMessageActions = (
messageActionsAfterPermission.push(MESSAGE_ACTIONS.mute);
}

if (canReact && messageActions.indexOf(MESSAGE_ACTIONS.react) > -1) {
messageActionsAfterPermission.push(MESSAGE_ACTIONS.react);
}

if (canReply && messageActions.indexOf(MESSAGE_ACTIONS.reply) > -1) {
messageActionsAfterPermission.push(MESSAGE_ACTIONS.reply);
}

return messageActionsAfterPermission;
};

Expand Down