feat: statez

This commit is contained in:
cha0s 2020-07-16 00:23:06 -05:00
parent df22c68672
commit e6f5cb6a92
2 changed files with 77 additions and 2 deletions

71
src/common/state/chat.js Normal file
View File

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

View File

@ -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) => {},
},
});