From 2c22db517b32f32f60f3f42a5c36f6ee75de8f96 Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 16 Jul 2020 12:27:14 -0500 Subject: [PATCH] feat: user state --- src/client/store.js | 4 +++- src/common/state/user.js | 47 ++++++++++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/client/store.js b/src/client/store.js index 8f5b8cb..b8f4260 100644 --- a/src/client/store.js +++ b/src/client/store.js @@ -1,10 +1,12 @@ import merge from 'deepmerge'; import {combineReducers} from 'redux'; -import createCommonStore from '~/common/store'; +import chat from '~/common/state/chat'; import user from '~/common/state/user'; +import createCommonStore from '~/common/store'; const reducer = combineReducers({ + chat, user, }); diff --git a/src/common/state/user.js b/src/common/state/user.js index 35aa239..f312655 100644 --- a/src/common/state/user.js +++ b/src/common/state/user.js @@ -8,19 +8,19 @@ import hydration from './hydration'; export const userSelector = (state) => state.user; -export const friendsSelector = createSelector( +export const friendshipSelector = createSelector( userSelector, - (user) => user.friends, + (user) => user.friendship, ); -export const activeFriendsSelector = createSelector( - friendsSelector, - (friends) => Object.values(friends).filter(({state}) => 'active' === state), +export const activeFriendshipSelector = createSelector( + friendshipSelector, + (friendship) => Object.values(friendship).filter(({state}) => 'active' === state), ); -export const pendingFriendsSelector = createSelector( - friendsSelector, - (friends) => Object.values(friends).filter(({state}) => 'pending' === state), +export const pendingFriendshipSelector = createSelector( + friendshipSelector, + (friendship) => Object.values(friendship).filter(({state}) => 'pending' === state), ); export const redditUsernameSelector = createSelector( @@ -31,19 +31,34 @@ export const redditUsernameSelector = createSelector( const slice = createSlice({ name: 'user', initialState: { - favorites: {}, - friends: {}, + blocked: [], + favorites: [], + friendship: [], 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) => {}, + addFriendship: (state, {payload: {friendship}}) => { + state.friendship.push(friendship); + }, + addToFavorites: (state, {payload: {favorite}}) => { + state.favorites.push(favorite); + }, + blockUser: (state, {payload: {blocked}}) => { + state.blocked.push(blocked); + }, + removeFriendship: (state, {payload: {id}}) => { + state.friendship.splice(state.friendship.findIndex((friendship) => friendship.id === id), 1); + }, + removeFromFavorites: (state, {payload: {type, name}}) => { + state.favorites.splice(state.favorites.findIndex( + (favorite) => favorite.name === name && favorite.type === type, + ), 1); + }, + updateFriendshipStatus: (state, {payload: {id, status}}) => { + state.friendship.find((friendship) => friendship.id === id).status = status; + }, }, });