2017-12-08 21:14:35 +01:00
|
|
|
// @flow
|
2017-12-21 18:32:51 +01:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2017-12-22 02:21:22 +01:00
|
|
|
import { handleActions } from 'util/redux-utils';
|
2017-12-21 18:32:51 +01:00
|
|
|
|
|
|
|
export type Subscription = {
|
|
|
|
channelName: string,
|
|
|
|
uri: string,
|
2018-03-06 09:36:04 +01:00
|
|
|
latest: ?string,
|
2017-12-21 18:32:51 +01:00
|
|
|
};
|
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 = {
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
type HasFetchedSubscriptions = {
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.HAS_FETCHED_SUBSCRIPTIONS,
|
2017-12-13 22:36:30 +01:00
|
|
|
};
|
2017-12-08 21:14:35 +01:00
|
|
|
|
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-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-03-06 09:36:04 +01:00
|
|
|
export type Action =
|
|
|
|
| doChannelSubscribe
|
|
|
|
| doChannelUnsubscribe
|
|
|
|
| HasFetchedSubscriptions
|
|
|
|
| setSubscriptionLatest
|
|
|
|
| CheckSubscriptionStarted
|
|
|
|
| CheckSubscriptionCompleted
|
|
|
|
| Function;
|
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(
|
|
|
|
{
|
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,
|
|
|
|
};
|
|
|
|
},
|
2017-12-21 18:32:51 +01:00
|
|
|
[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,
|
|
|
|
}),
|
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
|
|
|
|
),
|
|
|
|
}),
|
2017-12-08 21:14:35 +01:00
|
|
|
},
|
|
|
|
defaultState
|
|
|
|
);
|