Refactored Messaging as a part of BOD-14. Breaking changes remain.
This commit is contained in:
@@ -1,36 +1,25 @@
|
||||
import MessagingActionTypes from "./messaging.types";
|
||||
|
||||
export const toggleChatVisible = () => ({
|
||||
type: MessagingActionTypes.TOGGLE_CHAT_VISIBLE
|
||||
type: MessagingActionTypes.TOGGLE_CHAT_VISIBLE,
|
||||
//payload: user
|
||||
});
|
||||
|
||||
export const toggleConversationVisible = conversationId => ({
|
||||
type: MessagingActionTypes.TOGGLE_CONVERSATION_VISIBLE,
|
||||
payload: conversationId
|
||||
});
|
||||
|
||||
export const openConversation = phone => ({
|
||||
type: MessagingActionTypes.OPEN_CONVERSATION,
|
||||
payload: phone
|
||||
});
|
||||
|
||||
export const closeConversation = phone => ({
|
||||
type: MessagingActionTypes.CLOSE_CONVERSATION,
|
||||
payload: phone
|
||||
});
|
||||
|
||||
export const sendMessage = message => ({
|
||||
export const sendMessage = (message) => ({
|
||||
type: MessagingActionTypes.SEND_MESSAGE,
|
||||
payload: message
|
||||
payload: message,
|
||||
});
|
||||
|
||||
export const sendMessageSuccess = message => ({
|
||||
export const sendMessageSuccess = (message) => ({
|
||||
type: MessagingActionTypes.SEND_MESSAGE_SUCCESS,
|
||||
payload: message
|
||||
payload: message,
|
||||
});
|
||||
|
||||
export const sendMessageFailure = error => ({
|
||||
export const sendMessageFailure = (error) => ({
|
||||
type: MessagingActionTypes.SEND_MESSAGE_FAILURE,
|
||||
payload: error
|
||||
payload: error,
|
||||
});
|
||||
export const setSelectedConversation = (conversationId) => ({
|
||||
type: MessagingActionTypes.SET_SELECTED_CONVERSATION,
|
||||
payload: conversationId,
|
||||
});
|
||||
|
||||
@@ -2,23 +2,8 @@ import MessagingActionTypes from "./messaging.types";
|
||||
|
||||
const INITIAL_STATE = {
|
||||
visible: false,
|
||||
unread: 0,
|
||||
conversations: [
|
||||
// {
|
||||
// phone_num: "6049992002",
|
||||
// id: "519ba10d-6467-4fa5-9c22-59ae891edeb6",
|
||||
// open: false,
|
||||
// isSending: false,
|
||||
// sendingError: null
|
||||
// },
|
||||
// {
|
||||
// phone_num: "6049992991",
|
||||
// id: "ab57deba-eeb9-40db-b5ae-23f3ce8d7c7b",
|
||||
// open: false,
|
||||
// isSending: false,
|
||||
// sendingError: null
|
||||
// }
|
||||
],
|
||||
selectedConversationId: null,
|
||||
isSending: false,
|
||||
error: null,
|
||||
};
|
||||
|
||||
@@ -29,77 +14,24 @@ const messagingReducer = (state = INITIAL_STATE, action) => {
|
||||
...state,
|
||||
visible: !state.visible,
|
||||
};
|
||||
case MessagingActionTypes.SET_CHAT_VISIBLE:
|
||||
return {
|
||||
...state,
|
||||
visible: true,
|
||||
};
|
||||
case MessagingActionTypes.SET_SELECTED_CONVERSATION:
|
||||
return { ...state, selectedConversationId: action.payload };
|
||||
case MessagingActionTypes.SEND_MESSAGE:
|
||||
return {
|
||||
...state,
|
||||
conversations: state.conversations.map((c) =>
|
||||
c.id === action.payload.conversationid ? { ...c, isSending: true } : c
|
||||
),
|
||||
error: null,
|
||||
isSending: true,
|
||||
};
|
||||
case MessagingActionTypes.SEND_MESSAGE_SUCCESS:
|
||||
return {
|
||||
...state,
|
||||
conversations: state.conversations.map((c) =>
|
||||
c.id === action.payload.conversationid
|
||||
? { ...c, isSending: false }
|
||||
: c
|
||||
),
|
||||
isSending: false,
|
||||
};
|
||||
case MessagingActionTypes.SEND_MESSAGE_FAILURE:
|
||||
return {
|
||||
...state,
|
||||
conversations: state.conversations.map((c) =>
|
||||
c.id === action.payload.conversationid
|
||||
? { ...c, isSending: false, sendingError: action.payload.error }
|
||||
: c
|
||||
),
|
||||
error: action.payload,
|
||||
};
|
||||
case MessagingActionTypes.OPEN_CONVERSATION:
|
||||
if (
|
||||
state.conversations.find(
|
||||
(c) => c.phone_num === action.payload.phone_num
|
||||
)
|
||||
)
|
||||
return {
|
||||
...state,
|
||||
conversations: state.conversations.map((c) =>
|
||||
c.phone_num === action.payload.phone_num ? { ...c, open: true } : c
|
||||
),
|
||||
};
|
||||
else
|
||||
return {
|
||||
...state,
|
||||
conversations: [
|
||||
...state.conversations,
|
||||
{
|
||||
phone_num: action.payload.phone_num,
|
||||
id: action.payload.id,
|
||||
open: true,
|
||||
isSending: false,
|
||||
sendingError: null,
|
||||
},
|
||||
],
|
||||
};
|
||||
case MessagingActionTypes.CLOSE_CONVERSATION:
|
||||
return {
|
||||
...state,
|
||||
conversations: state.conversations.filter(
|
||||
(c) => c.phone_num !== action.payload
|
||||
),
|
||||
};
|
||||
case MessagingActionTypes.TOGGLE_CONVERSATION_VISIBLE:
|
||||
return {
|
||||
...state,
|
||||
conversations: state.conversations.map((c) =>
|
||||
c.id === action.payload ? { ...c, open: !c.open } : c
|
||||
),
|
||||
};
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,23 @@
|
||||
import { createSelector } from "reselect";
|
||||
|
||||
const selectMessaging = state => state.messaging;
|
||||
const selectMessaging = (state) => state.messaging;
|
||||
|
||||
export const selectChatVisible = createSelector(
|
||||
[selectMessaging],
|
||||
messaging => messaging.visible
|
||||
(messaging) => messaging.visible
|
||||
);
|
||||
|
||||
export const selectConversations = createSelector(
|
||||
export const selectIsSending = createSelector(
|
||||
[selectMessaging],
|
||||
messaging => messaging.conversations
|
||||
(messaging) => messaging.isSending
|
||||
);
|
||||
|
||||
export const selectError = createSelector(
|
||||
[selectMessaging],
|
||||
(messaging) => messaging.error
|
||||
);
|
||||
|
||||
export const selectSelectedConversation = createSelector(
|
||||
[selectMessaging],
|
||||
(messaging) => messaging.selectedConversationId
|
||||
);
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
const MessagingActionTypes = {
|
||||
TOGGLE_CHAT_VISIBLE: "TOGGLE_CHAT_VISIBLE",
|
||||
SET_CHAT_VISIBLE: "SET_CHAT_VISIBLE",
|
||||
OPEN_CONVERSATION: "OPEN_CONVERSATION",
|
||||
CLOSE_CONVERSATION: "CLOSE_CONVERSATION",
|
||||
TOGGLE_CONVERSATION_VISIBLE: "TOGGLE_CONVERSATION_VISIBLE",
|
||||
SEND_MESSAGE: "SEND_MESSAGE",
|
||||
SEND_MESSAGE_SUCCESS: "SEND_MESSAGE_SUCCESS",
|
||||
SEND_MESSAGE_FAILURE: "SEND_MESSAGE_FAILURE"
|
||||
SEND_MESSAGE_FAILURE: "SEND_MESSAGE_FAILURE",
|
||||
SET_SELECTED_CONVERSATION: 'SET_SELECTED_CONVERSATION'
|
||||
};
|
||||
export default MessagingActionTypes;
|
||||
|
||||
Reference in New Issue
Block a user