lbry-desktop/src/renderer/redux/reducers/subscriptions.js

134 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-12-08 21:14:35 +01:00
// @flow
import * as ACTIONS from 'constants/action_types';
2018-10-19 22:38:07 +02:00
import { VIEW_ALL } from 'constants/subscriptions';
2017-12-22 02:21:22 +01:00
import { handleActions } from 'util/redux-utils';
2018-10-19 22:38:07 +02:00
import type {
SubscriptionState,
Subscription,
DoChannelSubscribe,
DoChannelUnsubscribe,
SetSubscriptionLatest,
DoUpdateSubscriptionUnreads,
DoRemoveSubscriptionUnreads,
FetchedSubscriptionsSucess,
SetViewMode,
} from 'types/subscription';
const defaultState: SubscriptionState = {
2017-12-08 21:14:35 +01:00
subscriptions: [],
2018-10-19 22:38:07 +02:00
unread: {},
loading: false,
2018-10-19 22:38:07 +02:00
viewMode: VIEW_ALL,
2017-12-08 21:14:35 +01:00
};
export default handleActions(
{
[ACTIONS.CHANNEL_SUBSCRIBE]: (
2017-12-08 21:14:35 +01:00
state: SubscriptionState,
2018-10-19 22:38:07 +02:00
action: DoChannelSubscribe
2017-12-08 21:14:35 +01:00
): SubscriptionState => {
const newSubscription: Subscription = action.data;
2017-12-13 22:36:30 +01:00
const newSubscriptions: Array<Subscription> = state.subscriptions.slice();
2017-12-08 21:14:35 +01:00
newSubscriptions.unshift(newSubscription);
return {
...state,
subscriptions: newSubscriptions,
};
},
[ACTIONS.CHANNEL_UNSUBSCRIBE]: (
2017-12-08 21:14:35 +01:00
state: SubscriptionState,
2018-10-19 22:38:07 +02:00
action: DoChannelUnsubscribe
2017-12-08 21:14:35 +01:00
): SubscriptionState => {
const subscriptionToRemove: Subscription = action.data;
const newSubscriptions = state.subscriptions
.slice()
.filter(subscription => subscription.channelName !== subscriptionToRemove.channelName);
2017-12-08 21:14:35 +01:00
// Check if we need to remove it from the 'unread' state
2018-11-05 16:50:49 +01:00
const { unread } = state;
if (unread[subscriptionToRemove.uri]) {
delete unread[subscriptionToRemove.uri];
}
2017-12-08 21:14:35 +01:00
return {
...state,
...unread,
2017-12-08 21:14:35 +01:00
subscriptions: newSubscriptions,
};
},
[ACTIONS.SET_SUBSCRIPTION_LATEST]: (
state: SubscriptionState,
2018-10-19 22:38:07 +02:00
action: SetSubscriptionLatest
): SubscriptionState => ({
...state,
2018-03-06 09:36:04 +01:00
subscriptions: state.subscriptions.map(
subscription =>
subscription.channelName === action.data.subscription.channelName
? { ...subscription, latest: action.data.uri }
: subscription
),
}),
2018-10-19 22:38:07 +02:00
[ACTIONS.UPDATE_SUBSCRIPTION_UNREADS]: (
state: SubscriptionState,
2018-10-19 22:38:07 +02:00
action: DoUpdateSubscriptionUnreads
): SubscriptionState => {
const { channel, uris, type } = action.data;
return {
...state,
unread: {
...state.unread,
[channel]: {
uris,
type,
},
},
};
},
[ACTIONS.REMOVE_SUBSCRIPTION_UNREADS]: (
state: SubscriptionState,
2018-10-19 22:38:07 +02:00
action: DoRemoveSubscriptionUnreads
): SubscriptionState => {
const { channel, uris } = action.data;
const newUnread = { ...state.unread };
if (!uris) {
delete newUnread[channel];
} else {
newUnread[channel].uris = uris;
}
return {
...state,
unread: {
...newUnread,
},
};
},
[ACTIONS.FETCH_SUBSCRIPTIONS_START]: (state: SubscriptionState): SubscriptionState => ({
...state,
loading: true,
}),
[ACTIONS.FETCH_SUBSCRIPTIONS_FAIL]: (state: SubscriptionState): SubscriptionState => ({
...state,
loading: false,
}),
[ACTIONS.FETCH_SUBSCRIPTIONS_SUCCESS]: (
state: SubscriptionState,
2018-10-19 22:38:07 +02:00
action: FetchedSubscriptionsSucess
): SubscriptionState => ({
...state,
loading: false,
subscriptions: action.data,
}),
2018-10-19 22:38:07 +02:00
[ACTIONS.SET_VIEW_MODE]: (
state: SubscriptionState,
action: SetViewMode
): SubscriptionState => ({
...state,
viewMode: action.data,
}),
2017-12-08 21:14:35 +01:00
},
defaultState
);