Skip to content

Commit

Permalink
API: /shops/market-gear - refactoring pinnedGearUtils - move _isPinne…
Browse files Browse the repository at this point in the history
…d to common/libs
  • Loading branch information
negue committed Aug 22, 2017
1 parent b3acc69 commit c16a4aa
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 20 deletions.
4 changes: 2 additions & 2 deletions website/client/components/creatorIntro.vue
Expand Up @@ -544,7 +544,7 @@ import hairIcon from 'assets/svg/hair.svg';
import backgroundsIcon from 'assets/svg/backgrounds.svg';
import gem from 'assets/svg/gem.svg';
import pin from 'assets/svg/pin.svg';
import _isPinned from './shops/_isPinned';
import { isPinned } from 'common/script/ops/pinnedGearUtils';
export default {
Expand Down Expand Up @@ -713,7 +713,7 @@ export default {
return backgroundClass;
},
isBackgroundPinned (bg) {
return _isPinned(this.user, bg);
return isPinned(this.user, bg);
},
togglePinned (bg) {
if (!this.$store.dispatch('user:togglePinnedItem', {type: bg.pinType, path: bg.path})) {
Expand Down
6 changes: 0 additions & 6 deletions website/client/components/shops/_isPinned.js

This file was deleted.

9 changes: 4 additions & 5 deletions website/client/components/shops/market/index.vue
Expand Up @@ -420,15 +420,14 @@
import featuredItems from 'common/script/content/shop-featuredItems';
import getItemInfo from 'common/script/libs/getItemInfo';
import { isPinned } from 'common/script/ops/pinnedGearUtils';
import _filter from 'lodash/filter';
import _sortBy from 'lodash/sortBy';
import _map from 'lodash/map';
import _get from 'lodash/get';
import _throttle from 'lodash/throttle';
import _isPinned from '../_isPinned';
const sortGearTypes = ['sortByType', 'sortByPrice', 'sortByCon', 'sortByPer', 'sortByStr', 'sortByInt'];
import notifications from 'client/mixins/notifications';
Expand Down Expand Up @@ -568,7 +567,7 @@ export default {
featuredItems () {
return featuredItems.market.map(i => {
let newItem = getItemInfo(this.user, i.type, _get(this.content, i.path));
newItem.pinned = _isPinned(this.user, newItem);
newItem.pinned = isPinned(this.user, newItem);
return newItem;
});
Expand Down Expand Up @@ -636,7 +635,7 @@ export default {
result = _map(result, (e) => {
let newItem = getItemInfo(this.user, 'marketGear', e);
newItem.pinned = _isPinned(this.user, newItem);
newItem.pinned = isPinned(this.user, newItem);
newItem.locked = this.isGearLocked(newItem);
return newItem;
Expand Down Expand Up @@ -669,7 +668,7 @@ export default {
let result = _map(category.items, (e) => {
return {
...e,
pinned: _isPinned(this.user, e),
pinned: isPinned(this.user, e),
};
});
Expand Down
6 changes: 3 additions & 3 deletions website/client/components/shops/quests/index.vue
Expand Up @@ -341,7 +341,7 @@
import featuredItems from 'common/script/content/shop-featuredItems';
import getItemInfo from 'common/script/libs/getItemInfo';
import _isPinned from '../_isPinned';
import { isPinned } from 'common/script/ops/pinnedGearUtils';
import _filter from 'lodash/filter';
import _sortBy from 'lodash/sortBy';
Expand Down Expand Up @@ -416,7 +416,7 @@ export default {
featuredItems () {
return featuredItems.quests.map(i => {
let newItem = getItemInfo(this.user, i.type, _get(this.content, i.path));
newItem.pinned = _isPinned(this.user, newItem);
newItem.pinned = isPinned(this.user, newItem);
return newItem;
});
Expand All @@ -427,7 +427,7 @@ export default {
let result = _map(category.items, (e) => {
return {
...e,
pinned: _isPinned(this.user, e),
pinned: isPinned(this.user, e),
};
});
Expand Down
4 changes: 2 additions & 2 deletions website/client/components/shops/seasonal/index.vue
Expand Up @@ -314,7 +314,7 @@
import _throttle from 'lodash/throttle';
import _groupBy from 'lodash/groupBy';
import _isPinned from '../_isPinned';
import { isPinned } from 'common/script/ops/pinnedGearUtils';
import i18n from 'common/script/i18n';
Expand Down Expand Up @@ -433,7 +433,7 @@
let result = _map(category.items, (e) => {
return {
...e,
pinned: _isPinned(this.user, e),
pinned: isPinned(this.user, e),
};
});
Expand Down
4 changes: 2 additions & 2 deletions website/client/components/shops/timeTravelers/index.vue
Expand Up @@ -279,7 +279,7 @@
import _groupBy from 'lodash/groupBy';
import _map from 'lodash/map';
import _isPinned from '../_isPinned';
import { isPinned } from 'common/script/ops/pinnedGearUtils';
export default {
components: {
Expand Down Expand Up @@ -379,7 +379,7 @@ export default {
let result = _map(category.items, (e) => {
return {
...e,
pinned: _isPinned(this.user, e),
pinned: isPinned(this.user, e),
};
});
Expand Down
3 changes: 3 additions & 0 deletions website/common/script/libs/getItemInfo.js
Expand Up @@ -3,6 +3,8 @@ import content from '../content/index';
import { BadRequest } from './errors';
import count from '../count';

import isPinned from './isPinned';

import _mapValues from 'lodash/mapValues';

function lockQuest (quest, user) {
Expand Down Expand Up @@ -265,6 +267,7 @@ module.exports = function getItemInfo (user, type, item, language = 'en') {

if (itemInfo) {
itemInfo.isSuggested = isItemSuggested(itemInfo);
itemInfo.locked = isPinned(user, itemInfo);
} else {
throw new BadRequest(i18n.t('wrongItemType', {type}, language));
}
Expand Down
9 changes: 9 additions & 0 deletions website/common/script/libs/isPinned.js
@@ -0,0 +1,9 @@
module.exports = function isPinned (user, item) {
if (user === null)
return false;

const isUnpinned = user.unpinnedItems.findIndex(unpinned => unpinned.path === item.path) > -1;
const pinnedItem = user.pinnedItems.findIndex(pinned => pinned.path === item.path) > -1;

return pinnedItem && !isUnpinned;
};
53 changes: 53 additions & 0 deletions website/common/script/libs/shops.js
Expand Up @@ -2,13 +2,15 @@ import values from 'lodash/values';
import map from 'lodash/map';
import keys from 'lodash/keys';
import each from 'lodash/each';
import filter from 'lodash/filter';
import eachRight from 'lodash/eachRight';
import toArray from 'lodash/toArray';
import pickBy from 'lodash/pickBy';
import sortBy from 'lodash/sortBy';
import content from '../content/index';
import i18n from '../i18n';
import getItemInfo from './getItemInfo';
import updateStore from './updateStore';

let shops = {};

Expand Down Expand Up @@ -69,6 +71,57 @@ shops.getMarketCategories = function getMarket (user, language) {
return categories;
};

function getClassName (classType, language) {
if (classType === 'wizard') {
return i18n.t('mage', language);
} else {
return i18n.t(classType, language);
}
}

shops.checkMarketGearLocked = function checkMarketGearLocked (user, items) {
let result = filter(items, ['pinTyp', 'marketGear']);

let availableGear = map(updateStore(user), (item) => item.path);

for (let gear of result) {
if (gear.klass !== user.stats.class) {
gear.locked = true;
}

if (!availableGear.includes(gear.path)) {
gear.locked = true;
}
}
};

shops.getMarketGearCategories = function getMarketGear (user, language) {
let categories = [];

for (let classType of content.classes) {
let category = {
identifier: classType,
text: getClassName(classType, language),
};

let result = filter(content.gear.flat, ['klass', classType]);
category.items = map(result, (e) => {
let newItem = getItemInfo(user, 'marketGear', e);

newItem.locked = this.isGearLocked(newItem);

return newItem;
});

shops.checkMarketGearLocked(user, category.items);

categories.push(category);
}

return categories;
};


shops.getQuestShopCategories = function getQuestShopCategories (user, language) {
let categories = [];

Expand Down
3 changes: 3 additions & 0 deletions website/common/script/ops/pinnedGearUtils.js
Expand Up @@ -4,6 +4,8 @@ import get from 'lodash/get';
import { BadRequest } from '../libs/errors';
import i18n from '../i18n';

import isPinned from '../libs/isPinned';

const officialPinnedItems = content.officialPinnedItems;

import updateStore from '../libs/updateStore';
Expand Down Expand Up @@ -120,4 +122,5 @@ module.exports = {
removePinnedGearAddPossibleNewOnes,
togglePinnedItem,
removeItemByPath,
isPinned,
};
23 changes: 23 additions & 0 deletions website/server/controllers/api-v3/shops.js
Expand Up @@ -31,6 +31,29 @@ api.getMarketItems = {
},
};

/**
* @apiIgnore
* @api {get} /api/v3/shops/market-gear get the available gear for the market
* @apiName GetMarketGear
* @apiGroup Shops
*
* @apiSuccess {Object} data List of available gear
*/
api.getMarketGear = {
method: 'GET',
url: '/shops/market-gear',
middlewares: [authWithHeaders()],
async handler (req, res) {
let user = res.locals.user;

let resObject = {
categories: shops.getMarketGearCategories(user, req.language),
};

res.respond(200, resObject);
},
};

/**
* @apiIgnore
* @api {get} /api/v3/shops/quests get the available items for the quests shop
Expand Down

0 comments on commit c16a4aa

Please sign in to comment.