2017-12-08 21:14:35 +01:00
|
|
|
// @flow
|
2017-12-21 18:32:51 +01:00
|
|
|
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 = {
|
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
|
|
|
|
2017-12-21 18:32:51 +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(
|
|
|
|
{
|
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,
|
|
|
|
}),
|
2017-12-08 21:14:35 +01:00
|
|
|
},
|
|
|
|
defaultState
|
|
|
|
);
|