Files
remark42/frontend/app/store/user/reducers.ts
T
Pavel MineevandGitHub 85c71716f9 Small refactoring (#609)
* Move visibility param from redux store to local state

* use func for parsing location.search

* Remove bad wrapper

* we already have wrapper with NODE_ID on page and we shouldn't another one with the same id on page
* move common part of markup to level up

* Tinny refac of setSorting

* add dummy types for pollyfils

also, a bit rewrited way to resolve imports

* Move sort flag to local store

Because we don't need to share this param between diffrent parts of interface

* Add action creators

* move action to action creators

* Remove unused functions

* Merge in conditions in one

* One way for export API methods

* add default tags

* Rewrited changing read only mode

* removed unused function

* set sort changes

* always send sort from store
* rollback if sort don't  work

* constanst

* add typing
* shorten export
* remove double define of host
2020-03-07 12:25:18 -06:00

78 lines
1.8 KiB
TypeScript

import { User, BlockedUser } from '@app/common/types';
import {
USER_SET,
USER_BAN,
USER_UNBAN,
USER_ACTIONS,
USER_BANLIST_SET,
USER_HIDELIST_SET,
USER_HIDE,
USER_UNHIDE,
USER_SUBSCRIPTION_SET,
} from './types';
export const user = (state: User | null = null, action: USER_ACTIONS): User | null => {
switch (action.type) {
case USER_SET: {
return action.user;
}
case USER_SUBSCRIPTION_SET: {
if (state === null) {
return state;
}
return {
...state,
email_subscription: action.payload,
};
}
default:
return state;
}
};
export const bannedUsers = (state: BlockedUser[] = [], action: USER_ACTIONS): BlockedUser[] => {
switch (action.type) {
case USER_BANLIST_SET: {
return action.list;
}
case USER_BAN: {
if (state.find(u => u.id === action.user.id) !== undefined) {
return state;
}
return [action.user, ...state];
}
case USER_UNBAN: {
const index = state.findIndex(u => u.id === action.id);
if (index === -1) {
return state;
}
return [...state.slice(0, index), ...state.slice(index + 1)];
}
default:
return state;
}
};
export const hiddenUsers = (state: { [id: string]: User } = {}, action: USER_ACTIONS): { [id: string]: User } => {
switch (action.type) {
case USER_HIDELIST_SET: {
return action.payload;
}
case USER_HIDE: {
return { ...state, [action.user.id]: action.user };
}
case USER_UNHIDE: {
if (!Object.prototype.hasOwnProperty.call(state, action.id)) return state;
const newState = { ...state };
delete newState[action.id];
return newState;
}
default:
return state;
}
};
export default { user, bannedUsers, hiddenUsers };