Skip to content

Instantly share code, notes, and snippets.

@rafaell-lycan
Last active December 30, 2024 22:05
Show Gist options
  • Save rafaell-lycan/7fc7bb379cab7435fc298cf36d0d3941 to your computer and use it in GitHub Desktop.
Save rafaell-lycan/7fc7bb379cab7435fc298cf36d0d3941 to your computer and use it in GitHub Desktop.
Collection Locations + ad
import type { CollectionConfig } from 'payload'
export const States: CollectionConfig = {
slug: 'states',
admin: {
useAsTitle: 'name',
},
fields: [
{
name: 'name',
type: 'text',
label: 'Nome',
required: true,
unique: true,
},
{
name: 'code', // UF code like SP, RJ, MG
type: 'text',
label: 'UF',
required: true,
unique: true,
// minLength: 2,
maxLength: 2,
admin: {
description: 'UF code (e.g., SP, RJ, MG)',
},
},
{
name: 'active',
type: 'checkbox',
defaultValue: true,
},
],
}
export const Cities: CollectionConfig = {
slug: 'cities',
admin: {
useAsTitle: 'name',
defaultColumns: ['name', 'state', 'hasNeighborhoods', 'active'],
},
fields: [
{
name: 'name',
type: 'text',
required: true,
},
{
name: 'state',
type: 'relationship',
relationTo: 'states',
required: true,
hasMany: false,
},
{
name: 'hasNeighborhoods',
type: 'checkbox',
defaultValue: false,
admin: {
description: 'Check if this city should have a neighborhood list',
},
},
{
name: 'neighborhoods',
type: 'array',
admin: {
condition: (data) => data.hasNeighborhoods,
},
fields: [
{
name: 'name',
type: 'text',
required: true,
},
{
name: 'active',
type: 'checkbox',
defaultValue: true,
},
{
name: 'order',
type: 'number',
admin: {
step: 1,
},
},
],
},
{
name: 'active',
type: 'checkbox',
defaultValue: true,
},
],
}
export const Post: CollectionConfig = {
slug: 'post',
access: {
read: () => true,
},
defaultPopulate: {
name: true,
slug: true,
},
admin: {
useAsTitle: 'name',
defaultColumns: ['name', 'slug', 'updatedAt'],
},
fields: [
{
name: 'name',
type: 'text',
label: 'Nome',
required: true,
},
{
name: 'status',
type: 'group',
fields: [
{
name: 'active',
type: 'checkbox',
defaultValue: true,
},
{
name: 'featured',
type: 'checkbox',
defaultValue: false,
},
],
},
{
type: 'tabs',
tabs: [
// ...lots of stuff here...
{
label: 'Location',
fields: [
{
name: 'locations',
type: 'group',
fields: [
{
name: 'state',
type: 'relationship',
relationTo: 'states',
required: true,
hasMany: false,
// This field will trigger the filtering of cities
},
{
name: 'cities',
type: 'relationship',
relationTo: 'cities',
required: true,
// hasMany: true,
filterOptions: ({ data }) => {
// Filter cities based on selected state
if (data?.state) {
return {
state: { equals: data.state },
active: { equals: true },
}
}
return { active: { equals: true } }
},
},
{
name: 'neighborhoods',
type: 'array',
admin: {
condition: (data, siblingData) => {
// Only show if any selected city has neighborhoods
return siblingData?.cities?.some((city) => city.hasNeighborhoods)
},
},
fields: [
{
name: 'city',
type: 'relationship',
relationTo: 'cities',
required: true,
hasMany: false,
admin: {
condition: (data, siblingData) => {
// Only show cities that have neighborhoods
return siblingData?.cities?.hasNeighborhoods
},
},
},
{
name: 'selectedNeighborhoods',
type: 'select',
hasMany: true,
admin: {
isClearable: true,
isSortable: true,
},
options: async () => {
// Dynamically load neighborhoods based on selected city
if (siblingData?.city) {
const city = await payload.findByID({
collection: 'cities',
id: siblingData.city,
})
return city?.neighborhoods
.filter((n) => n.active)
.map((n) => ({
label: n.name,
value: n.name,
}))
}
return []
},
},
],
},
],
},
],
},
],
},
],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment