From 32f8ded652f920c8d9456ccecfbafd3b645b9ed6 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Mon, 23 Sep 2019 09:35:27 +0100 Subject: [PATCH] update subscriptions reducer to handle USER_STATE_POPULATE action --- dist/bundle.es.js | 35 +++++++++++++++++++++++- dist/bundle.js | 41 ++++++++++++++++++++++++++--- src/redux/reducers/subscriptions.js | 34 ++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 5 deletions(-) diff --git a/dist/bundle.es.js b/dist/bundle.es.js index 99e9110..76d73c3 100644 --- a/dist/bundle.es.js +++ b/dist/bundle.es.js @@ -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) { diff --git a/dist/bundle.js b/dist/bundle.js index 011650a..eb3b850 100644 --- a/dist/bundle.js +++ b/dist/bundle.js @@ -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)); /***/ }), diff --git a/src/redux/reducers/subscriptions.js b/src/redux/reducers/subscriptions.js index b58f6a0..ec7c5f3 100644 --- a/src/redux/reducers/subscriptions.js +++ b/src/redux/reducers/subscriptions.js @@ -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 } } + ) => { + 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 );