Forum example: categories -> categoriesIds

This commit is contained in:
SachaG 2017-12-29 08:59:58 +09:00
parent 8d0cc3e5e2
commit abe0a5e639
5 changed files with 43 additions and 39 deletions

View file

@ -27,7 +27,6 @@ class PostsEditForm extends PureComponent {
<Components.SmartForm
collection={Posts}
documentId={this.props.post._id}
mutationFragment={getFragment('PostsPage')}
successCallback={post => {
this.props.closeModal();
this.props.flash(this.context.intl.formatMessage({ id: 'posts.edit_success' }, { title: post.title }), 'success');

View file

@ -9,7 +9,7 @@ import { getCategoriesAsOptions } from './schema.js';
Posts.addField([
{
fieldName: 'categories',
fieldName: 'categoriesIds',
fieldSchema: {
type: Array,
control: 'checkboxgroup',
@ -17,11 +17,17 @@ Posts.addField([
insertableBy: ['members'],
editableBy: ['members'],
viewableBy: ['guests'],
form: {
type: 'bootstrap-category',
order: 50,
options: formProps => getCategoriesAsOptions(formProps.client),
options: props => {
return getCategoriesAsOptions(props.data.CategoriesList);
},
query: `
CategoriesList{
_id
name
slug
order
}
`,
resolveAs: {
fieldName: 'categories',
type: '[Category]',
@ -29,12 +35,13 @@ Posts.addField([
if (!post.categories) return [];
const categories = _.compact(await Categories.loader.loadMany(post.categories));
return Users.restrictViewableFields(currentUser, Categories, categories);
}
},
addOriginalField: true,
}
}
},
{
fieldName: 'categories.$',
fieldName: 'categoriesIds.$',
fieldSchema: {
type: String,
optional: true

View file

@ -7,36 +7,18 @@ Categories schema
import { Utils } from 'meteor/vulcan:core';
import { Categories } from './collection.js';
export function getCategories (apolloClient) {
// get the current data of the store
const apolloData = apolloClient.store.getState().apollo.data;
// filter these data based on their typename: we are interested in the categories data
let categories = _.filter(apolloData, (object, key) => {
return object.__typename === 'Category'
});
// order categories
categories = _.sortBy(categories, cat => cat.order);
return categories;
export function getCategoriesAsOptions (categories) {
// give the form component (here: checkboxgroup) exploitable data
return categories.map(category => ({
value: category._id,
label: category.name,
// slug: category.slug, // note: it may be used to look up from prefilled props
}));
}
export function getCategoriesAsOptions (apolloClient) {
export function getCategoriesAsNestedOptions (categories) {
// give the form component (here: checkboxgroup) exploitable data
return getCategories(apolloClient).map(function (category) {
return {
value: category._id,
label: category.name,
// slug: category.slug, // note: it may be used to look up from prefilled props
};
});
}
export function getCategoriesAsNestedOptions (apolloClient) {
// give the form component (here: checkboxgroup) exploitable data
const formattedCategories = getCategories(apolloClient).map(function (category) {
const formattedCategories = categories.map(function (category) {
return {
value: category._id,
label: category.name,
@ -122,9 +104,17 @@ const schema = {
},
addOriginalField: true
},
form: {
options: formProps => getCategoriesAsOptions(formProps.client)
}
options: props => {
return getCategoriesAsOptions(props.data.CategoriesList);
},
query: `
CategoriesList{
_id
name
slug
order
}
`,
}
};

View file

@ -77,7 +77,15 @@ const schema = {
editableBy: ['members'],
control: 'url',
order: 10,
searchable: true
searchable: true,
form: {
query: `
SiteData{
logoUrl
title
}
`,
},
},
/**
Title