#Determining Subcategories within a Tenants Tag Groups

88 messages · Page 1 of 1 (latest)

balmy lanceBOT
#

Guys how can I determine the subcategories of category.

Imagine I select the tenant X

I just want to show the tag groups of this related tenant.

A tenant has multiple tag groups and a user can belong to multiple tenants.

Entities in a nutshell.

  • Tenant:

    • Has many Tag Groups
    • Has many Products
  • Tag Group:

    • Belongs to one Tenant
    • Has many Tags
  • Tag:

    • Belongs to one Tag Group
    • Belongs to one Tenant
    • References many Products
  • Product:

    • References many Tags
    • Belongs to one Tenant

These relationships outline how the entities are interconnected within the system.

cloud spireBOT
#

Original message from @magic blade - Moved from #general message

arctic shale
#

Looking at this now

#

So the tag group select isn't populated?

magic blade
#

The issue arises when I create a new tag.
In creation time, all the tag groups associated with user tenants continue to be displayed, which can clutter the interface and cause inconsistency in db.

arctic shale
#

Hmm, if you detail out the issue a bit more with a screenshot, I can try to make some suggestions

#

Just a heads up, I'm running to lunch shortly, but will continue to check this thread when i return

magic blade
#

The user has access to Tenant A and Tenant B
When I select tenant A, it must show the tag groups associated with the selected tenant (Tenant A) until there the Tag Groups must be disabled.

#

Thanks @arctic shale

magic blade
#

@arctic shale are you there?

arctic shale
#

Hey back

#

So what are you doing with the tag groups now

#

just showing all of them?

#

@magic blade

magic blade
#

yes

arctic shale
#

Can you post your collection here so I can see your logic?

magic blade
#

Flow,

  1. User logs in
  2. Selects a tenant
  3. When creating something (tags / tag groups) show only the entities of that tenant.
arctic shale
#

Gotcha

#

Okay yeah, share your collection code and Ill see what's up

#

(you can just paste it right in here)

magic blade
#

export const Users: CollectionConfig = {
  slug: 'users',
  admin: {
    useAsTitle: 'email',
  },
  auth: {
    depth: 0,
  },
  fields: [
    {
      name: 'tenants',
      type: 'relationship',
      relationTo: 'tenants',
      hasMany: true,
      saveToJWT: true,
    },
  ],
}

export const Tenants: CollectionConfig = {
  slug: 'tenants',
  admin: {
    useAsTitle: 'name',
  },
  fields: [
    {
      name: 'name',
      type: 'text',
      required: true,
    },
    {
      name: 'users',
      type: 'relationship',
      relationTo: 'users',
      hasMany: true,
    },
    {
      name: 'tag_groups',
      type: 'relationship',
      relationTo: 'tag_groups',
      hasMany: true,
    },
  ],
}

export const TagGroups: CollectionConfig = {
  slug: 'tag_groups',
  admin: {
    useAsTitle: 'name',
  },
  fields: [
    {
      name: 'name',
      type: 'text',
      required: true,
    },
    {
      name: 'tenant',
      type: 'relationship',
      relationTo: 'tenants',
      required: true,
    },
  ],
}

export const Tags: CollectionConfig = {
  slug: 'tags',
  admin: {
    useAsTitle: 'name',
  },
  fields: [
    {
      name: 'name',
      type: 'text',
      required: true,
    },
    {
      name: 'tag_group',
      type: 'relationship',
      relationTo: 'tag_groups',
      filterOptions: ({ user }) => {
        return {
          tenant: {
            // I need to find a way to get the tenant from the user (after login showing a dropdown of all available tenants, or select the tenant from a dropdown
            // Hardcoded to the first tenant
            equals: user!.tenants![0],
          },
        }
      },
      required: true,
    },
  ],
}
arctic shale
#

@magic blade So in your filterOptions method, you also have access to siblingData

#

which will give you the value of the other fields in the collection

#
      filterOptions: ({ user, siblingData }) => {
        return {
          tenant: {
            // I need to find a way to get the tenant from the user (after login showing a dropdown of all available tenants, or select the tenant from a dropdown
            // Hardcoded to the first tenant
            equals: user!.tenants![0],
          },
        }
      },
#

Can you log, console.log(siblingData) before the return statement and tell me if you get the sibling field data

#

Then we can continue

magic blade
#

In create

arctic shale
#

What's that?

magic blade
arctic shale
#

Ah yes, like that

#

what does it log?

#

it should be the other fields in the collection and their values

magic blade
#

Im using pgsql

arctic shale
#

what does it log?
it should be the other fields in the collection and their values

#

Let me know if you need additional instruction

magic blade
#

Alright

#

im using payload 3.0 beta

arctic shale
#

That shouldn't matter here, we're just trying to see what siblingData is

#

You put the log statement in

#

Now if you view the collection, it should log the value to your console

magic blade
#

sorry

arctic shale
#

It's okay take your time

magic blade
#

this is just showing the id's of tag groups.

arctic shale
#

Check out the example here:

#

For the first part of this, you just need to confirm that you can access the sibling data

magic blade
#

I have the following.

tag groups

arctic shale
#

To confirm

#

You should be going to your collection

#

Then checking your log for the siblingData

#

Does that make sense?

#

And I think in particular, youll want to get the value of the "users" field

#

the collection in question only has like two fields

magic blade
#

I have this tags

#

My user is associated with tenant A1

When I create a new tag it shows all the tag groups of all tenats

arctic shale
#

Why didn't equals: user!.tenants work

#

You said that you want to get the tennant from the user

magic blade
arctic shale
#

what's wrong with tenants[1] ?

magic blade
arctic shale
#

ohhh

magic blade
#

Sorry if my english is not very good

arctic shale
#

Nah it's alright, its impressive you know more than 1 lang

magic blade
#

I saw a lot of people with this kind of problems

#

Thanks!

arctic shale
#

Ah I see

#

I'm happy to help you out with this more, but I do have to run an errand

#

Can you wait a little bit?

#

Shouldn't be more than an hour

magic blade
#

Off course. Thanks, please don't forget this issue. We're really trying to migrate to PayloadCMS, we're in love with this project.

magic blade
#

@arctic shale are you back?

magic blade
#

@short juniper can you help me here?

arctic shale
#

Good morning

magic blade
#

Hello

#

Do you know how can I pass the filterOptions data to the custom component?

arctic shale
#

Hey - did you solve this yet?

#

I am back

magic blade
#

Not yet

#

How can I access the filter Options data to the custom component?

#

I need to do an API call?

arctic shale
#

Generally in a custom functional component, you define what props it can take in

#

Then you pass the props wherever it is constructed

#

MyCustomComponent(filterOptions, other, stuff)

magic blade
#

I need to pass the data in the default relationship (the default select)