Skip to content

Commit 48a1cde

Browse files
committed
refactor: create helpers functions to get updated messagesComponents and messagesJSON inside reducer
1 parent 19f03fa commit 48a1cde

File tree

2 files changed

+60
-54
lines changed

2 files changed

+60
-54
lines changed

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

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import merge from 'lodash.merge'
2-
31
import { SENDERS } from '../../index-types'
4-
import { isCustom } from '../../message-utils'
52
import { WebchatAction } from './actions'
63
import { WebchatState } from './types'
74

@@ -94,45 +91,40 @@ function updateMessageReducer(
9491
state: WebchatState,
9592
action: { type: WebchatAction; payload?: any }
9693
) {
97-
const msgIndex = state.messagesJSON.map(m => m.id).indexOf(action.payload.id)
94+
const messageId = action.payload.id
95+
const msgIndex = state.messagesJSON.map(m => m.id).indexOf(messageId)
9896
if (msgIndex > -1) {
9997
const msgComponent = state.messagesComponents[msgIndex]
100-
let updatedMessageComponents = {}
10198
if (msgComponent) {
102-
const updatedMsgComponent = {
103-
...msgComponent,
104-
...{
105-
props: { ...msgComponent.props, ack: action.payload.ack },
106-
},
99+
msgComponent.props = {
100+
...msgComponent.props,
101+
ack: action.payload.ack,
107102
}
103+
}
108104

109-
if (isCustom(action.payload)) {
110-
// If the message is a custom message, update the json property to update props.
111-
// If user close and open the chat the message will render again with the new props.
112-
updatedMsgComponent.props.json = action.payload.data.json
113-
}
105+
const updatedMessagesComponents = msgComponent
106+
? getUpdatedMessagesComponents(state, msgIndex, msgComponent)
107+
: state.messagesComponents
114108

115-
updatedMessageComponents = {
116-
messagesComponents: [
117-
...state.messagesComponents.slice(0, msgIndex),
118-
{ ...updatedMsgComponent },
119-
...state.messagesComponents.slice(msgIndex + 1),
120-
],
109+
const messageJSON = state.messagesJSON.find(m => m.id === messageId)
110+
if (messageJSON) {
111+
messageJSON.data = {
112+
...messageJSON.data,
121113
}
122114
}
123115

116+
const updatedMessagesJSON = messageJSON
117+
? getUpdatedMessagesJSON(state, msgIndex, action.payload)
118+
: state.messagesJSON
119+
124120
const numUnreadMessages = state.messagesComponents.filter(
125121
messageComponent => messageComponent.props.isUnread
126122
).length
127123

128124
return {
129125
...state,
130-
messagesJSON: [
131-
...state.messagesJSON.slice(0, msgIndex),
132-
{ ...action.payload },
133-
...state.messagesJSON.slice(msgIndex + 1),
134-
],
135-
...updatedMessageComponents,
126+
messagesJSON: updatedMessagesJSON,
127+
messagesComponents: updatedMessagesComponents,
136128
numUnreadMessages,
137129
}
138130
}
@@ -161,51 +153,65 @@ function updateCustomMessagePropsReducer(
161153
) {
162154
const { messageId, props } = action.payload
163155

164-
// Similar to updateMessageReducer but only for custom messages
156+
// Similar to updateMessageReducer but only for custom messages when update props
165157
const msgIndex = state.messagesJSON.map(m => m.id).indexOf(messageId)
166158
if (msgIndex > -1) {
167159
const msgComponent = state.messagesComponents[msgIndex]
168-
let updatedMessageComponents = {}
169-
let updatedMessagesJSON = {}
170160
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-
],
161+
msgComponent.props = {
162+
...msgComponent.props,
163+
ack: action.payload.ack,
164+
...props,
184165
}
185166
}
186167

168+
const updatedMessagesComponents = msgComponent
169+
? getUpdatedMessagesComponents(state, msgIndex, msgComponent)
170+
: state.messagesComponents
171+
187172
const messageJSON = state.messagesJSON.find(m => m.id === messageId)
188173
if (messageJSON) {
189174
messageJSON.data = {
190175
...messageJSON.data,
191176
...props,
192177
}
193-
194-
updatedMessagesJSON = {
195-
messagesJSON: [
196-
...state.messagesJSON.slice(0, msgIndex),
197-
{ ...messageJSON },
198-
...state.messagesJSON.slice(msgIndex + 1),
199-
],
200-
}
201178
}
202179

180+
const updatedMessagesJSON = messageJSON
181+
? getUpdatedMessagesJSON(state, msgIndex, messageJSON)
182+
: state.messagesJSON
183+
203184
return {
204185
...state,
205-
...updatedMessagesJSON,
206-
...updatedMessageComponents,
186+
messagesJSON: updatedMessagesJSON,
187+
messagesComponents: updatedMessagesComponents,
207188
}
208189
}
209190

210191
return state
211192
}
193+
194+
// Helper functions to update messagesComponents and messagesJSON
195+
function getUpdatedMessagesComponents(
196+
state: WebchatState,
197+
msgIndex: number,
198+
updatedMessageComponent: any
199+
) {
200+
return [
201+
...state.messagesComponents.slice(0, msgIndex),
202+
{ ...updatedMessageComponent },
203+
...state.messagesComponents.slice(msgIndex + 1),
204+
]
205+
}
206+
207+
function getUpdatedMessagesJSON(
208+
state: WebchatState,
209+
msgIndex: number,
210+
messageJSON: any
211+
) {
212+
return [
213+
...state.messagesJSON.slice(0, msgIndex),
214+
{ ...messageJSON },
215+
...state.messagesJSON.slice(msgIndex + 1),
216+
]
217+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface CustomRatingMessageProps {
1313
messageText: string
1414
buttonText: string
1515
ratingType: RatingType
16-
id: string
16+
id?: string // This id not exist in local development
1717
valueSent?: number
1818
}
1919

0 commit comments

Comments
 (0)