Skip to content

Commit

Permalink
Step 11.1: Distinguish messages
Browse files Browse the repository at this point in the history
  • Loading branch information
DAB0mB authored and Urigo committed Jul 28, 2019
1 parent 6fae866 commit 420318c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
Binary file added public/assets/message-other.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 30 additions & 7 deletions src/components/ChatRoomScreen/MessagesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import moment from 'moment';
import React from 'react';
import { useEffect, useRef } from 'react';
import ReactDOM from 'react-dom';
import styled from 'styled-components';
import styled, { css } from 'styled-components';

const Container = styled.div`
display: block;
Expand All @@ -11,9 +11,11 @@ const Container = styled.div`
padding: 0 15px;
`;

type StyledProp = {
isMine: any;
};

const MessageItem = styled.div`
float: right;
background-color: #dcf8c6;
display: inline-block;
position: relative;
max-width: 100%;
Expand All @@ -30,17 +32,36 @@ const MessageItem = styled.div`
}
&::before {
background-image: url(/assets/message-mine.png);
content: '';
position: absolute;
bottom: 3px;
width: 12px;
height: 19px;
right: -11px;
background-position: 50% 50%;
background-repeat: no-repeat;
background-size: contain;
}
${(props: StyledProp) =>
props.isMine
? css`
float: right;
background-color: #dcf8c6;
&::before {
right: -11px;
background-image: url(/assets/message-mine.png);
}
`
: css`
float: left;
background-color: #fff;
&::before {
left: -11px;
background-image: url(/assets/message-other.png);
}
`}
`;

const Contents = styled.div`
Expand Down Expand Up @@ -75,15 +96,17 @@ const MessagesList: React.FC<MessagesListProps> = ({ messages }) => {

useEffect(() => {
if (!selfRef.current) return;

const selfDOMNode = ReactDOM.findDOMNode(selfRef.current) as HTMLElement;
selfDOMNode.scrollTop = Number.MAX_SAFE_INTEGER;
}, [messages.length]);

return (
<Container ref={selfRef}>
{messages.map((message: any) => (
<MessageItem data-testid="message-item" key={message.id}>
<MessageItem
data-testid="message-item"
isMine={message.isMine}
key={message.id}>
<Contents data-testid="message-content">{message.content}</Contents>
<Timestamp data-testid="message-date">
{moment(message.createdAt).format('HH:mm')}
Expand Down
1 change: 1 addition & 0 deletions src/components/ChatRoomScreen/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const [addMessage] = useAddMessageMutation();
.toString(36)
.substr(2, 9),
createdAt: new Date(),
isMine: true,
chat: {
__typename: 'Chat',
id: chatId,
Expand Down
2 changes: 2 additions & 0 deletions src/components/ChatsListScreen/ChatsList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('ChatsList', () => {
id: 1,
content: 'Hello',
createdAt: new Date('1 Jan 2019 GMT'),
isMine: true,
chat: {
__typename: 'Chat',
id: 1,
Expand Down Expand Up @@ -86,6 +87,7 @@ describe('ChatsList', () => {
id: 1,
content: 'Hello',
createdAt: new Date('1 Jan 2019 GMT'),
isMine: true,
chat: {
__typename: 'Chat',
id: 1,
Expand Down
1 change: 1 addition & 0 deletions src/graphql/fragments/message.fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default gql`
id
createdAt
content
isMine
chat {
id
}
Expand Down

0 comments on commit 420318c

Please sign in to comment.