-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(botonic-react): create FeedbackMessage component to add thumbsUp…
… and thumbsDown in the footer of the message
- Loading branch information
Showing
9 changed files
with
242 additions
and
36 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/botonic-react/src/components/message/message-feedback.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React, { useContext, useEffect, useState } from 'react' | ||
import { v4 as uuid } from 'uuid' | ||
|
||
import ThumbsDown from '../../assets/thumbs-down.svg' | ||
import ThumbsUp from '../../assets/thumbs-up.svg' | ||
import { RequestContext, WebchatContext } from '../../contexts' | ||
import { ActionRequest } from '../../index-types' | ||
import { resolveImage } from '../../util' | ||
import { EventAction, FeedbackOption } from '../../webchat/tracking' | ||
import { FeedbackButton, FeedbackMessageContainer } from './styles' | ||
|
||
interface ButtonsState { | ||
positive: boolean | ||
negative: boolean | ||
} | ||
|
||
interface RatingProps { | ||
inferenceid?: string | ||
messageId: string | ||
} | ||
|
||
export const MessageFeedback = ({ inferenceid, messageId }: RatingProps) => { | ||
const { webchatState, updateMessage, trackEvent } = useContext(WebchatContext) | ||
const request = useContext(RequestContext) | ||
|
||
const [className, setClassName] = useState('') | ||
const [disabled, setDisabled] = useState<ButtonsState>({ | ||
positive: false, | ||
negative: false, | ||
}) | ||
|
||
const updateMsgWithFeedback = (withfeedback: boolean) => { | ||
const message = webchatState.messagesJSON.find( | ||
message => message.id === messageId | ||
) | ||
const updatedMsg = { | ||
...message, | ||
withfeedback, | ||
} | ||
updateMessage(updatedMsg) | ||
} | ||
|
||
useEffect(() => { | ||
updateMsgWithFeedback(true) | ||
}, []) | ||
|
||
useEffect(() => { | ||
if (disabled.positive || disabled.negative) { | ||
setClassName('clicked') | ||
updateMsgWithFeedback(false) | ||
} | ||
}, [disabled]) | ||
|
||
const handleClick = async (isUseful: boolean) => { | ||
if (!trackEvent) { | ||
return | ||
} | ||
|
||
if (isUseful) { | ||
setDisabled({ positive: false, negative: true }) | ||
} else { | ||
setDisabled({ positive: true, negative: false }) | ||
} | ||
|
||
const args = { | ||
knowledgebaseInferenceId: inferenceid, | ||
feedbackTargetId: messageId, | ||
feedbackGroupId: uuid(), | ||
possibleOptions: [FeedbackOption.ThumbsUp, FeedbackOption.ThumbsDown], | ||
possibleValues: [0, 1], | ||
option: isUseful ? FeedbackOption.ThumbsUp : FeedbackOption.ThumbsDown, | ||
value: isUseful ? 1 : 0, | ||
} | ||
await trackEvent( | ||
request as ActionRequest, | ||
EventAction.FeedbackKnowledgebase, | ||
args | ||
) | ||
} | ||
|
||
return ( | ||
<FeedbackMessageContainer> | ||
<FeedbackButton | ||
className={className} | ||
disabled={disabled.positive} | ||
onClick={() => handleClick(true)} | ||
> | ||
<img src={resolveImage(ThumbsUp)} /> | ||
</FeedbackButton> | ||
<FeedbackButton | ||
className={className} | ||
disabled={disabled.negative} | ||
onClick={() => handleClick(false)} | ||
> | ||
<img src={resolveImage(ThumbsDown)} /> | ||
</FeedbackButton> | ||
</FeedbackMessageContainer> | ||
) | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/botonic-react/src/components/message/message-footer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React, { useContext } from 'react' | ||
|
||
import { WebchatContext } from '../../contexts' | ||
import { SENDERS } from '../../index-types' | ||
import { MessageFeedback } from './message-feedback' | ||
import { MessageFooterContainer } from './styles' | ||
import { MessageTimestamp, resolveMessageTimestamps } from './timestamps' | ||
|
||
interface MessageFooterProps { | ||
enabletimestamps: boolean | ||
messageJSON: any | ||
sentBy: SENDERS | ||
withfeedback: boolean | ||
inferenceid?: string | ||
} | ||
|
||
export const MessageFooter = ({ | ||
enabletimestamps, | ||
messageJSON, | ||
sentBy, | ||
withfeedback, | ||
inferenceid, | ||
}: MessageFooterProps) => { | ||
const { getThemeProperty } = useContext(WebchatContext) | ||
|
||
const { timestampsEnabled, timestampStyle } = resolveMessageTimestamps( | ||
getThemeProperty, | ||
enabletimestamps | ||
) | ||
const isSentByUser = sentBy === SENDERS.user | ||
const messageFotterClass = isSentByUser | ||
? 'message-footer-user' | ||
: 'message-footer-bot' | ||
|
||
return ( | ||
<MessageFooterContainer | ||
className={messageFotterClass} | ||
isSentByUser={isSentByUser} | ||
> | ||
{timestampsEnabled ? ( | ||
<MessageTimestamp | ||
sentBy={sentBy} | ||
style={timestampStyle} | ||
timestamp={messageJSON.timestamp} | ||
/> | ||
) : null} | ||
{withfeedback ? ( | ||
<MessageFeedback inferenceid={inferenceid} messageId={messageJSON.id} /> | ||
) : null} | ||
</MessageFooterContainer> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export enum EventAction { | ||
FeedbackKnowledgebase = 'feedback_knowledgebase', | ||
} | ||
|
||
export enum FeedbackOption { | ||
ThumbsUp = 'thumbsUp', | ||
ThumbsDown = 'thumbsDown', | ||
} |