From dad51f052f500fe2fb0d413cd0985e605936a5b6 Mon Sep 17 00:00:00 2001 From: Vyrtsev Mikhail Date: Mon, 17 Jun 2019 00:46:57 +0300 Subject: [PATCH] fix sorting change makes hidden users to appear --- frontend/app/store/sort/actions.ts | 38 ++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/frontend/app/store/sort/actions.ts b/frontend/app/store/sort/actions.ts index 8bb08fd6..6ca70e5a 100644 --- a/frontend/app/store/sort/actions.ts +++ b/frontend/app/store/sort/actions.ts @@ -1,28 +1,40 @@ -import api from '@app/common/api'; import { Sorting } from '@app/common/types'; import { COOKIE_SORT_KEY } from '@app/common/constants'; import { setCookie } from '@app/common/cookies'; import { StoreAction } from '../index'; -import { setPostInfo } from '../post_info/actions'; -import { setComments } from '../comments/actions'; +import { fetchComments } from '../comments/actions'; import { SORT_SET, SORT_SET_ACTION } from './types'; -export const setSort = (sort: Sorting): StoreAction> => async dispatch => { +function setSortCookie(sort: Sorting) { try { setCookie(COOKIE_SORT_KEY, sort, { expires: 60 * 60 * 24 * 365 }); // save sorting for a year - } catch (e) { + } catch { // can't save; ignore it } +} - const info = await api.getPostComments(sort); +export const setSort = (sort: Sorting): StoreAction> => async (dispatch, getState) => { + const originalSort = getState().sort; + setSortCookie(sort); - const action: SORT_SET_ACTION = { - type: SORT_SET, - sort, - }; + try { + const action: SORT_SET_ACTION = { + type: SORT_SET, + sort, + }; - dispatch(action); - dispatch(setPostInfo(info.info)); - dispatch(setComments(info.comments)); + await dispatch(action); + await dispatch(fetchComments(sort)); + } catch { + // restore sort in case of error, probably network error + + const action: SORT_SET_ACTION = { + type: SORT_SET, + sort: originalSort, + }; + + setSortCookie(originalSort); + await dispatch(action); + } };