2017-12-08 21:14:35 +01:00
|
|
|
// @flow
|
2017-12-21 18:32:51 +01:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2018-03-26 09:31:52 +02:00
|
|
|
import * as NOTIFICATION_TYPES from 'constants/notification_types';
|
2017-12-22 02:21:22 +01:00
|
|
|
import { handleActions } from 'util/redux-utils';
|
2018-05-07 06:50:55 +02:00
|
|
|
import type { Subscription } from 'types/subscription';
|
2018-10-13 19:03:09 +02:00
|
|
|
import type { Dispatch as ReduxDispatch } from 'types/redux';
|
2017-12-08 21:14:35 +01:00
|
|
|
|
2018-03-26 09:31:52 +02: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>,
|
2018-03-26 09:31:52 +02:00
|
|
|
notifications: SubscriptionNotifications,
|
2018-05-07 06:50:55 +02:00
|
|
|
loading: boolean,
|
2017-12-08 21:14:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Subscription action types
|
2017-12-08 21:38:20 +01:00
|
|
|
type doChannelSubscribe = {
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.CHANNEL_SUBSCRIBE,
|
2017-12-08 21:14:35 +01:00
|
|
|
data: Subscription,
|
|
|
|
};
|
|
|
|
|
2017-12-08 21:38:20 +01:00
|
|
|
type doChannelUnsubscribe = {
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.CHANNEL_UNSUBSCRIBE,
|
2017-12-08 21:14:35 +01:00
|
|
|
data: Subscription,
|
|
|
|
};
|
|
|
|
|
2018-03-06 01:28:11 +01:00
|
|
|
type setSubscriptionLatest = {
|
|
|
|
type: ACTIONS.SET_SUBSCRIPTION_LATEST,
|
|
|
|
data: {
|
|
|
|
subscription: Subscription,
|
2018-03-06 09:36:04 +01:00
|
|
|
uri: string,
|
|
|
|
},
|
|
|
|
};
|
2018-03-06 01:28:11 +01:00
|
|
|
|
2018-03-26 09:31:52 +02:00
|
|
|
type setSubscriptionNotification = {
|
|
|
|
type: ACTIONS.SET_SUBSCRIPTION_NOTIFICATION,
|
|
|
|
data: {
|
|
|
|
subscription: Subscription,
|
|
|
|
uri: string,
|
|
|
|
type: NotificationType,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
type setSubscriptionNotifications = {
|
|
|
|
type: ACTIONS.SET_SUBSCRIPTION_NOTIFICATIONS,
|
|
|
|
data: {
|
|
|
|
notifications: SubscriptionNotifications,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-03-06 08:44:36 +01:00
|
|
|
type CheckSubscriptionStarted = {
|
2018-03-06 09:36:04 +01:00
|
|
|
type: ACTIONS.CHECK_SUBSCRIPTION_STARTED,
|
|
|
|
};
|
2018-03-06 08:44:36 +01:00
|
|
|
|
|
|
|
type CheckSubscriptionCompleted = {
|
2018-03-06 09:36:04 +01:00
|
|
|
type: ACTIONS.CHECK_SUBSCRIPTION_COMPLETED,
|
|
|
|
};
|
2018-03-06 08:44:36 +01:00
|
|
|
|
2018-05-07 06:50:55 +02:00
|
|
|
type fetchedSubscriptionsSucess = {
|
|
|
|
type: ACTIONS.FETCH_SUBSCRIPTIONS_SUCCESS,
|
|
|
|
data: Array<Subscription>,
|
|
|
|
};
|
|
|
|
|
2018-03-06 09:36:04 +01:00
|
|
|
export type Action =
|
|
|
|
| doChannelSubscribe
|
|
|
|
| doChannelUnsubscribe
|
|
|
|
| setSubscriptionLatest
|
2018-03-26 09:31:52 +02:00
|
|
|
| setSubscriptionNotification
|
2018-03-06 09:36:04 +01:00
|
|
|
| CheckSubscriptionStarted
|
|
|
|
| CheckSubscriptionCompleted
|
|
|
|
| Function;
|
2018-10-13 19:03:09 +02:00
|
|
|
export type Dispatch = ReduxDispatch<Action>;
|
2017-12-08 21:14:35 +01:00
|
|
|
|
|
|
|
const defaultState = {
|
|
|
|
subscriptions: [],
|
2018-03-26 09:31:52 +02:00
|
|
|
notifications: {},
|
2018-05-07 06:50:55 +02:00
|
|
|
loading: false,
|
2017-12-08 21:14:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default handleActions(
|
|
|
|
{
|
2017-12-21 18:32:51 +01:00
|
|
|
[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,
|
|
|
|
};
|
|
|
|
},
|
2017-12-21 18:32:51 +01:00
|
|
|
[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()
|
2017-12-21 18:32:51 +01:00
|
|
|
.filter(subscription => subscription.channelName !== subscriptionToRemove.channelName);
|
2017-12-08 21:14:35 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
subscriptions: newSubscriptions,
|
|
|
|
};
|
|
|
|
},
|
2018-03-06 01:28:11 +01:00
|
|
|
[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
|
|
|
|
),
|
|
|
|
}),
|
2018-03-26 09:31:52 +02:00
|
|
|
[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,
|
|
|
|
}),
|
2018-05-07 06:50:55 +02:00
|
|
|
[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
|
|
|
|
);
|