Skip to content

Commit d8b205f

Browse files
author
Martín Callegari
authored
Merge pull request #263 from Wolox/issue-212-profile-avatar-user
Profile avatar
2 parents 015477e + 7a009e0 commit d8b205f

File tree

9 files changed

+47
-10
lines changed

9 files changed

+47
-10
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export default App;
150150
|**subtitle**|string|NO|'This is your chat subtitle'|Subtitle of the widget|
151151
|**senderPlaceHolder**|string|NO|'Type a message...'|The placeholder of the message input|
152152
|**profileAvatar**|string|NO| |The profile image that will be set on the responses|
153+
|**profileClientAvatar**|string|NO| |The profile image that will be set on the client messages|
153154
|**titleAvatar**|string|NO| |The picture image that will be shown next to the chat title|
154155
|**showCloseButton**|boolean|NO|false|Show or hide the close button in full screen mode|
155156
|**fullScreenMode**|boolean|NO|false|Allow the use of full screen in full desktop mode|

src/components/Widget/components/Conversation/components/Messages/components/Message/styles.scss

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
display: flex;
77
white-space: pre-wrap;
88
word-wrap: break-word;
9+
10+
&-client {
11+
flex-direction: row-reverse;
12+
}
913
}
1014

1115
.rcw-timestamp {
@@ -57,4 +61,8 @@
5761
height: 40px;
5862
border-radius: 100%;
5963
margin-right: 10px;
64+
65+
&-client {
66+
margin: 0 0 0 10px;
67+
}
6068
}

src/components/Widget/components/Conversation/components/Messages/index.tsx

+14-5
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ import format from 'date-fns/format';
55
import { scrollToBottom } from '../../../../../../utils/messages';
66
import { MessageTypes, Link, CustomCompMessage, GlobalState } from '../../../../../../store/types';
77
import { setBadgeCount, markAllMessagesRead } from '../../../../../../store/actions';
8+
import { MESSAGE_SENDER } from '../../../../../../constants';
89

910
import Loader from './components/Loader';
1011
import './styles.scss';
1112

1213
type Props = {
1314
showTimeStamp: boolean,
1415
profileAvatar?: string;
16+
profileClientAvatar?: string;
1517
}
1618

