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

169 lines
4.3 KiB
JavaScript
Raw Normal View History

2017-12-08 21:14:35 +01:00
// @flow
import * as ACTIONS from 'constants/action_types';
import * as NOTIFICATION_TYPES from 'constants/notification_types';
2017-12-22 02:21:22 +01:00
import { handleActions } from 'util/redux-utils';
import type { Subscription } from 'types/subscription';
import type { Dispatch as ReduxDispatch } from 'types/redux';
2017-12-08 21:14:35 +01:00
export type NotificationType =
| NOTIFICATION_TYPES.DOWNLOADING
| NOTIFICATION_TYPES.DOWNLOADED
| NOTIFICATION_TYPES.NOTIFY_ONLY;
export type SubscriptionNotifications = {
[string]: {
subscription: Subscription,
type: NotificationType,
},
};
2017-12-08 21:14:35 +01:00
// Subscription redux types
export type SubscriptionState = {
subscriptions: Array<Subscription>,
notifications: SubscriptionNotifications,
loading: 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 setSubscriptionLatest = {
type: ACTIONS.SET_SUBSCRIPTION_LATEST,
data: {
subscription: Subscription,
2018-03-06 09:36:04 +01:00
uri: string,
},
};
type setSubscriptionNotification = {
type: ACTIONS.SET_SUBSCRIPTION_NOTIFICATION,
data: {
subscription: Subscription,
uri: string,
type: NotificationType,
},
};
type setSubscriptionNotifications = {
type: ACTIONS.SET_SUBSCRIPTION_NOTIFICATIONS,
data: {
notifications: SubscriptionNotifications,
},
};
type CheckSubscriptionStarted = {
2018-03-06 09:36:04 +01:00
type: ACTIONS.CHECK_SUBSCRIPTION_STARTED,
};
type CheckSubscriptionCompleted = {
2018-03-06 09:36:04 +01:00
type: ACTIONS.CHECK_SUBSCRIPTION_COMPLETED,
};
type fetchedSubscriptionsSucess = {
type: ACTIONS.FETCH_SUBSCRIPTIONS_SUCCESS,
data: Array<Subscription>,
};
2018-03-06 09:36:04 +01:00
export type Action =
| doChannelSubscribe
| doChannelUnsubscribe
| setSubscriptionLatest
| setSubscriptionNotification
2018-03-06 09:36:04 +01:00
| CheckSubscriptionStarted
| CheckSubscriptionCompleted
| Function;
export type Dispatch = ReduxDispatch<Action>;
2017-12-08 21:14:35 +01:00
const defaultState = {
subscriptions: [],
notifications: {},
loading: 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.SET_SUBSCRIPTION_LATEST]: (
state: SubscriptionState,
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
),
}),
[ACTIONS.SET_SUBSCRIPTION_NOTIFICATION]: (
state: SubscriptionState,
action: setSubscriptionNotification
): SubscriptionState => ({
...state,
notifications: {
...state.notifications,
[action.data.uri]: { subscription: action.data.subscription, type: action.data.type },
},
}),
[ACTIONS.SET_SUBSCRIPTION_NOTIFICATIONS]: (
state: SubscriptionState,
action: setSubscriptionNotifications
): SubscriptionState => ({
...state,
notifications: action.data.notifications,
}),
[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,
action: fetchedSubscriptionsSucess
): SubscriptionState => ({
...state,
loading: false,
subscriptions: action.data,
}),
2017-12-08 21:14:35 +01:00
},
defaultState
);