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

175 lines
5.1 KiB
JavaScript
Raw Normal View History

2017-12-08 21:14:35 +01:00
// @flow
import * as ACTIONS from 'constants/action_types';
2019-09-26 18:07:11 +02:00
import { parseURI, ACTIONS as LBRY_REDUX_ACTIONS } from 'lbry-redux';
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
const defaultState: SubscriptionState = {
2017-12-08 21:14:35 +01:00
subscriptions: [],
2018-10-19 22:38:07 +02:00
unread: {},
2018-11-21 22:20:55 +01:00
suggested: {},
loading: false,
2018-10-19 22:38:07 +02:00
viewMode: VIEW_ALL,
2018-11-21 22:20:55 +01:00
loadingSuggested: false,
firstRunCompleted: false,
showSuggestedSubs: false,
2019-09-26 18:07:11 +02:00
enabledChannelNotifications: [],
2017-12-08 21:14:35 +01:00
};
export default handleActions(
{
2019-05-07 23:38:29 +02:00
[ACTIONS.CHANNEL_SUBSCRIBE]: (state: SubscriptionState, action: DoChannelSubscribe): SubscriptionState => {
2017-12-08 21:14:35 +01:00
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,
};
},
2019-05-07 23:38:29 +02:00
[ACTIONS.CHANNEL_UNSUBSCRIBE]: (state: SubscriptionState, action: DoChannelUnsubscribe): SubscriptionState => {
2017-12-08 21:14:35 +01:00
const subscriptionToRemove: Subscription = action.data;
const newSubscriptions = state.subscriptions
.slice()
.filter(subscription => subscription.channelName !== subscriptionToRemove.channelName);
2017-12-08 21:14:35 +01:00
// Check if we need to remove it from the 'unread' state
2018-11-05 16:50:49 +01:00
const { unread } = state;
if (unread[subscriptionToRemove.uri]) {
delete unread[subscriptionToRemove.uri];
}
2017-12-08 21:14:35 +01:00
return {
...state,
2018-11-20 23:23:11 +01:00
unread: { ...unread },
2017-12-08 21:14:35 +01:00
subscriptions: newSubscriptions,
};
},
[ACTIONS.SET_SUBSCRIPTION_LATEST]: (
state: SubscriptionState,
2018-10-19 22:38:07 +02:00
action: SetSubscriptionLatest
): SubscriptionState => ({
...state,
subscriptions: state.subscriptions.map(subscription =>
subscription.channelName === action.data.subscription.channelName
? { ...subscription, latest: action.data.uri }
: subscription
2018-03-06 09:36:04 +01:00
),
}),
2018-10-19 22:38:07 +02:00
[ACTIONS.UPDATE_SUBSCRIPTION_UNREADS]: (
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]: (
state: SubscriptionState,
2018-10-19 22:38:07 +02:00
action: DoRemoveSubscriptionUnreads
): SubscriptionState => {
const { channel, uris } = action.data;
// If no channel is passed in, remove all unreads
let newUnread;
if (channel) {
newUnread = { ...state.unread };
if (!uris) {
delete newUnread[channel];
} else {
newUnread[channel].uris = uris;
}
2018-10-19 22:38:07 +02:00
} else {
newUnread = {};
2018-10-19 22:38:07 +02:00
}
return {
...state,
unread: {
...newUnread,
},
};
},
[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
): SubscriptionState => ({
...state,
loading: false,
subscriptions: action.data,
}),
2019-05-07 23:38:29 +02:00
[ACTIONS.SET_VIEW_MODE]: (state: SubscriptionState, action: SetViewMode): SubscriptionState => ({
2018-10-19 22:38:07 +02:00
...state,
viewMode: action.data,
}),
2018-11-21 22:20:55 +01:00
[ACTIONS.GET_SUGGESTED_SUBSCRIPTIONS_START]: (state: SubscriptionState): SubscriptionState => ({
...state,
loadingSuggested: true,
}),
[ACTIONS.GET_SUGGESTED_SUBSCRIPTIONS_SUCCESS]: (
state: SubscriptionState,
action: GetSuggestedSubscriptionsSuccess
): SubscriptionState => ({
...state,
suggested: action.data,
loadingSuggested: false,
}),
[ACTIONS.GET_SUGGESTED_SUBSCRIPTIONS_FAIL]: (state: SubscriptionState): SubscriptionState => ({
...state,
loadingSuggested: false,
}),
2019-09-26 18:07:11 +02:00
[LBRY_REDUX_ACTIONS.USER_STATE_POPULATE]: (
state: SubscriptionState,
action: { data: { subscriptions: ?Array<string> } }
) => {
const { subscriptions } = action.data;
let newSubscriptions;
if (!subscriptions) {
newSubscriptions = state.subscriptions;
} else {
const parsedSubscriptions = subscriptions.map(uri => {
const { channelName } = parseURI(uri);
return {
uri,
channelName: `@${channelName}`,
};
});
if (!state.subscriptions || !state.subscriptions.length) {
newSubscriptions = parsedSubscriptions;
} else {
const map = {};
newSubscriptions = parsedSubscriptions.concat(state.subscriptions).filter(sub => {
return map[sub.uri] ? false : (map[sub.uri] = true);
}, {});
}
}
return {
...state,
subscriptions: newSubscriptions,
};
},
2017-12-08 21:14:35 +01:00
},
defaultState
);