From 5431f80e84bd40a38c849a3f7ce4cb5522d3d72d Mon Sep 17 00:00:00 2001 From: Misha Vyrtsev Date: Wed, 24 Jul 2019 00:45:52 +0300 Subject: [PATCH] fix unblocking after page refresh stalled (#384) --- frontend/app/store/comments/utils.ts | 2 +- frontend/app/store/user/actions.ts | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/frontend/app/store/comments/utils.ts b/frontend/app/store/comments/utils.ts index 689add16..590f04b1 100644 --- a/frontend/app/store/comments/utils.ts +++ b/frontend/app/store/comments/utils.ts @@ -62,7 +62,7 @@ export function filterTree(tree: Node[], fn: (node: Node) => boolean): Node[] { * Traverses through tree and applies function to comment on which function passed. * Note that function must not mutate comment */ -function mapTree(tree: Node[], fn: (c: Comment) => Comment): Node[] { +export function mapTree(tree: Node[], fn: (c: Comment) => Comment): Node[] { return tree.map(node => { const clone: Node = { comment: fn(node.comment), diff --git a/frontend/app/store/user/actions.ts b/frontend/app/store/user/actions.ts index f71921c9..834b968c 100644 --- a/frontend/app/store/user/actions.ts +++ b/frontend/app/store/user/actions.ts @@ -17,7 +17,7 @@ import { SETTINGS_VISIBLE_SET, } from './types'; import { setComments, unsetCommentMode } from '../comments/actions'; -import { setUserVerified as uSetUserVerified, filterTree } from '../comments/utils'; +import { setUserVerified as uSetUserVerified, filterTree, mapTree } from '../comments/utils'; import { IS_STORAGE_AVAILABLE, LS_HIDDEN_USERS_KEY } from '@app/common/constants'; import { getItem } from '@app/common/local-storage'; import { Dispatch } from 'redux'; @@ -74,12 +74,18 @@ export const blockUser = ( }); }; -export const unblockUser = (id: User['id']): StoreAction> => async dispatch => { +export const unblockUser = (id: User['id']): StoreAction> => async (dispatch, getState) => { await api.unblockUser(id); + const comments = mapTree(getState().comments, c => { + if (c.user.id !== id) return c; + if (!c.user.block) return c; + return { ...c, user: { ...c.user, block: false } }; + }); dispatch({ type: USER_UNBAN, id, }); + dispatch(setComments(comments)); }; export const fetchHiddenUsers = (): StoreAction => dispatch => {