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:
Dave Richer
2024-11-18 10:11:41 -08:00
parent 27de849be7
commit ae0bfad89a
7 changed files with 1283 additions and 2297 deletions

View File

@@ -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

View File

@@ -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,

View File

@@ -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
};

View File

@@ -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;