feature/IO-3000-Migrate-MSG-to-Sockets - Add Conversation, merge master, packages
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -76,6 +76,11 @@ const useSocket = (bodyshop) => {
|
||||
dispatch({ type: "ADD_MESSAGE", payload: message });
|
||||
};
|
||||
|
||||
const handleNewConversation = (data) => {
|
||||
dispatch({ type: "ADD_CONVERSATION", payload: data.conversation });
|
||||
dispatch({ type: "ADD_MESSAGE", payload: data.message });
|
||||
};
|
||||
|
||||
const handleReadUpdated = ({ conversationId }) => {
|
||||
dispatch({ type: "UPDATE_UNREAD_COUNT", payload: conversationId });
|
||||
};
|
||||
@@ -88,6 +93,7 @@ const useSocket = (bodyshop) => {
|
||||
socketInstance.on("connect_error", handleConnectionError);
|
||||
socketInstance.on("disconnect", handleDisconnect);
|
||||
socketInstance.on("bodyshop-message", handleBodyshopMessage);
|
||||
socketInstance.on("new-conversation", handleNewConversation);
|
||||
}
|
||||
} else {
|
||||
// User is not authenticated
|
||||
|
||||
@@ -50,6 +50,12 @@ export const addMessage = (message) => ({
|
||||
payload: message
|
||||
});
|
||||
|
||||
// Add a Conversation to the list of conversations
|
||||
export const addConversation = (conversation) => ({
|
||||
type: "ADD_CONVERSATION",
|
||||
payload: conversation
|
||||
});
|
||||
|
||||
// Update unread count for a conversation (e.g., after marking messages as read)
|
||||
export const updateUnreadCount = (conversationId) => ({
|
||||
type: MessagingActionTypes.UPDATE_UNREAD_COUNT,
|
||||
|
||||
@@ -71,18 +71,23 @@ const messagingReducer = (state = INITIAL_STATE, action) => {
|
||||
messages: [...state.messages, action.payload]
|
||||
};
|
||||
|
||||
case MessagingActionTypes.UPDATE_UNREAD_COUNT:
|
||||
console.log("SKL");
|
||||
console.dir({ action, state });
|
||||
case MessagingActionTypes.ADD_CONVERSATION:
|
||||
return {
|
||||
...state,
|
||||
conversations: Array.isArray(state.conversations)
|
||||
? state.conversations.map((conversation) =>
|
||||
conversation.id === action.payload
|
||||
? { ...conversation, unreadCount: 0 } // Reset unread count for the selected conversation
|
||||
: conversation
|
||||
)
|
||||
: state.conversations,
|
||||
conversations: [...state.conversations, action.payload]
|
||||
};
|
||||
|
||||
case MessagingActionTypes.UPDATE_UNREAD_COUNT:
|
||||
return {
|
||||
...state,
|
||||
conversations: {
|
||||
...state.conversations,
|
||||
conversations: state.conversations.conversations.map((conversation) =>
|
||||
conversation.id === action.payload
|
||||
? { ...conversation, unreadcnt: 0 } // Reset unread count for the selected conversation
|
||||
: conversation
|
||||
)
|
||||
},
|
||||
unreadCount: Math.max(state.unreadCount - 1, 0) // Ensure unreadCount does not go below zero
|
||||
};
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ const MessagingActionTypes = {
|
||||
SET_MESSAGE: "SET_MESSAGE",
|
||||
ADD_MESSAGE: "ADD_MESSAGE",
|
||||
UPDATE_UNREAD_COUNT: "UPDATE_UNREAD_COUNT",
|
||||
SET_CONVERSATIONS: "SET_CONVERSATIONS"
|
||||
SET_CONVERSATIONS: "SET_CONVERSATIONS",
|
||||
ADD_CONVERSATION: "ADD_CONVERSATION"
|
||||
};
|
||||
export default MessagingActionTypes;
|
||||
|
||||
Reference in New Issue
Block a user