Suggestions: consolidate reducers
This commit is contained in:
parent
8cfc2f9d2d
commit
c92de334e8
|
@ -10,8 +10,8 @@ import Account from './components/account';
|
||||||
import Button from 'soapbox/components/button';
|
import Button from 'soapbox/components/button';
|
||||||
|
|
||||||
const mapStateToProps = state => ({
|
const mapStateToProps = state => ({
|
||||||
suggestions: state.getIn(['suggestions_v2', 'items']),
|
suggestions: state.getIn(['suggestions', 'items']),
|
||||||
isLoading: state.getIn(['suggestions_v2', 'isLoading']),
|
isLoading: state.getIn(['suggestions', 'isLoading']),
|
||||||
});
|
});
|
||||||
|
|
||||||
export default @connect(mapStateToProps)
|
export default @connect(mapStateToProps)
|
||||||
|
|
|
@ -43,10 +43,10 @@ class WhoToFollowPanel extends ImmutablePureComponent {
|
||||||
</div>
|
</div>
|
||||||
<div className='wtf-panel__content'>
|
<div className='wtf-panel__content'>
|
||||||
<div className='wtf-panel__list'>
|
<div className='wtf-panel__list'>
|
||||||
{suggestions && suggestions.map(accountId => (
|
{suggestions && suggestions.map(suggestion => (
|
||||||
<AccountContainer
|
<AccountContainer
|
||||||
key={accountId}
|
key={suggestion.get('account')}
|
||||||
id={accountId}
|
id={suggestion.get('account')}
|
||||||
actionIcon='times'
|
actionIcon='times'
|
||||||
actionTitle={intl.formatMessage(messages.dismissSuggestion)}
|
actionTitle={intl.formatMessage(messages.dismissSuggestion)}
|
||||||
onActionClick={dismissSuggestion}
|
onActionClick={dismissSuggestion}
|
||||||
|
|
|
@ -31,7 +31,6 @@ import listAdder from './list_adder';
|
||||||
import filters from './filters';
|
import filters from './filters';
|
||||||
import conversations from './conversations';
|
import conversations from './conversations';
|
||||||
import suggestions from './suggestions';
|
import suggestions from './suggestions';
|
||||||
import suggestions_v2 from './suggestions_v2';
|
|
||||||
import polls from './polls';
|
import polls from './polls';
|
||||||
import identity_proofs from './identity_proofs';
|
import identity_proofs from './identity_proofs';
|
||||||
import trends from './trends';
|
import trends from './trends';
|
||||||
|
@ -89,7 +88,6 @@ const appReducer = combineReducers({
|
||||||
filters,
|
filters,
|
||||||
conversations,
|
conversations,
|
||||||
suggestions,
|
suggestions,
|
||||||
suggestions_v2,
|
|
||||||
polls,
|
polls,
|
||||||
trends,
|
trends,
|
||||||
groups,
|
groups,
|
||||||
|
|
|
@ -4,6 +4,13 @@ import {
|
||||||
SUGGESTIONS_FETCH_FAIL,
|
SUGGESTIONS_FETCH_FAIL,
|
||||||
SUGGESTIONS_DISMISS,
|
SUGGESTIONS_DISMISS,
|
||||||
} from '../actions/suggestions';
|
} from '../actions/suggestions';
|
||||||
|
import {
|
||||||
|
SUGGESTIONS_V2_FETCH_REQUEST,
|
||||||
|
SUGGESTIONS_V2_FETCH_SUCCESS,
|
||||||
|
SUGGESTIONS_V2_FETCH_FAIL,
|
||||||
|
} from '../actions/suggestions_v2';
|
||||||
|
import { ACCOUNT_BLOCK_SUCCESS, ACCOUNT_MUTE_SUCCESS } from 'soapbox/actions/accounts';
|
||||||
|
import { DOMAIN_BLOCK_SUCCESS } from 'soapbox/actions/domain_blocks';
|
||||||
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
|
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
|
||||||
|
|
||||||
const initialState = ImmutableMap({
|
const initialState = ImmutableMap({
|
||||||
|
@ -11,19 +18,55 @@ const initialState = ImmutableMap({
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 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) => {
|
||||||
|
return state.update('items', list => list.filterNot(x => x.account === accountId));
|
||||||
|
};
|
||||||
|
|
||||||
|
const dismissAccounts = (state, accountIds) => {
|
||||||
|
return state.update('items', list => list.filterNot(x => accountIds.includes(x.account)));
|
||||||
|
};
|
||||||
|
|
||||||
export default function suggestionsReducer(state = initialState, action) {
|
export default function suggestionsReducer(state = initialState, action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case SUGGESTIONS_FETCH_REQUEST:
|
case SUGGESTIONS_FETCH_REQUEST:
|
||||||
|
case SUGGESTIONS_V2_FETCH_REQUEST:
|
||||||
return state.set('isLoading', true);
|
return state.set('isLoading', true);
|
||||||
case SUGGESTIONS_FETCH_SUCCESS:
|
case SUGGESTIONS_FETCH_SUCCESS:
|
||||||
return state.withMutations(map => {
|
return importAccounts(state, action.accounts);
|
||||||
map.set('items', fromJS(action.accounts.map(x => x.id)));
|
case SUGGESTIONS_V2_FETCH_SUCCESS:
|
||||||
map.set('isLoading', false);
|
return importSuggestions(state, action.suggestions);
|
||||||
});
|
|
||||||
case SUGGESTIONS_FETCH_FAIL:
|
case SUGGESTIONS_FETCH_FAIL:
|
||||||
|
case SUGGESTIONS_V2_FETCH_FAIL:
|
||||||
return state.set('isLoading', false);
|
return state.set('isLoading', false);
|
||||||
case SUGGESTIONS_DISMISS:
|
case SUGGESTIONS_DISMISS:
|
||||||
return state.update('items', list => list.filterNot(id => id === action.id));
|
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:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,37 +0,0 @@
|
||||||
import {
|
|
||||||
SUGGESTIONS_V2_FETCH_REQUEST,
|
|
||||||
SUGGESTIONS_V2_FETCH_SUCCESS,
|
|
||||||
SUGGESTIONS_V2_FETCH_FAIL,
|
|
||||||
} from '../actions/suggestions_v2';
|
|
||||||
import { SUGGESTIONS_DISMISS } from '../actions/suggestions';
|
|
||||||
import { ACCOUNT_BLOCK_SUCCESS, ACCOUNT_MUTE_SUCCESS } from 'soapbox/actions/accounts';
|
|
||||||
import { DOMAIN_BLOCK_SUCCESS } from 'soapbox/actions/domain_blocks';
|
|
||||||
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
|
|
||||||
|
|
||||||
const initialState = ImmutableMap({
|
|
||||||
items: ImmutableList(),
|
|
||||||
isLoading: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
export default function suggestionsReducer(state = initialState, action) {
|
|
||||||
switch(action.type) {
|
|
||||||
case SUGGESTIONS_V2_FETCH_REQUEST:
|
|
||||||
return state.set('isLoading', true);
|
|
||||||
case SUGGESTIONS_V2_FETCH_SUCCESS:
|
|
||||||
return state.withMutations(map => {
|
|
||||||
map.set('items', fromJS(action.suggestions.map(x => ({ ...x, account: x.account.id }))));
|
|
||||||
map.set('isLoading', false);
|
|
||||||
});
|
|
||||||
case SUGGESTIONS_V2_FETCH_FAIL:
|
|
||||||
return state.set('isLoading', false);
|
|
||||||
case SUGGESTIONS_DISMISS:
|
|
||||||
return state.update('items', list => list.filterNot(x => x.account === action.id));
|
|
||||||
case ACCOUNT_BLOCK_SUCCESS:
|
|
||||||
case ACCOUNT_MUTE_SUCCESS:
|
|
||||||
return state.update('items', list => list.filterNot(x => x.account === action.relationship.id));
|
|
||||||
case DOMAIN_BLOCK_SUCCESS:
|
|
||||||
return state.update('items', list => list.filterNot(x => action.accounts.includes(x.account)));
|
|
||||||
default:
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue