95 lines
2.4 KiB
JavaScript
95 lines
2.4 KiB
JavaScript
import MessagingActionTypes from "./messaging.types";
|
|
|
|
const INITIAL_STATE = {
|
|
open: false,
|
|
selectedConversationId: null,
|
|
isSending: false,
|
|
error: null,
|
|
message: null,
|
|
conversations: [], // Holds the list of conversations
|
|
messages: [], // Holds the list of messages for the selected conversation
|
|
unreadCount: 0,
|
|
searchingForConversation: false
|
|
};
|
|
|
|
const messagingReducer = (state = INITIAL_STATE, action) => {
|
|
switch (action.type) {
|
|
case MessagingActionTypes.SET_MESSAGE:
|
|
return { ...state, message: action.payload };
|
|
|
|
case MessagingActionTypes.TOGGLE_CHAT_VISIBLE:
|
|
return {
|
|
...state,
|
|
open: !state.open
|
|
};
|
|
|
|
case MessagingActionTypes.OPEN_CHAT_BY_PHONE:
|
|
return {
|
|
...state,
|
|
searchingForConversation: true
|
|
};
|
|
|
|
case MessagingActionTypes.SET_SELECTED_CONVERSATION:
|
|
return {
|
|
...state,
|
|
open: true,
|
|
searchingForConversation: false,
|
|
selectedConversationId: action.payload,
|
|
messages: [] // Reset messages when a new conversation is selected
|
|
};
|
|
|
|
case MessagingActionTypes.SEND_MESSAGE:
|
|
return {
|
|
...state,
|
|
error: null,
|
|
isSending: true
|
|
};
|
|
|
|
case MessagingActionTypes.SEND_MESSAGE_SUCCESS:
|
|
return {
|
|
...state,
|
|
message: "",
|
|
isSending: false
|
|
};
|
|
|
|
case MessagingActionTypes.SEND_MESSAGE_FAILURE:
|
|
return {
|
|
...state,
|
|
error: action.payload,
|
|
isSending: false
|
|
};
|
|
|
|
case MessagingActionTypes.SET_CONVERSATIONS:
|
|
return {
|
|
...state,
|
|
conversations: action.payload
|
|
};
|
|
|
|
case MessagingActionTypes.ADD_MESSAGE:
|
|
return {
|
|
...state,
|
|
messages: [...state.messages, action.payload]
|
|
};
|
|
|
|
case MessagingActionTypes.UPDATE_UNREAD_COUNT:
|
|
console.log("SKL");
|
|
console.dir({ action, state });
|
|
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,
|
|
unreadCount: Math.max(state.unreadCount - 1, 0) // Ensure unreadCount does not go below zero
|
|
};
|
|
|
|
default:
|
|
return state;
|
|
}
|
|
};
|
|
|
|
export default messagingReducer;
|