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

add confirm modal before pinning messages #479

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
108 changes: 107 additions & 1 deletion packages/react/src/components/Message/Message.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo, useContext, useMemo } from 'react';
import React, { memo, useContext, useMemo, useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { format } from 'date-fns';
import { css } from '@emotion/react';
Expand All @@ -20,6 +20,9 @@ import { MessageDivider } from './MessageDivider';
import { useToastBarDispatch } from '../../hooks/useToastBarDispatch';
import MessageAvatarContainer from './MessageAvatarContainer';
import MessageBodyContainer from './MessageBodyContainer';
import { Modal } from '../Modal';
import { Icon } from '../Icon';
import { Button } from '../Button';

const MessageCss = css`
display: flex;
Expand Down Expand Up @@ -71,6 +74,20 @@ const Message = ({
setEditMessage: state.setEditMessage,
}));
const openThread = useMessageStore((state) => state.openThread);
const [pinMessageConfirmed, setPinMessageConfirmed] = useState(false);
const [confirmationModal, setConfirmationModal] = useState(false);

const openConfirmationModal = () => {
setConfirmationModal(true);
};
const closeConfirmationModal = () => {
setConfirmationModal(false);
};

const handleConfirmPinning = () => {
setPinMessageConfirmed(true);
closeConfirmationModal();
};

const handleStarMessage = async (msg) => {
const isStarred =
Expand All @@ -92,6 +109,12 @@ const Message = ({

const handlePinMessage = async (msg) => {
const isPinned = msg.pinned;
if (!isPinned && !pinMessageConfirmed) {
openConfirmationModal();
return;
}
setPinMessageConfirmed(false);

const pinOrUnpin = isPinned
? await RCInstance.unpinMessage(msg._id)
: await RCInstance.pinMessage(msg._id);
Expand All @@ -108,6 +131,10 @@ const Message = ({
}
};

useEffect(() => {
if (pinMessageConfirmed) handlePinMessage(message);
}, [pinMessageConfirmed]);

const handleDeleteMessage = async (msg) => {
const res = await RCInstance.deleteMessage(msg._id);

Expand Down Expand Up @@ -248,6 +275,85 @@ const Message = ({
{format(new Date(message.ts), 'MMMM d, yyyy')}
</MessageDivider>
) : null}
{confirmationModal && (
<Modal>
<Modal
css={css`
padding: 1em;
`}
onClose={closeConfirmationModal}
>
<Modal.Header>
<Modal.Title>
<Icon
name="pin"
size="1.25rem"
css={css`
margin-right: 5px;
`}
/>
Pin Message
</Modal.Title>
<Modal.Close onClick={closeConfirmationModal} />
</Modal.Header>
<Modal.Content
css={css`
margin: 1em;
`}
>
{' '}
Are you sure you want to pin this message?{' '}
</Modal.Content>
<Modal.Content
css={css`
margin: 1em;
`}
>
{/* Add the isPinned prop after that gets merged */}
<Box key={message._id} css={MessageCss}>
{showAvatar && (
<MessageAvatarContainer
message={message}
sequential={false}
isStarred={false}
/>
)}
<MessageBodyContainer>
<MessageHeader message={message} isTimeStamped={false} />
<MessageBody>
{message.attachments && message.attachments.length > 0
? message.file.name
: message.msg}
</MessageBody>
</MessageBodyContainer>
</Box>
</Modal.Content>
<Modal.Content
css={css`
margin: 1em;
`}
>
<span
css={css`
font-weight: bold;
display: block;
`}
>
Pinned messages are visible to everyone
</span>
Starred messages are only visible to you
</Modal.Content>
<Modal.Footer>
<Button color="secondary" onClick={closeConfirmationModal}>
Cancel
</Button>
<Button onClick={handleConfirmPinning} color="primary">
Yes, Pin Message
</Button>
</Modal.Footer>
</Modal>
</Modal>
)}
</>
);
};
Expand Down
Loading