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-10-19 22:38:07 +02:00
|
|
|
import { VIEW_ALL } from 'constants/subscriptions';
|
2017-12-22 02:21:22 +01:00
|
|
|
import { handleActions } from 'util/redux-utils';
|
2018-10-19 22:38:07 +02:00
|
|
|
import type {
|
|
|
|
SubscriptionState,
|
|
|
|
Subscription,
|
|
|
|
DoChannelSubscribe,
|
|
|
|
DoChannelUnsubscribe,
|
|
|
|
SetSubscriptionLatest,
|
|
|
|
DoUpdateSubscriptionUnreads,
|
|
|
|
DoRemoveSubscriptionUnreads,
|
|
|
|
FetchedSubscriptionsSucess,
|
|
|
|
SetViewMode,
|
|
|
|
} from 'types/subscription';
|
|
|
|
|
|
|
|
const defaultState: SubscriptionState = {
|
2017-12-08 21:14:35 +01:00
|
|
|
subscriptions: [],
|
2018-10-19 22:38:07 +02:00
|
|
|
unread: {},
|
2018-05-07 06:50:55 +02:00
|
|
|
loading: false,
|
2018-10-19 22:38:07 +02:00
|
|
|
viewMode: VIEW_ALL,
|
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,
|
2018-10-19 22:38:07 +02: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,
|
2018-10-19 22:38:07 +02: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
|
|
|
|
2018-11-02 19:33:00 +01:00
|
|
|
// Check if we need to remove it from the 'unread' state
|
2018-11-05 16:50:49 +01:00
|
|
|
const { unread } = state;
|
2018-11-02 19:33:00 +01:00
|
|
|
if (unread[subscriptionToRemove.uri]) {
|
|
|
|
delete unread[subscriptionToRemove.uri];
|
|
|
|
}
|
2017-12-08 21:14:35 +01:00
|
|
|
return {
|
|
|
|
...state,
|
2018-11-02 19:33:00 +01:00
|
|
|
...unread,
|
2017-12-08 21:14:35 +01:00
|
|
|
subscriptions: newSubscriptions,
|
|
|
|
};
|
|
|
|
},
|
2018-03-06 01:28:11 +01:00
|
|
|
[ACTIONS.SET_SUBSCRIPTION_LATEST]: (
|
|
|
|
state: SubscriptionState,
|
2018-10-19 22:38:07 +02:00
|
|
|
action: SetSubscriptionLatest
|
2018-03-06 01:28:11 +01:00
|
|
|
): 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-10-19 22:38:07 +02:00
|
|
|
[ACTIONS.UPDATE_SUBSCRIPTION_UNREADS]: (
|
2018-03-26 09:31:52 +02:00
|
|
|
state: SubscriptionState,
|
2018-10-19 22:38:07 +02:00
|
|
|
action: DoUpdateSubscriptionUnreads
|
|
|
|
): SubscriptionState => {
|
|
|
|
const { channel, uris, type } = action.data;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
unread: {
|
|
|
|
...state.unread,
|
|
|
|
[channel]: {
|
|
|
|
uris,
|
|
|
|
type,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
[ACTIONS.REMOVE_SUBSCRIPTION_UNREADS]: (
|
2018-03-26 09:31:52 +02:00
|
|
|
state: SubscriptionState,
|
2018-10-19 22:38:07 +02:00
|
|
|
action: DoRemoveSubscriptionUnreads
|
|
|
|
): SubscriptionState => {
|
|
|
|
const { channel, uris } = action.data;
|
|
|
|
const newUnread = { ...state.unread };
|
|
|
|
|
|
|
|
if (!uris) {
|
|
|
|
delete newUnread[channel];
|
|
|
|
} else {
|
|
|
|
newUnread[channel].uris = uris;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
unread: {
|
|
|
|
...newUnread,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
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,
|
2018-10-19 22:38:07 +02:00
|
|
|
action: FetchedSubscriptionsSucess
|
2018-05-07 06:50:55 +02:00
|
|
|
): SubscriptionState => ({
|
|
|
|
...state,
|
|
|
|
loading: false,
|
|
|
|
subscriptions: action.data,
|
|
|
|
}),
|
2018-10-19 22:38:07 +02:00
|
|
|
[ACTIONS.SET_VIEW_MODE]: (
|
|
|
|
state: SubscriptionState,
|
|
|
|
action: SetViewMode
|
|
|
|
): SubscriptionState => ({
|
|
|
|
...state,
|
|
|
|
viewMode: action.data,
|
|
|
|
}),
|
2017-12-08 21:14:35 +01:00
|
|
|
},
|
|
|
|
defaultState
|
|
|
|
);
|