diff --git a/src/common/state/chat.js b/src/common/state/chat.js new file mode 100644 index 0000000..bbb31ba --- /dev/null +++ b/src/common/state/chat.js @@ -0,0 +1,71 @@ +/* eslint-disable no-param-reassign */ +import { + createSelector, + createSlice, +} from '@reduxjs/toolkit'; + +import hydration from './hydration'; + +export const userSelector = (state) => state.user; + +const slice = createSlice({ + name: 'chat', + initialState: { + channels: {}, + focus: '', + messages: {}, + recent: {}, + unread: {}, + users: {}, + ...(hydration('chat') || {}), + }, + reducers: { + addMessage: (state, {payload: {channel, message}}) => { + state.messages.push(message); + state.channels[channel].messages.push(message.uuid); + if (state.focus !== channel) { + state.unread[channel] += 1; + } + }, + editMessage: (state, {payload: {uuid, content}}) => { + state.messages[uuid] = content; + }, + focus: (state, {payload: {channel}}) => { + state.unread[channel] = 0; + }, + join: (state, {payload: {channel, messages, users}}) => { + state.channels[channel] = {messages, users}; + state.unread[channel] = 0; + }, + joined: (state, {payload: {channel, id}}) => { + state.channels[channel].users.push(id); + }, + leave: (state, {payload: {channel}}) => { + delete state.channels[channel]; + delete state.unread[channel]; + }, + left: (state, {payload: {channel, id}}) => { + const {users} = state.channels[channel]; + users.splice(users.indexOf(id), 1); + }, + removeMessage: (state, {payload: {channel, uuid}}) => { + delete state.messages[uuid]; + const {messages} = state.channels[channel]; + messages.splice(messages.indexOf(uuid), 1); + }, + removeRecent: (state, {payload: {channel}}) => { + delete state.recent[channel]; + }, + }, +}); + +export const { + addMessage, + editMessage, + join, + leave, + removeMessage, + removeRecent, +} = slice.actions; + +export default slice.reducer; diff --git a/src/common/state/user.js b/src/common/state/user.js index 8be46bc..35aa239 100644 --- a/src/common/state/user.js +++ b/src/common/state/user.js @@ -30,15 +30,19 @@ export const redditUsernameSelector = createSelector( const slice = createSlice({ name: 'user', - initialState: hydration('user') || { + initialState: { + favorites: {}, friends: {}, - isAnonymous: true, + isAnonymous: false, redditUsername: 'anonymous', + ...(hydration('user') || {isAnonymous: true}), }, reducers: { addFriend: (state, action) => {}, + addToFavorites: (state, action) => {}, blockUser: (state, action) => {}, removeFriend: (state, action) => {}, + removeFromFavorites: (state, action) => {}, updateFriendshipStatus: (state, action) => {}, }, });