update subscriptions reducer to handle USER_STATE_POPULATE action

This commit is contained in:
Akinwale Ariwodola 2019-09-23 09:35:27 +01:00 committed by Sean Yesmunt
parent ebc8122c99
commit 32f8ded652
3 changed files with 105 additions and 5 deletions

35
dist/bundle.es.js vendored
View file

@ -632,7 +632,40 @@ var subscriptions = handleActions({
}),
[VIEW_SUGGESTED_SUBSCRIPTIONS]: state => ({ ...state,
showSuggestedSubs: true
})
}),
[lbryRedux.ACTIONS.USER_STATE_POPULATE]: (state, action) => {
const {
subscriptions
} = action.data;
let newSubscriptions;
if (!subscriptions) {
newSubscriptions = state.subscriptions;
} else {
const parsedSubscriptions = subscriptions.map(uri => {
const {
channelName
} = lbryRedux.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
};
}
}, defaultState);
function swapKeyAndValue(dict) {

41
dist/bundle.js vendored
View file

@ -1207,8 +1207,10 @@ rewards.setCallback = function (name, method) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var constants_action_types__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
/* harmony import */ var constants_subscriptions__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(9);
/* harmony import */ var util_redux_utils__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(10);
/* harmony import */ var lbry_redux__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(3);
/* harmony import */ var lbry_redux__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(lbry_redux__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var constants_subscriptions__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(9);
/* harmony import */ var util_redux_utils__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(10);
var _handleActions;
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
@ -1218,18 +1220,19 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
var defaultState = {
enabledChannelNotifications: [],
subscriptions: [],
unread: {},
suggested: {},
loading: false,
viewMode: constants_subscriptions__WEBPACK_IMPORTED_MODULE_1__["VIEW_ALL"],
viewMode: constants_subscriptions__WEBPACK_IMPORTED_MODULE_2__["VIEW_ALL"],
loadingSuggested: false,
firstRunCompleted: false,
showSuggestedSubs: false
};
/* harmony default export */ __webpack_exports__["default"] = (Object(util_redux_utils__WEBPACK_IMPORTED_MODULE_2__["handleActions"])((_handleActions = {}, _defineProperty(_handleActions, constants_action_types__WEBPACK_IMPORTED_MODULE_0__["CHANNEL_SUBSCRIBE"], function (state, action) {
/* harmony default export */ __webpack_exports__["default"] = (Object(util_redux_utils__WEBPACK_IMPORTED_MODULE_3__["handleActions"])((_handleActions = {}, _defineProperty(_handleActions, constants_action_types__WEBPACK_IMPORTED_MODULE_0__["CHANNEL_SUBSCRIBE"], function (state, action) {
var newSubscription = action.data;
var newSubscriptions = state.subscriptions.slice();
newSubscriptions.unshift(newSubscription);
@ -1354,6 +1357,36 @@ var defaultState = {
return _objectSpread({}, state, {
showSuggestedSubs: true
});
}), _defineProperty(_handleActions, lbry_redux__WEBPACK_IMPORTED_MODULE_1__["ACTIONS"].USER_STATE_POPULATE, function (state, action) {
var subscriptions = action.data.subscriptions;
var newSubscriptions;
if (!subscriptions) {
newSubscriptions = state.subscriptions;
} else {
var parsedSubscriptions = subscriptions.map(function (uri) {
var _parseURI = Object(lbry_redux__WEBPACK_IMPORTED_MODULE_1__["parseURI"])(uri),
channelName = _parseURI.channelName;
return {
uri: uri,
channelName: "@".concat(channelName)
};
});
if (!state.subscriptions || !state.subscriptions.length) {
newSubscriptions = parsedSubscriptions;
} else {
var map = {};
newSubscriptions = parsedSubscriptions.concat(state.subscriptions).filter(function (sub) {
return map[sub.uri] ? false : map[sub.uri] = true;
}, {});
}
}
return _objectSpread({}, state, {
subscriptions: newSubscriptions
});
}), _handleActions), defaultState));
/***/ }),

View file

@ -1,5 +1,6 @@
// @flow
import * as ACTIONS from 'constants/action_types';
import { parseURI, ACTIONS as LBRY_REDUX_ACTIONS } from 'lbry-redux';
import { VIEW_ALL } from 'constants/subscriptions';
import { handleActions } from 'util/redux-utils';
@ -194,6 +195,39 @@ export default handleActions(
...state,
showSuggestedSubs: true,
}),
[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,
};
},
},
defaultState
);