@websolutespa/payload-plugin-bowl
Version:
Bowl PayloadCms plugin of the BOM Repository
122 lines (121 loc) • 3.98 kB
JavaScript
import { deepMerge, withCollectionHook } from '@websolutespa/payload-utils';
import { toField } from '../../mapper';
import { options } from '../../options';
import { isRole } from '../access';
import { afterCollectionReadHook, collectionBulkPatch, collectionDetailGet, collectionExportGet, collectionImportPost, collectionIndexGet, collectionUpdatePatch } from '../api/collection.service';
import { translateCollection } from '../translations';
import { mergeFields } from '../utils';
export const AddressDefault = {
admin: {
group: options.group.config,
useAsTitle: 'address',
defaultColumns: [
'address',
'firstName',
'lastName',
'companyName'
]
},
access: {
create: isRole(options.roles.Admin, options.roles.Contributor, options.roles.Editor, options.roles.Guest, options.roles.User),
read: isRole(options.roles.Admin, options.roles.Contributor, options.roles.Editor, options.roles.Guest, options.roles.User),
update: isRole(options.roles.Admin, options.roles.Contributor, options.roles.Editor),
delete: isRole(options.roles.Admin, options.roles.Contributor, options.roles.Editor)
}
};
/**
*
* @param {WithAddressProps} config
* @returns {CollectionConfig} Return a `CollectionConfig` with inherited fields: `id`, `createdAt`, `updatedAt`.
*/ export const withAddress = (config)=>{
const addressConfig = deepMerge(AddressDefault, config);
const addressFields = addressConfig.fields;
const defaultFields = [
toField({
name: 'firstName',
label: 'First Name',
type: 'withText'
}),
toField({
name: 'lastName',
label: 'Last Name',
type: 'withText'
}),
toField({
name: 'companyName',
label: 'Company Name',
type: 'withText'
}),
toField({
name: 'email',
label: 'Email',
type: 'withText'
}),
toField({
name: 'phoneNumber',
label: 'Phone Number',
type: 'withText'
}),
toField({
name: 'country',
type: 'relationship',
relationTo: options.slug.country
}),
toField({
name: 'region',
type: 'relationship',
relationTo: options.slug.region
}),
toField({
name: 'province',
type: 'relationship',
relationTo: options.slug.province
}),
toField({
name: 'address',
label: 'Address',
type: 'withText'
}),
toField({
name: 'streetNumber',
label: 'Street Number',
type: 'withText'
}),
toField({
name: 'zipCode',
label: 'Zip Code',
type: 'withText'
}),
toField({
name: 'city',
label: 'City',
type: 'withText'
}),
toField({
name: 'websiteUrl',
label: 'Website Url',
type: 'withText'
})
];
addressConfig.fields = mergeFields(defaultFields, addressFields);
// !!! todo check if
if (!addressConfig.endpoints) {
addressConfig.endpoints = [
collectionIndexGet(config.slug),
collectionUpdatePatch(config.slug),
collectionBulkPatch(config.slug),
collectionImportPost(config.slug),
collectionExportGet(config.slug),
collectionDetailGet(config.slug)
];
}
withCollectionHook(addressConfig, 'afterRead', afterCollectionReadHook(addressConfig));
/*
!!! removed on v3
addressConfig.admin.hooks = {
beforeDuplicate: beforeDuplicateCollectionHook,
};
*/ translateCollection(addressConfig);
return addressConfig;
};
//# sourceMappingURL=withAddress.js.map