mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 10:01:40 -05:00
Refactor PostsCategoryParameter callback to use readQuery and runQuery
This commit is contained in:
parent
c52e53afbe
commit
c610ab4f10
1 changed files with 33 additions and 26 deletions
|
@ -4,47 +4,54 @@ Categories parameter
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Categories from './index.js';
|
import { addCallback, getSetting, registerSetting, getFragment, runQuery } from 'meteor/vulcan:core';
|
||||||
import { addCallback, getSetting, registerSetting } from 'meteor/vulcan:core';
|
import gql from 'graphql-tag';
|
||||||
import { getCategories } from './schema.js';
|
|
||||||
|
|
||||||
registerSetting('forum.categoriesFilter', 'union', 'Display posts belonging to all (“intersection”) or at least one of (“union”) the selected categories');
|
registerSetting('forum.categoriesFilter', 'union', 'Display posts belonging to all (“intersection”) or at least one of (“union”) the selected categories');
|
||||||
|
|
||||||
// Category Posts Parameters
|
// Category Posts Parameters
|
||||||
// Add a 'categories' property to terms which can be used to filter *all* existing Posts views.
|
// Add a 'categories' property to terms which can be used to filter *all* existing Posts views.
|
||||||
function PostsCategoryParameter(parameters, terms, apolloClient) {
|
async function PostsCategoryParameter(parameters, terms, apolloClient) {
|
||||||
|
|
||||||
|
// get category slugs
|
||||||
const cat = terms.cat || terms['cat[]'];
|
const cat = terms.cat || terms['cat[]'];
|
||||||
// filter by category if category slugs are provided
|
const categoriesSlugs = Array.isArray(cat) ? cat : [cat];
|
||||||
if (cat) {
|
let allCategories = [];
|
||||||
|
|
||||||
let categoriesIds = [];
|
if (cat.length) {
|
||||||
let selector = {};
|
|
||||||
let slugs;
|
|
||||||
|
|
||||||
if (typeof cat === 'string') { // cat is a string
|
// get all categories
|
||||||
selector = {slug: cat};
|
// note: specify all arguments, see https://github.com/apollographql/apollo-client/issues/2051
|
||||||
slugs = [cat];
|
const query = `
|
||||||
} else if (Array.isArray(cat)) { // cat is an array
|
query GetCategories($terms: JSON) {
|
||||||
selector = {slug: {$in: cat}};
|
CategoriesList(terms: $terms) {
|
||||||
slugs = cat;
|
_id
|
||||||
|
slug
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
if (Meteor.isClient) {
|
||||||
|
// get categories from Redux store
|
||||||
|
allCategories = apolloClient.readQuery({
|
||||||
|
query: gql`${query}`,
|
||||||
|
variables: {terms: {limit: 0, itemsPerPage: 0}}
|
||||||
|
}).CategoriesList;
|
||||||
|
} else {
|
||||||
|
// get categories through GraphQL API using runQuery
|
||||||
|
const results = await runQuery(query);
|
||||||
|
allCategories = results.data.CategoriesList;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: use new Apollo imperative API
|
// get corresponding category ids
|
||||||
// get all categories passed in terms
|
const categoriesIds = _.pluck(_.filter(allCategories, category => _.contains(categoriesSlugs, category.slug)), '_id');
|
||||||
const categories = Meteor.isClient ? _.filter(getCategories(apolloClient), category => _.contains(slugs, category.slug) ) : Categories.find(selector).fetch();
|
|
||||||
|
|
||||||
// for each category, add its ID and the IDs of its children to categoriesId array
|
|
||||||
categories.forEach(function (category) {
|
|
||||||
categoriesIds.push(category._id);
|
|
||||||
// TODO: find a better way to handle child categories
|
|
||||||
// categoriesIds = categoriesIds.concat(_.pluck(Categories.getChildren(category), '_id'));
|
|
||||||
});
|
|
||||||
|
|
||||||
const operator = getSetting('forum.categoriesFilter', 'union') === 'union' ? '$in' : '$all';
|
const operator = getSetting('forum.categoriesFilter', 'union') === 'union' ? '$in' : '$all';
|
||||||
|
|
||||||
parameters.selector = Meteor.isClient ? {...parameters.selector, 'categories._id': {$in: categoriesIds}} : {...parameters.selector, categories: {[operator]: categoriesIds}};
|
// parameters.selector = Meteor.isClient ? {...parameters.selector, 'categories._id': {$in: categoriesIds}} : {...parameters.selector, categories: {[operator]: categoriesIds}};
|
||||||
|
parameters.selector = {...parameters.selector, categories: {[operator]: categoriesIds}};
|
||||||
}
|
}
|
||||||
|
|
||||||
return parameters;
|
return parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue