@websolutespa/payload-plugin-bowl
Version:
Bowl PayloadCms plugin of the BOM Repository
106 lines (105 loc) • 3.46 kB
JavaScript
import { hasRole, isAdmin, isRole } from '../core';
import { roles } from '../types';
export const Market = (options)=>({
type: 'withCollection',
slug: options.slug.market,
admin: {
group: options.group.config,
useAsTitle: 'title',
defaultColumns: [
'title',
'id',
'countries',
'languages'
],
hidden: (args)=>!hasRole(args.user, roles.Admin)
},
access: {
create: isAdmin,
read: isRole(),
update: isAdmin,
delete: isAdmin
},
fields: [
// inherited fields: id, createdAt, updatedAt
{
type: 'withId',
required: true
},
{
type: 'withTitle',
required: true,
localized: false
},
{
type: 'withIsActive'
},
{
type: 'withIsDefault'
},
{
type: 'withCheckbox',
name: 'isHidden'
},
// !!! todo create a generic withRelation(slug.country) ?
{
name: 'countries',
label: 'Countries',
type: 'relationship',
relationTo: options.slug.country,
hasMany: true
},
{
name: 'languages',
label: 'Languages',
type: 'relationship',
relationTo: options.slug.language,
hasMany: true,
filterOptions: ()=>{
return {
'id': {
in: options.locales
}
};
}
},
{
name: 'defaultLanguage',
label: 'Default language',
type: 'relationship',
relationTo: options.slug.language,
hasMany: false,
hooks: {
beforeChange: [
({ data, value })=>{
const languages = data?.languages;
if (languages && languages.length > 0) return value;
return null;
}
]
},
admin: {
condition: (data)=>{
const languages = data.languages;
if (languages && languages.length > 0) return true;
return false;
}
},
filterOptions: ()=>{
return {
'id': {
in: options.locales
}
};
},
validate: async (value, { data })=>{
const languages = data && 'languages' in data ? data.languages : undefined;
if (!value || languages && languages.length > 0 && languages.indexOf(value) < 0) {
return 'You must select a default language from the selected languages.';
}
return true;
}
}
]
});
//# sourceMappingURL=Market.js.map