Spaces:
Paused
Paused
File size: 4,565 Bytes
a0fda44 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
import { createSlice, current } from "@reduxjs/toolkit";
const initialState = {
// Manages typing or recording state in order to make state changes for local user
mode: null,
// Manages chat room being displayed or not
active: false,
// saves chat room currently being displayed
currentChatRoom: { chatProfile: {}, messageHistory: [] },
// Saves all chat rooms
chatHistory: {},
};
const chatSlice = createSlice({
name: "chat",
initialState,
reducers: {
setMode: (state, { payload }) => {
state.mode = payload.mode;
},
resetMode: (state) => {
state.mode = null;
},
setChatActive: (state) => {
state.active = true;
},
setChatUnactive: (state) => {
state.active = false;
},
setChatRoom: (state, { payload }) => {
state.currentChatRoom = payload.chatRoom;
},
addToChatRoomHistory: (state, { payload }) => {
state.chatHistory[payload.chatRoomId] = payload.chatRoom;
},
removeChatRoom: (state, { payload }) => {
const currentState = current(state);
if (currentState.currentChatRoom._id === payload.chatRoomId) {
state.currentChatRoom = { chatProfile: {}, messageHistory: {} };
}
delete state.chatHistory[payload.chatRoomId];
},
updateChatProfile: (state, { payload: { payload } }) => {
const currentChatRoom = current(state.currentChatRoom);
// If user is currently displayed in chatRoom, update online status
if (payload.id === currentChatRoom.chatProfile._id) {
state.currentChatRoom.chatProfile = {
...currentChatRoom.chatProfile,
...payload,
};
}
// Update user online status in chat history
state.chatHistory[currentChatRoom._id] = {
...currentChatRoom,
chatProfile: {
...currentChatRoom.chatProfile,
...payload,
},
};
},
setChatProfileMode: (state, { payload }) => {
// Update mode in currentChatRoom
const currentChatRoom = current(state.currentChatRoom);
if (payload.id === currentChatRoom.chatProfile._id) {
state.currentChatRoom.chatProfile.mode = payload.mode;
}
},
updateMessageHistory: (state, { payload: { chatRoomId, message } }) => {
// Get chatHistory
const chatHistory = current(state.chatHistory);
const currentChatRoom = current(state.currentChatRoom);
// Get chat room
const chatRoom = chatHistory[chatRoomId];
if (!chatRoom) return;
// Get last chatRoom day message
const lastDayMessage =
chatRoom.messageHistory[chatRoom.messageHistory.length - 1];
// Get day message was sent
const dayString = new Date(message.timeSent).toLocaleString("en-US", {
month: "long",
day: "2-digit",
year: "numeric",
});
const day = new Date(dayString).getTime();
// Check if day is today
if (lastDayMessage?.day === day) {
// Add to object if day is today
state.chatHistory[chatRoomId].messageHistory[
chatRoom.messageHistory.length - 1
].messages.push(message);
} else {
// Else create new object for day
const newDayObject = {
day,
messages: [message],
};
state.chatHistory[chatRoomId].messageHistory.push(newDayObject);
}
if (currentChatRoom._id === chatRoomId) {
state.currentChatRoom.messageHistory = current(
state.chatHistory[chatRoomId].messageHistory
);
}
},
updateMessageStatus: (
state,
{ payload: { chatRoomId, messageId, day, status } }
) => {
const messageHistory = current(
state.chatHistory[chatRoomId].messageHistory
);
const dayMessagesIndex = messageHistory.findIndex(
(dayMessage) => dayMessage.day === day
);
const messageIndex = messageHistory[dayMessagesIndex].messages.findIndex(
(message) => message._id === messageId
);
// Update status
state.chatHistory[chatRoomId].messageHistory[dayMessagesIndex].messages[
messageIndex
][status] = true;
// If room is the currentChatRoom
const currentChatRoom = current(state.currentChatRoom);
if (currentChatRoom._id === chatRoomId)
state.currentChatRoom.messageHistory[dayMessagesIndex].messages[
messageIndex
][status] = true;
},
resetChat: (state) => initialState,
},
});
export const chatActions = chatSlice.actions;
export default chatSlice.reducer;
|