17-
function Messages({ profileAvatar, showTimeStamp }: Props) {
19+
function Messages({ profileAvatar, profileClientAvatar, showTimeStamp }: Props) {
1820
const dispatch = useDispatch();
1921
const { messages, typing, showChat, badgeCount } = useSelector((state: GlobalState) => ({
2022
messages: state.messages.messages,
@@ -47,13 +49,20 @@ function Messages({ profileAvatar, showTimeStamp }: Props) {
4749
// }
4850
// }
4951

52+
const isClient = (sender) => sender === MESSAGE_SENDER.CLIENT;
53+
5054
return (
5155
<div id="messages" className="rcw-messages-container" ref={messageRef}>
5256
{messages?.map((message, index) =>
53-
<div className="rcw-message" key={`${index}-${format(message.timestamp, 'hh:mm')}`}>
54-
{profileAvatar &&
55-
message.showAvatar &&
56-
<img src={profileAvatar} className="rcw-avatar" alt="profile" />
57+
<div className={`rcw-message ${isClient(message.sender) ? 'rcw-message-client' : ''}`}
58+
key={`${index}-${format(message.timestamp, 'hh:mm')}`}>
59+
{((profileAvatar && !isClient(message.sender)) || (profileClientAvatar && isClient(message.sender))) &&
60+
message.showAvatar &&
61+
<img
62+
src={isClient(message.sender) ? profileClientAvatar : profileAvatar}
63+
className={`rcw-avatar ${isClient(message.sender) ? 'rcw-avatar-client' : ''}`}
64+
alt="profile"
65+
/>
5766
}
5867
{getComponentToRender(message)}
5968
</div>

src/components/Widget/components/Conversation/index.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Props = {
2626
sendMessage: AnyFunction;
2727
toggleChat: AnyFunction;
2828
profileAvatar?: string;
29+
profileClientAvatar?: string;
2930
titleAvatar?: string;
3031
onQuickButtonClicked?: AnyFunction;
3132
onTextInputChange?: (event: any) => void;
@@ -46,6 +47,7 @@ function Conversation({
4647
sendMessage,
4748
toggleChat,
4849
profileAvatar,
50+
profileClientAvatar,
4951
titleAvatar,
5052
onQuickButtonClicked,
5153
onTextInputChange,
@@ -112,7 +114,11 @@ function Conversation({
112114
showCloseButton={showCloseButton}
113115
titleAvatar={titleAvatar}
114116
/>
115-
<Messages profileAvatar={profileAvatar} showTimeStamp={showTimeStamp} />
117+
<Messages
118+
profileAvatar={profileAvatar}
119+
profileClientAvatar={profileClientAvatar}
120+
showTimeStamp={showTimeStamp}
121+
/>
116122
<QuickButtons onQuickButtonClicked={onQuickButtonClicked} />
117123
{emojis && pickerStatus && (<Picker
118124
style={{ position: 'absolute', bottom: pickerOffset, left: '0', width: '100%' }}

src/components/Widget/index.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Props = {
1212
subtitle: string;
1313
senderPlaceHolder: string;
1414
profileAvatar?: string;
15+
profileClientAvatar?: string;
1516
showCloseButton: boolean;
1617
fullScreenMode: boolean;
1718
autofocus: boolean;
@@ -41,6 +42,7 @@ function Widget({
4142
subtitle,
4243
senderPlaceHolder,
4344
profileAvatar,
45+
profileClientAvatar,
4446
showCloseButton,
4547
fullScreenMode,
4648
autofocus,
@@ -95,6 +97,7 @@ function Widget({
9597
subtitle={subtitle}
9698
senderPlaceHolder={senderPlaceHolder}
9799
profileAvatar={profileAvatar}
100+
profileClientAvatar={profileClientAvatar}
98101
showCloseButton={showCloseButton}
99102
fullScreenMode={fullScreenMode}
100103
autofocus={autofocus}

src/components/Widget/layout.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Props = {
2121
senderPlaceHolder: string;
2222
onQuickButtonClicked: AnyFunction;
2323
profileAvatar?: string;
24+
profileClientAvatar?: string;
2425
showCloseButton: boolean;
2526
fullScreenMode: boolean;
2627
autofocus: boolean;
@@ -49,6 +50,7 @@ function WidgetLayout({
4950
senderPlaceHolder,
5051
onQuickButtonClicked,
5152
profileAvatar,
53+
profileClientAvatar,
5254
showCloseButton,
5355
fullScreenMode,
5456
autofocus,
@@ -132,6 +134,7 @@ function WidgetLayout({
132134
sendMessage={onSendMessage}
133135
senderPlaceHolder={senderPlaceHolder}
134136
profileAvatar={profileAvatar}
137+
profileClientAvatar={profileClientAvatar}
135138
toggleChat={onToggleConversation}
136139
showCloseButton={showCloseButton}
137140
disabledInput={dissableInput}

src/index.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Props = {
1717
fullScreenMode?: boolean;
1818
autofocus?: boolean;
1919
profileAvatar?: string;
20+
profileClientAvatar?: string;
2021
launcher?: AnyFunction;
2122
handleTextInputChange?: (event: any) => void;
2223
chatId?: string;
@@ -44,6 +45,7 @@ function ConnectedWidget({
4445
fullScreenMode,
4546
autofocus,
4647
profileAvatar,
48+
profileClientAvatar,
4749
launcher,
4850
handleNewUserMessage,
4951
handleQuickButtonClicked,
@@ -73,6 +75,7 @@ function ConnectedWidget({
7375
handleQuickButtonClicked={handleQuickButtonClicked}
7476
senderPlaceHolder={senderPlaceHolder}
7577
profileAvatar={profileAvatar}
78+
profileClientAvatar={profileClientAvatar}
7679
showCloseButton={showCloseButton}
7780
fullScreenMode={fullScreenMode}
7881
autofocus={autofocus}

src/store/reducers/messagesReducer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const initialState = {
2222
};
2323

2424
const messagesReducer = {
25-
[ADD_NEW_USER_MESSAGE]: (state: MessagesState, { text, id }) =>
26-
({ ...state, messages: [...state.messages, createNewMessage(text, MESSAGE_SENDER.CLIENT, id)] }),
25+
[ADD_NEW_USER_MESSAGE]: (state: MessagesState, { text, showClientAvatar, id }) =>
26+
({ ...state, messages: [...state.messages, createNewMessage(text, MESSAGE_SENDER.CLIENT, id)]}),
2727

2828
[ADD_NEW_RESPONSE_MESSAGE]: (state: MessagesState, { text, id }) =>
2929
({ ...state, messages: [...state.messages, createNewMessage(text, MESSAGE_SENDER.RESPONSE, id)], badgeCount: state.badgeCount + 1 }),

src/utils/messages.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ import QuickButton from '../components/Widget/components/Conversation/components
88

99
import { MESSAGES_TYPES, MESSAGE_SENDER, MESSAGE_BOX_SCROLL_DURATION } from '../constants';
1010

11-
export function createNewMessage(text: string, sender: string, id?: string): MessageI {
11+
export function createNewMessage(
12+
text: string,
13+
sender: string,
14+
id?: string,
15+
): MessageI {
1216
return {
1317
type: MESSAGES_TYPES.TEXT,
1418
component: Message,
1519
text,
1620
sender,
1721
timestamp: new Date(),
18-
showAvatar: sender === MESSAGE_SENDER.RESPONSE,
22+
showAvatar: true,
1923
customId: id,
2024
unread: sender === MESSAGE_SENDER.RESPONSE
2125
};

0 commit comments

Comments
 (0)