Refactored Messaging as a part of BOD-14. Breaking changes remain.

This commit is contained in:
Patrick Fic
2020-04-29 16:46:23 -07:00
parent 0bc8d95120
commit dcfcf71ca4
24 changed files with 393 additions and 549 deletions

View File

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