suggestions.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
  2. import { blockAccountSuccess, muteAccountSuccess } from 'mastodon/actions/accounts';
  3. import { blockDomainSuccess } from 'mastodon/actions/domain_blocks';
  4. import {
  5. SUGGESTIONS_FETCH_REQUEST,
  6. SUGGESTIONS_FETCH_SUCCESS,
  7. SUGGESTIONS_FETCH_FAIL,
  8. SUGGESTIONS_DISMISS,
  9. } from '../actions/suggestions';
  10. const initialState = ImmutableMap({
  11. items: ImmutableList(),
  12. isLoading: false,
  13. });
  14. export default function suggestionsReducer(state = initialState, action) {
  15. switch(action.type) {
  16. case SUGGESTIONS_FETCH_REQUEST:
  17. return state.set('isLoading', true);
  18. case SUGGESTIONS_FETCH_SUCCESS:
  19. return state.withMutations(map => {
  20. map.set('items', fromJS(action.suggestions.map(x => ({ ...x, account: x.account.id }))));
  21. map.set('isLoading', false);
  22. });
  23. case SUGGESTIONS_FETCH_FAIL:
  24. return state.set('isLoading', false);
  25. case SUGGESTIONS_DISMISS:
  26. return state.update('items', list => list.filterNot(x => x.account === action.id));
  27. case blockAccountSuccess.type:
  28. case muteAccountSuccess.type:
  29. return state.update('items', list => list.filterNot(x => x.account === action.payload.relationship.id));
  30. case blockDomainSuccess.type:
  31. return state.update('items', list => list.filterNot(x => action.payload.accounts.includes(x.account)));
  32. default:
  33. return state;
  34. }
  35. }