bigbuffet-rw/app/soapbox/reducers/suggestions.js

74 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-01-10 14:01:24 -08:00
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
2022-01-10 14:01:24 -08:00
import { ACCOUNT_BLOCK_SUCCESS, ACCOUNT_MUTE_SUCCESS } from 'soapbox/actions/accounts';
import { DOMAIN_BLOCK_SUCCESS } from 'soapbox/actions/domain_blocks';
2020-03-27 13:59:38 -07:00
import {
SUGGESTIONS_FETCH_REQUEST,
SUGGESTIONS_FETCH_SUCCESS,
SUGGESTIONS_FETCH_FAIL,
SUGGESTIONS_DISMISS,
2021-09-17 13:21:27 -07:00
SUGGESTIONS_V2_FETCH_REQUEST,
SUGGESTIONS_V2_FETCH_SUCCESS,
SUGGESTIONS_V2_FETCH_FAIL,
2021-09-17 13:38:38 -07:00
} from '../actions/suggestions';
2020-03-27 13:59:38 -07:00
const initialState = ImmutableMap({
items: ImmutableList(),
isLoading: false,
});
2021-09-17 13:21:27 -07:00
// Convert a v1 account into a v2 suggestion
const accountToSuggestion = account => {
return {
source: 'past_interactions',
account: account.id,
};
};
const importAccounts = (state, accounts) => {
return state.withMutations(state => {
state.set('items', fromJS(accounts.map(accountToSuggestion)));
state.set('isLoading', false);
});
};
const importSuggestions = (state, suggestions) => {
return state.withMutations(state => {
state.set('items', fromJS(suggestions.map(x => ({ ...x, account: x.account.id }))));
state.set('isLoading', false);
});
};
const dismissAccount = (state, accountId) => {
2021-10-02 13:18:27 -07:00
return state.update('items', items => items.filterNot(item => item.get('account') === accountId));
2021-09-17 13:21:27 -07:00
};
const dismissAccounts = (state, accountIds) => {
2021-10-02 13:18:27 -07:00
return state.update('items', items => items.filterNot(item => accountIds.includes(item.get('account'))));
2021-09-17 13:21:27 -07:00
};
2020-03-27 13:59:38 -07:00
export default function suggestionsReducer(state = initialState, action) {
switch(action.type) {
case SUGGESTIONS_FETCH_REQUEST:
2021-09-17 13:21:27 -07:00
case SUGGESTIONS_V2_FETCH_REQUEST:
2020-03-27 13:59:38 -07:00
return state.set('isLoading', true);
case SUGGESTIONS_FETCH_SUCCESS:
2021-09-17 13:21:27 -07:00
return importAccounts(state, action.accounts);
case SUGGESTIONS_V2_FETCH_SUCCESS:
return importSuggestions(state, action.suggestions);
2020-03-27 13:59:38 -07:00
case SUGGESTIONS_FETCH_FAIL:
2021-09-17 13:21:27 -07:00
case SUGGESTIONS_V2_FETCH_FAIL:
2020-03-27 13:59:38 -07:00
return state.set('isLoading', false);
case SUGGESTIONS_DISMISS:
2021-09-17 13:21:27 -07:00
return dismissAccount(state, action.id);
case ACCOUNT_BLOCK_SUCCESS:
case ACCOUNT_MUTE_SUCCESS:
return dismissAccount(state, action.relationship.id);
case DOMAIN_BLOCK_SUCCESS:
return dismissAccounts(state, action.accounts);
2020-03-27 13:59:38 -07:00
default:
return state;
}
2021-08-03 12:22:51 -07:00
}