fix sorting change makes hidden users to appear

This commit is contained in:
Vyrtsev Mikhail
2019-06-16 17:35:56 -05:00
committed by Umputun
parent 556b053ae7
commit dad51f052f
+25 -13
View File
@@ -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<Promise<void>> => 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<Promise<void>> => 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);
}
};