accounts_map.js 887 B

123456789101112131415161718192021222324
  1. import { Map as ImmutableMap } from 'immutable';
  2. import { ACCOUNT_LOOKUP_FAIL } from '../actions/accounts';
  3. import { importAccounts } from '../actions/accounts_typed';
  4. import { domain } from '../initial_state';
  5. export const normalizeForLookup = str => {
  6. str = str.toLowerCase();
  7. const trailingIndex = str.indexOf(`@${domain.toLowerCase()}`);
  8. return (trailingIndex > 0) ? str.slice(0, trailingIndex) : str;
  9. };
  10. const initialState = ImmutableMap();
  11. export default function accountsMap(state = initialState, action) {
  12. switch(action.type) {
  13. case ACCOUNT_LOOKUP_FAIL:
  14. return action.error?.response?.status === 404 ? state.set(normalizeForLookup(action.acct), null) : state;
  15. case importAccounts.type:
  16. return state.withMutations(map => action.payload.accounts.forEach(account => map.set(normalizeForLookup(account.acct), account.id)));
  17. default:
  18. return state;
  19. }
  20. }