environments/review-change-ent-6o2wvy/deployments/3528
parent
ad1718b5f9
commit
75dbeb65b6
@ -0,0 +1,13 @@
|
||||
import { useAppSelector } from './useAppSelector';
|
||||
|
||||
function useLoggedIn() {
|
||||
const me = useAppSelector(state => state.me);
|
||||
return {
|
||||
isLoggedIn: typeof me === 'string',
|
||||
isLoginLoading: me === null,
|
||||
isLoginFailed: me === false,
|
||||
me,
|
||||
};
|
||||
}
|
||||
|
||||
export { useLoggedIn };
|
@ -1,37 +0,0 @@
|
||||
import { Map as ImmutableMap } from 'immutable';
|
||||
|
||||
import reducer from '../accounts-counters';
|
||||
// import { ACCOUNT_FOLLOW_SUCCESS, ACCOUNT_UNFOLLOW_SUCCESS } from 'soapbox/actions/accounts';
|
||||
// import relationship from 'soapbox/__fixtures__/relationship.json';
|
||||
// import accounts_counter_initial from 'soapbox/__fixtures__/accounts_counter_initial.json';
|
||||
// import accounts_counter_unfollow from 'soapbox/__fixtures__/accounts_counter_unfollow.json';
|
||||
// import accounts_counter_follow from 'soapbox/__fixtures__/accounts_counter_follow.json';
|
||||
|
||||
describe('accounts_counters reducer', () => {
|
||||
it('should return the initial state', () => {
|
||||
expect(reducer(undefined, {} as any)).toEqual(ImmutableMap());
|
||||
});
|
||||
|
||||
// it('should handle ACCOUNT_FOLLOW_SUCCESS', () => {
|
||||
// const state = ImmutableList([accounts_counter_initial]);
|
||||
// const action = {
|
||||
// type: ACCOUNT_FOLLOW_SUCCESS,
|
||||
// relationship: relationship,
|
||||
// alreadyFollowing: false,
|
||||
// };
|
||||
// expect(reducer(state, action)).toEqual(
|
||||
// ImmutableList([ accounts_counter_follow ]));
|
||||
// });
|
||||
//
|
||||
// it('should handle ACCOUNT_UNFOLLOW_SUCCESS', () => {
|
||||
// const state = ImmutableList([accounts_counter_initial]);
|
||||
// const action = {
|
||||
// type: ACCOUNT_UNFOLLOW_SUCCESS,
|
||||
// relationship: relationship,
|
||||
// alreadyFollowing: true,
|
||||
// };
|
||||
// expect(reducer(state, action)).toEqual(
|
||||
// ImmutableList([accounts_counter_unfollow]));
|
||||
// });
|
||||
|
||||
});
|
@ -1,64 +0,0 @@
|
||||
import { List as ImmutableList, Map as ImmutableMap, Record as ImmutableRecord } from 'immutable';
|
||||
|
||||
import {
|
||||
ACCOUNT_FOLLOW_SUCCESS,
|
||||
ACCOUNT_UNFOLLOW_SUCCESS,
|
||||
} from 'soapbox/actions/accounts';
|
||||
import { ACCOUNT_IMPORT, ACCOUNTS_IMPORT } from 'soapbox/actions/importer';
|
||||
import { STREAMING_FOLLOW_RELATIONSHIPS_UPDATE } from 'soapbox/actions/streaming';
|
||||
|
||||
import type { AnyAction } from 'redux';
|
||||
import type { APIEntity } from 'soapbox/types/entities';
|
||||
|
||||
const CounterRecord = ImmutableRecord({
|
||||
followers_count: 0,
|
||||
following_count: 0,
|
||||
statuses_count: 0,
|
||||
});
|
||||
|
||||
type Counter = ReturnType<typeof CounterRecord>;
|
||||
type State = ImmutableMap<string, Counter>;
|
||||
type APIEntities = Array<APIEntity>;
|
||||
|
||||
const normalizeAccount = (state: State, account: APIEntity) => state.set(account.id, CounterRecord({
|
||||
followers_count: account.followers_count,
|
||||
following_count: account.following_count,
|
||||
statuses_count: account.statuses_count,
|
||||
}));
|
||||
|
||||
const normalizeAccounts = (state: State, accounts: ImmutableList<APIEntities>) => {
|
||||
accounts.forEach(account => {
|
||||
state = normalizeAccount(state, account);
|
||||
});
|
||||
|
||||
return state;
|
||||
};
|
||||
|
||||
const updateFollowCounters = (state: State, counterUpdates: APIEntities) => {
|
||||
return state.withMutations(state => {
|
||||
counterUpdates.forEach((counterUpdate) => {
|
||||
state.update(counterUpdate.id, CounterRecord(), counters => counters.merge({
|
||||
followers_count: counterUpdate.follower_count,
|
||||
following_count: counterUpdate.following_count,
|
||||
}));
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export default function accountsCounters(state: State = ImmutableMap<string, Counter>(), action: AnyAction) {
|
||||
switch (action.type) {
|
||||
case ACCOUNT_IMPORT:
|
||||
return normalizeAccount(state, action.account);
|
||||
case ACCOUNTS_IMPORT:
|
||||
return normalizeAccounts(state, action.accounts);
|
||||
case ACCOUNT_FOLLOW_SUCCESS:
|
||||
return action.alreadyFollowing ? state :
|
||||
state.updateIn([action.relationship.id, 'followers_count'], 0, (count) => typeof count === 'number' ? count + 1 : 0);
|
||||
case ACCOUNT_UNFOLLOW_SUCCESS:
|
||||
return state.updateIn([action.relationship.id, 'followers_count'], 0, (count) => typeof count === 'number' ? Math.max(0, count - 1) : 0);
|
||||
case STREAMING_FOLLOW_RELATIONSHIPS_UPDATE:
|
||||
return updateFollowCounters(state, [action.follower, action.following]);
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue