12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- function chatReducer(state = {}, { type, chat_id, title, createdAt, lastModified, avatar, members, messages, msg_id, msg_text, msg_createdAt, msg_owner, msg_media, msg_replies }) {
- if (type === 'ADD_CHAT') {
- return {
- ...state,
- [chat_id]: {
- title,
- createdAt,
- lastModified,
- avatar,
- messages,
- members
- }
- }
- }
- if (type === "EDIT_CHAT") {
- return {
- ...state,
- [chat_id]: {
- title: title || state[chat_id].title,
- createdAt: state[chat_id].createdAt,
- lastModified: state[chat_id].lastModified,
- avatar: avatar || state[chat_id].avatar,
- messages: state[chat_id].messages,
- members: members || state[chat_id].members
- }
- }
- }
- if (type === "ADD_MESSAGE") {
- if ((state[chat_id]?.messages?.filter(msg => msg._id === msg_id).length === 0)) {
- return {
- ...state,
- [chat_id]: {
- title: state[chat_id].title,
- createdAt: state[chat_id].createdAt,
- lastModified: msg_createdAt,
- avatar: state[chat_id].avatar,
- messages: [...state[chat_id].messages, { _id: msg_id, text: msg_text, createdAt: msg_createdAt, owner: msg_owner, media: msg_media, replies: msg_replies }],
- members: state[chat_id].members
- }
- }
- }
- }
- if (type === "EDIT_MESSAGE") {
- let order;
- for (let key in state[chat_id].messages) {
- if (state[chat_id].messages[key]._id === messages[0]._id) {
- order = key
- }
- }
- return {
- ...state,
- [chat_id]: {
- title: state[chat_id].title,
- createdAt: state[chat_id].createdAt,
- lastModified,
- avatar: state[chat_id].avatar,
- messages: [
- ...state[chat_id].messages,
- state[chat_id].messages[order].text = msg_text,
- state[chat_id].messages[order].modified = true
- ],
- members: state[chat_id].members
- }
- }
- }
- return state
- }
- export default chatReducer
|