Vulcan/packages/vulcan-categories/lib/schema.js

104 lines
2.6 KiB
JavaScript
Raw Normal View History

import { Utils } from 'meteor/vulcan:core';
2017-01-25 13:58:02 +09:00
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) => {
2017-01-25 13:58:02 +09:00
return object.__typename === 'Category'
});
// order categories
categories = _.sortBy(categories, cat => cat.order);
return categories;
}
export function getCategoriesAsOptions (apolloClient) {
2017-01-25 13:58:02 +09:00
// give the form component (here: checkboxgroup) exploitable data
return getCategories(apolloClient).map(function (category) {
2017-01-25 13:58:02 +09:00
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) {
return {
value: category._id,
label: category.name,
parentId: category.parentId,
_id: category._id
// slug: category.slug, // note: it may be used to look up from prefilled props
};
});
const nestedCategories = Utils.unflatten(formattedCategories, {idProperty: '_id', parentIdProperty: 'parentId', childrenProperty: 'options'});
return nestedCategories;
}
2016-06-23 15:11:56 +09:00
// category schema
const schema = {
2016-11-08 15:16:58 +09:00
_id: {
type: String,
viewableBy: ['guests'],
2016-11-08 15:16:58 +09:00
optional: true,
},
2016-06-23 15:11:56 +09:00
name: {
type: String,
viewableBy: ['guests'],
insertableBy: ['members'],
editableBy: ['members'],
2016-06-23 15:11:56 +09:00
},
description: {
type: String,
optional: true,
viewableBy: ['guests'],
insertableBy: ['members'],
editableBy: ['members'],
form: {
2016-06-23 15:11:56 +09:00
rows: 3
}
},
order: {
type: Number,
optional: true,
viewableBy: ['guests'],
insertableBy: ['members'],
editableBy: ['members'],
2016-06-23 15:11:56 +09:00
},
slug: {
type: String,
optional: true,
viewableBy: ['guests'],
insertableBy: ['members'],
editableBy: ['members'],
2016-06-23 15:11:56 +09:00
},
image: {
type: String,
optional: true,
viewableBy: ['guests'],
insertableBy: ['members'],
editableBy: ['members'],
2016-06-23 15:11:56 +09:00
},
2017-01-25 13:58:02 +09:00
parentId: {
type: String,
optional: true,
control: "select",
viewableBy: ['guests'],
insertableBy: ['members'],
editableBy: ['members'],
resolveAs: 'parent: Category',
form: {
options: formProps => getCategoriesAsOptions(formProps.client)
2017-01-25 13:58:02 +09:00
}
}
};
2016-06-23 15:11:56 +09:00
export default schema;