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

87 lines
2.9 KiB
TypeScript
Raw Normal View History

import { OrderedSet as ImmutableOrderedSet, Record as ImmutableRecord } 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,
} from 'soapbox/actions/suggestions';
import type { AnyAction } from 'redux';
import type { APIEntity } from 'soapbox/types/entities';
2020-03-27 13:59:38 -07:00
const SuggestionRecord = ImmutableRecord({
source: '',
account: '',
});
const ReducerRecord = ImmutableRecord({
items: ImmutableOrderedSet<Suggestion>(),
next: null as string | null,
2020-03-27 13:59:38 -07:00
isLoading: false,
});
type State = ReturnType<typeof ReducerRecord>;
type Suggestion = ReturnType<typeof SuggestionRecord>;
type APIEntities = Array<APIEntity>;
// Convert a v1 account into a v2 suggestion
const accountToSuggestion = (account: APIEntity) => {
2021-09-17 13:21:27 -07:00
return {
source: 'past_interactions',
account: account.id,
};
};
const importAccounts = (state: State, accounts: APIEntities) => {
2021-09-17 13:21:27 -07:00
return state.withMutations(state => {
state.set('items', ImmutableOrderedSet(accounts.map(accountToSuggestion).map(suggestion => SuggestionRecord(suggestion))));
2021-09-17 13:21:27 -07:00
state.set('isLoading', false);
});
};
const importSuggestions = (state: State, suggestions: APIEntities, next: string | null) => {
2021-09-17 13:21:27 -07:00
return state.withMutations(state => {
state.update('items', items => items.concat(suggestions.map(x => ({ ...x, account: x.account.id })).map(suggestion => SuggestionRecord(suggestion))));
2021-09-17 13:21:27 -07:00
state.set('isLoading', false);
state.set('next', next);
2021-09-17 13:21:27 -07:00
});
};
const dismissAccount = (state: State, accountId: string) => {
return state.update('items', items => items.filterNot(item => item.account === accountId));
2021-09-17 13:21:27 -07:00
};
const dismissAccounts = (state: State, accountIds: Array<string>) => {
return state.update('items', items => items.filterNot(item => accountIds.includes(item.account)));
2021-09-17 13:21:27 -07:00
};
export default function suggestionsReducer(state: State = ReducerRecord(), action: AnyAction) {
switch (action.type) {
2022-05-11 14:06:35 -07:00
case SUGGESTIONS_FETCH_REQUEST:
case SUGGESTIONS_V2_FETCH_REQUEST:
return state.set('isLoading', true);
case SUGGESTIONS_FETCH_SUCCESS:
return importAccounts(state, action.accounts);
case SUGGESTIONS_V2_FETCH_SUCCESS:
return importSuggestions(state, action.suggestions, action.next);
2022-05-11 14:06:35 -07:00
case SUGGESTIONS_FETCH_FAIL:
case SUGGESTIONS_V2_FETCH_FAIL:
return state.set('isLoading', false);
case SUGGESTIONS_DISMISS:
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);
default:
return state;
2020-03-27 13:59:38 -07:00
}
2021-08-03 12:22:51 -07:00
}