Skip to content

Commit 19f03fa

Browse files
committed
refactor: no use props.json to pass and update props for a custom message
1 parent 8cac610 commit 19f03fa

File tree

10 files changed

+80
-42
lines changed

10 files changed

+80
-42
lines changed

packages/botonic-react/src/components/custom-message.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const customMessage = ({
8282
isUnread={props.isUnread}
8383
>
8484
<ErrorBoundary key={'errorBoundary'} {...customMessageProps}>
85-
<CustomMessageComponent {...customMessageProps}>
85+
<CustomMessageComponent id={id} {...customMessageProps}>
8686
{childrenWithoutReplies}
8787
</CustomMessageComponent>
8888
</ErrorBoundary>
@@ -93,12 +93,11 @@ export const customMessage = ({
9393
WrappedComponent.customTypeName = name
9494
// eslint-disable-next-line react/display-name
9595
WrappedComponent.deserialize = (msg: any) => {
96-
const json = { ...msg.data, messageId: msg.id }
9796
return (
9897
<WrappedComponent
9998
id={msg.id}
10099
key={msg.key}
101-
json={json}
100+
json={msg.data}
102101
{...msg.data}
103102
sentBy={msg.sentBy || SENDERS.bot}
104103
isUnread={msg.isUnread}

packages/botonic-react/src/webchat/context/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export enum WebchatAction {
1313
TOGGLE_EMOJI_PICKER = 'toggleEmojiPicker',
1414
TOGGLE_PERSISTENT_MENU = 'togglePersistentMenu',
1515
TOGGLE_WEBCHAT = 'toggleWebchat',
16-
UPDATE_CUSTOM_MESSAGE_JSON = 'updateCustomMessageJSON',
16+
UPDATE_CUSTOM_MESSAGE_PROPS = 'updateCustomMessageProps',
1717
UPDATE_DEV_SETTINGS = 'updateDevSettings',
1818
UPDATE_HANDOFF = 'updateHandoff',
1919
UPDATE_LAST_MESSAGE_DATE = 'updateLastMessageDate',

packages/botonic-react/src/webchat/context/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const WebchatContext = createContext<WebchatContextProps>({
5454
togglePersistentMenu: () => {
5555
return
5656
},
57-
updateCustomMessageJSON: () => {
57+
updateCustomMessageProps: () => {
5858
return
5959
},
6060
updateLatestInput: () => {

packages/botonic-react/src/webchat/context/messages-reducer.ts

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export const messagesReducer = (
1616
return addMessageComponent(state, action)
1717
case WebchatAction.UPDATE_MESSAGE:
1818
return updateMessageReducer(state, action)
19-
case WebchatAction.UPDATE_CUSTOM_MESSAGE_JSON:
20-
return updateCustomMessageJSONReducer(state, action)
19+
case WebchatAction.UPDATE_CUSTOM_MESSAGE_PROPS:
20+
return updateCustomMessagePropsReducer(state, action)
2121
case WebchatAction.UPDATE_REPLIES:
2222
return { ...state, replies: action.payload }
2323
case WebchatAction.REMOVE_REPLIES:
@@ -155,21 +155,57 @@ function addMessageReducer(
155155
}
156156
}
157157

158-
function updateCustomMessageJSONReducer(
158+
function updateCustomMessagePropsReducer(
159159
state: WebchatState,
160160
action: { type: WebchatAction; payload?: any }
161161
) {
162-
const { messageId, json } = action.payload
163-
const messageToUpdate = state.messagesJSON.find(m => m.id === messageId)
164-
const messageInfo = {
165-
data: {
166-
json,
167-
},
162+
const { messageId, props } = action.payload
163+
164+
// Similar to updateMessageReducer but only for custom messages
165+
const msgIndex = state.messagesJSON.map(m => m.id).indexOf(messageId)
166+
if (msgIndex > -1) {
167+
const msgComponent = state.messagesComponents[msgIndex]
168+
let updatedMessageComponents = {}
169+
let updatedMessagesJSON = {}
170+
if (msgComponent) {
171+
const updatedMsgComponent = {
172+
...msgComponent,
173+
...{
174+
props: { ...msgComponent.props, ack: action.payload.ack, ...props },
175+
},
176+
}
177+
178+
updatedMessageComponents = {
179+
messagesComponents: [
180+
...state.messagesComponents.slice(0, msgIndex),
181+
{ ...updatedMsgComponent },
182+
...state.messagesComponents.slice(msgIndex + 1),
183+
],
184+
}
185+
}
186+
187+
const messageJSON = state.messagesJSON.find(m => m.id === messageId)
188+
if (messageJSON) {
189+
messageJSON.data = {
190+
...messageJSON.data,
191+
...props,
192+
}
193+
194+
updatedMessagesJSON = {
195+
messagesJSON: [
196+
...state.messagesJSON.slice(0, msgIndex),
197+
{ ...messageJSON },
198+
...state.messagesJSON.slice(msgIndex + 1),
199+
],
200+
}
201+
}
202+
203+
return {
204+
...state,
205+
...updatedMessagesJSON,
206+
...updatedMessageComponents,
207+
}
168208
}
169-
const updatedMsg = merge(messageToUpdate, messageInfo)
170209

171-
return updateMessageReducer(state, {
172-
type: WebchatAction.UPDATE_MESSAGE,
173-
payload: updatedMsg,
174-
})
210+
return state
175211
}

packages/botonic-react/src/webchat/context/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ export interface WebchatContextProps {
7575
toggleEmojiPicker: (toggle: boolean) => void
7676
togglePersistentMenu: (toggle: boolean) => void
7777
toggleCoverComponent: (toggle: boolean) => void
78-
updateCustomMessageJSON: (messageId: string, json: any) => void
78+
updateCustomMessageProps: (
79+
messageId: string,
80+
json: Record<string, any>
81+
) => void
7982
updateLatestInput: (input: ClientInput) => void
8083
updateMessage: (message: WebchatMessage) => void
8184
updateReplies: (replies: (typeof Reply)[]) => void

packages/botonic-react/src/webchat/context/use-webchat.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ export interface UseWebchat {
5555
toggleEmojiPicker: (toggle: boolean) => void
5656
togglePersistentMenu: (toggle: boolean) => void
5757
toggleWebchat: (toggle: boolean) => void
58-
updateCustomMessageJSON: (messageId: string, json: any) => void
58+
updateCustomMessageProps: (
59+
messageId: string,
60+
props: Record<string, any>
61+
) => void
5962
updateDevSettings: (settings: DevSettings) => void
6063
updateHandoff: (handoff: boolean) => void
6164
updateLastMessageDate: (date: string) => void
@@ -103,10 +106,13 @@ export function useWebchat(theme?: WebchatTheme): UseWebchat {
103106
payload: message,
104107
})
105108

106-
const updateCustomMessageJSON = (messageId: string, json: any) =>
109+
const updateCustomMessageProps = (
110+
messageId: string,
111+
props: Record<string, any>
112+
) =>
107113
webchatDispatch({
108-
type: WebchatAction.UPDATE_CUSTOM_MESSAGE_JSON,
109-
payload: { messageId, json },
114+
type: WebchatAction.UPDATE_CUSTOM_MESSAGE_PROPS,
115+
payload: { messageId, props },
110116
})
111117

112118
const updateMessage = (message: WebchatMessage) =>
@@ -267,7 +273,7 @@ export function useWebchat(theme?: WebchatTheme): UseWebchat {
267273
toggleEmojiPicker,
268274
togglePersistentMenu,
269275
toggleWebchat,
270-
updateCustomMessageJSON,
276+
updateCustomMessageProps,
271277
updateDevSettings,
272278
updateHandoff,
273279
updateLastMessageDate,
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export { default as CustomRatingMessage } from './rating'
2-
export { CustomJsonBase } from './types'

packages/botonic-react/src/webchat/custom-messages/rating/index.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { ThemeContext } from 'styled-components'
44

55
import { Button, customMessage } from '../../../components'
66
import { WebchatContext } from '../../context'
7-
import { CustomJsonBase } from '../types'
87
import { RatingSelector } from './rating-selector'
98
import { MessageBubble } from './styles'
109
import { RatingType } from './types'
@@ -14,19 +13,18 @@ interface CustomRatingMessageProps {
1413
messageText: string
1514
buttonText: string
1615
ratingType: RatingType
17-
json: CustomJsonBase & { valueSent?: number }
16+
id: string
17+
valueSent?: number
1818
}
1919

2020
const CustomRatingMessage: React.FC<CustomRatingMessageProps> = props => {
21-
const { payloads, messageText, buttonText, ratingType } = props
22-
const { updateCustomMessageJSON, sendInput } = useContext(WebchatContext)
21+
const { payloads, messageText, buttonText, ratingType, id, valueSent } = props
22+
const { updateCustomMessageProps, sendInput } = useContext(WebchatContext)
2323

2424
const theme = useContext(ThemeContext)
2525
const color = theme?.brand?.color ?? ''
2626

27-
const [ratingValue, setRatingValue] = useState(
28-
props.json?.valueSent ? props.json.valueSent : -1
29-
)
27+
const [ratingValue, setRatingValue] = useState(valueSent ? valueSent : -1)
3028
const [showRating, setShowRating] = useState(true)
3129

3230
const ratingChanged = (newRating: number) => {
@@ -36,8 +34,8 @@ const CustomRatingMessage: React.FC<CustomRatingMessageProps> = props => {
3634
const handleButtonSend = () => {
3735
if (ratingValue === -1) return
3836

39-
if (props.json?.messageId) {
40-
updateCustomMessageJSON(props.json.messageId, {
37+
if (id) {
38+
updateCustomMessageProps(id, {
4139
valueSent: ratingValue,
4240
})
4341
}
@@ -63,10 +61,10 @@ const CustomRatingMessage: React.FC<CustomRatingMessageProps> = props => {
6361
onRatingChange={ratingChanged}
6462
ratingValue={ratingValue}
6563
ratingType={ratingType}
66-
valueSent={props.json?.valueSent}
64+
valueSent={valueSent}
6765
/>
6866
</MessageBubble>
69-
{!props.json?.valueSent && showRating && (
67+
{!props?.valueSent && showRating && (
7068
<Button
7169
autodisable={true}
7270
disabled={disabled}

packages/botonic-react/src/webchat/custom-messages/types.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/botonic-react/src/webchat/webchat.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const Webchat = forwardRef<WebchatRef | null, WebchatProps>((props, ref) => {
8686
toggleEmojiPicker,
8787
togglePersistentMenu,
8888
toggleWebchat,
89-
updateCustomMessageJSON,
89+
updateCustomMessageProps,
9090
updateDevSettings,
9191
updateHandoff,
9292
updateLastMessageDate,
@@ -651,7 +651,7 @@ const Webchat = forwardRef<WebchatRef | null, WebchatProps>((props, ref) => {
651651
toggleEmojiPicker,
652652
togglePersistentMenu,
653653
toggleCoverComponent,
654-
updateCustomMessageJSON,
654+
updateCustomMessageProps,
655655
updateLatestInput,
656656
updateMessage,
657657
updateReplies,

0 commit comments

Comments
 (0)