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

76 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-12-08 21:14:35 +01:00
// @flow
import * as ACTIONS from 'constants/action_types';
import handleActions from 'util/redux-utils';
export type Subscription = {
channelName: string,
uri: string,
};
2017-12-08 21:14:35 +01:00
// Subscription redux types
export type SubscriptionState = {
subscriptions: Array<Subscription>,
2017-12-13 22:36:30 +01:00
hasFetchedSubscriptions: boolean,
2017-12-08 21:14:35 +01:00
};
// Subscription action types
2017-12-08 21:38:20 +01:00
type doChannelSubscribe = {
type: ACTIONS.CHANNEL_SUBSCRIBE,
2017-12-08 21:14:35 +01:00
data: Subscription,
};
2017-12-08 21:38:20 +01:00
type doChannelUnsubscribe = {
type: ACTIONS.CHANNEL_UNSUBSCRIBE,
2017-12-08 21:14:35 +01:00
data: Subscription,
};
type HasFetchedSubscriptions = {
type: ACTIONS.HAS_FETCHED_SUBSCRIPTIONS,
2017-12-13 22:36:30 +01:00
};
2017-12-08 21:14:35 +01:00
export type Action = doChannelSubscribe | doChannelUnsubscribe | HasFetchedSubscriptions;
2017-12-08 21:14:35 +01:00
export type Dispatch = (action: Action) => any;
const defaultState = {
subscriptions: [],
2017-12-13 22:36:30 +01:00
hasFetchedSubscriptions: false,
2017-12-08 21:14:35 +01:00
};
export default handleActions(
{
[ACTIONS.CHANNEL_SUBSCRIBE]: (
2017-12-08 21:14:35 +01:00
state: SubscriptionState,
2017-12-08 21:38:20 +01: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,
2017-12-08 21:38:20 +01: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
return {
...state,
subscriptions: newSubscriptions,
};
},
[ACTIONS.HAS_FETCHED_SUBSCRIPTIONS]: (state: SubscriptionState): SubscriptionState => ({
2017-12-08 21:14:35 +01:00
...state,
2017-12-13 22:36:30 +01:00
hasFetchedSubscriptions: true,
}),
2017-12-08 21:14:35 +01:00
},
defaultState
);