From f35ef3a04621ef7c5e25fb66b9d1e7381f22795e Mon Sep 17 00:00:00 2001 From: negue Date: Sun, 31 Mar 2019 20:41:37 +0200 Subject: [PATCH] Multiple checks for pinnedItems (#11031) * remove null/undefined entries from pinnedItems when an item is toggled - more inner checks + test * drawer: fix when there isn't a page available * rollback cleaning up pinnedEntries on item-toggle * remove "re-setting" pinnedItems * remove the filter --- test/common/libs/inAppRewards.js | 12 ++++++++++++ test/common/ops/pinnedGearUtils.js | 18 ++++++++++++++++++ website/client/components/ui/drawer.vue | 4 +++- website/client/store/actions/shops.js | 3 --- website/common/script/libs/inAppRewards.js | 12 ++++++++---- website/common/script/libs/shops.js | 3 ++- website/common/script/ops/pinnedGearUtils.js | 13 +++++++++++-- 7 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 test/common/ops/pinnedGearUtils.js diff --git a/test/common/libs/inAppRewards.js b/test/common/libs/inAppRewards.js index d7634c72b0..43369f4307 100644 --- a/test/common/libs/inAppRewards.js +++ b/test/common/libs/inAppRewards.js @@ -62,6 +62,18 @@ describe('inAppRewards', () => { expect(result[9].path).to.eql('potion'); }); + it('ignores null/undefined entries', () => { + user.pinnedItems = testPinnedItems; + user.pinnedItems.push(null); + user.pinnedItems.push(undefined); + user.pinnedItemsOrder = testPinnedItemsOrder; + + let result = inAppRewards(user); + + expect(result[2].path).to.eql('armoire'); + expect(result[9].path).to.eql('potion'); + }); + it('does not return seasonal items which have been unpinned', () => { if (officialPinnedItems.length === 0) { return; // if no seasonal items, this test is not applicable diff --git a/test/common/ops/pinnedGearUtils.js b/test/common/ops/pinnedGearUtils.js new file mode 100644 index 0000000000..e45e60a729 --- /dev/null +++ b/test/common/ops/pinnedGearUtils.js @@ -0,0 +1,18 @@ +import { + generateUser, +} from '../../helpers/common.helper'; +import {addPinnedGear} from '../../../website/common/script/ops/pinnedGearUtils'; + +describe('shared.ops.pinnedGearUtils.addPinnedGear', () => { + let user; + + beforeEach(() => { + user = generateUser(); + }); + + it('not adds an item with empty properties to pinnedItems', () => { + addPinnedGear(user, undefined, undefined); + + expect(user.pinnedItems.length).to.be.eql(0); + }); +}); diff --git a/website/client/components/ui/drawer.vue b/website/client/components/ui/drawer.vue index 7d8924ae28..b7afbcb458 100644 --- a/website/client/components/ui/drawer.vue +++ b/website/client/components/ui/drawer.vue @@ -174,7 +174,9 @@ export default { let minPaddingBottom = 20; let drawerHeight = this.$el.offsetHeight; let standardPage = document.getElementsByClassName('standard-page')[0]; - standardPage.style.paddingBottom = `${drawerHeight + minPaddingBottom}px`; + if (standardPage) { + standardPage.style.paddingBottom = `${drawerHeight + minPaddingBottom}px`; + } }, toggle () { this.open = !this.isOpen; diff --git a/website/client/store/actions/shops.js b/website/client/store/actions/shops.js index 0d8359ef95..43b9f03b22 100644 --- a/website/client/store/actions/shops.js +++ b/website/client/store/actions/shops.js @@ -20,9 +20,6 @@ function buyItem (store, params) { let opResult = buyOp(user, {params, quantity}); - user.pinnedItems = opResult[0].pinnedItems; - - return { result: opResult, httpCall: axios.post(`/api/v4/user/buy/${params.key}`), diff --git a/website/common/script/libs/inAppRewards.js b/website/common/script/libs/inAppRewards.js index 939925af52..203df6cf7c 100644 --- a/website/common/script/libs/inAppRewards.js +++ b/website/common/script/libs/inAppRewards.js @@ -4,6 +4,7 @@ import getOfficialPinnedItems from './getOfficialPinnedItems'; import compactArray from 'lodash/compact'; import getItemByPathAndType from './getItemByPathAndType'; +import {checkPinnedAreasForNullEntries} from '../ops/pinnedGearUtils'; /** * Orders the pinned items so we always get our inAppRewards in the order @@ -32,6 +33,8 @@ function sortInAppRewards (user, items) { } module.exports = function getPinnedItems (user) { + checkPinnedAreasForNullEntries(user); + let officialPinnedItems = getOfficialPinnedItems(user); const officialPinnedItemsNotUnpinned = officialPinnedItems.filter(officialPin => { @@ -41,11 +44,12 @@ module.exports = function getPinnedItems (user) { const pinnedItems = officialPinnedItemsNotUnpinned.concat(user.pinnedItems); - let items = pinnedItems.map(({type, path}) => { - let item = getItemByPathAndType(type, path); + let items = pinnedItems + .map(({type, path}) => { + let item = getItemByPathAndType(type, path); - return getItemInfo(user, type, item, officialPinnedItems); - }); + return getItemInfo(user, type, item, officialPinnedItems); + }); shops.checkMarketGearLocked(user, items); diff --git a/website/common/script/libs/shops.js b/website/common/script/libs/shops.js index f781512e3b..b9c9c9df61 100644 --- a/website/common/script/libs/shops.js +++ b/website/common/script/libs/shops.js @@ -107,7 +107,8 @@ function getClassName (classType, language) { // TODO Refactor the `.locked` logic shops.checkMarketGearLocked = function checkMarketGearLocked (user, items) { let result = filter(items, ['pinType', 'marketGear']); - let availableGear = map(updateStore(user), (item) => getItemInfo(user, 'marketGear', item).path); + const officialPinnedItems = getOfficialPinnedItems(user); + let availableGear = map(updateStore(user), (item) => getItemInfo(user, 'marketGear', item, officialPinnedItems).path); for (let gear of result) { if (gear.klass !== user.stats.class) { gear.locked = true; diff --git a/website/common/script/ops/pinnedGearUtils.js b/website/common/script/ops/pinnedGearUtils.js index 2ec133509b..bc96ad2be3 100644 --- a/website/common/script/ops/pinnedGearUtils.js +++ b/website/common/script/ops/pinnedGearUtils.js @@ -27,6 +27,15 @@ function pathExistsInArray (array, path) { }); } +function checkForNullEntries (array) { + return array.filter(e => Boolean(e)); +} + +function checkPinnedAreasForNullEntries (user) { + user.pinnedItems = checkForNullEntries(user.pinnedItems); + user.unpinnedItems = checkForNullEntries(user.unpinnedItems); +} + function selectGearToPin (user) { let changes = []; @@ -41,11 +50,10 @@ function selectGearToPin (user) { return sortBy(changes, (change) => sortOrder[change.type]); } - function addPinnedGear (user, type, path) { const foundIndex = pathExistsInArray(user.pinnedItems, path); - if (foundIndex === -1) { + if (foundIndex === -1 && type && path) { user.pinnedItems.push({ type, path, @@ -176,5 +184,6 @@ module.exports = { togglePinnedItem, removeItemByPath, selectGearToPin, + checkPinnedAreasForNullEntries, isPinned, };