Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Is there a way to run a function when a user is inside the collection view?

default discord avatar
eddieDevlast year
14

Hello. When a user edits a collection, I need to know if there is an edit flag or something that will tell me that the user is editing the collection. The reason being, I need to remove a tag when a user is editing the collection. I did see

views.Edit

in the documentation:

https://payloadcms.com/docs/admin/components#collections

, but as I understand

views.Edit

only passes in components, which isn't what I need. I need to be able to pass in a function when the user is inside the collection editing. Any ideas?


Thanks.

  • default discord avatar
    tinoutilast year
    The reason being, I need to remove a tag when a user is editing the collection.

    Do you mind expanding a bit on what you're trying to achieve?


    For example:


    - when you say "remove a tag", do you mean "update a relationship field value", or is it a custom component thing?


    - when you say "is editing the collection", do you mean "the user has changed a value in the database", or "the user in on the page"?



    Having more context would help us help you find the best solution. 😊

  • default discord avatar
    eddieDevlast year

    Hey @tinouti . Sure thing!


    - remove tag: When a user is inside of the collection view.Edit page, if the collection is being edited, (data is being updated and user has changed value), I need to remove any tags related to prod for security reasons.


    - editing collection: user is inside the view.Edit page of that collection (data is being updated and user has changed value).


    Hope this gives you a bit more context. Ty!



    So in general, I need to be able to detect if the user has clicked inside of the collection to edit.


    I need to run a function to remove a specific tag if the user has made any changes i.e. data has been updated.


    If no data has been updated, function does nothing.

  • discord user avatar
    jmikrut
    last year

    you could build a custom

    Provider

    that uses React Router to monitor the route of the user



    and if they navigate to the route(s) in question, you could fire off callbacks



    https://payloadcms.com/docs/admin/components#custom-providers
  • default discord avatar
    eddieDevlast year

    thanks jm! will take a look



    I was trying to use the beforeOperation hook to see if url includes the collection id. If it does, it would mean they are in the edit page. Does that sound correct? Also How would I update the data? Would a call to /api/collectionName/id and changing that data make an update?



    If this doesn't work I'll definitely go with the context solution, but the question would still remain. How would I update the data to reflect changes to server and client? ie remove a specific tag in the data so that the client will see that that tag is no longer there before submitting any changes.

  • discord user avatar
    jmikrut
    last year

    you can definitely look at the

    req

    to determine if the update is coming from the admin UI.... but that's not really a pattern that we see often. I'm sure you have your reasons, but does it make a difference if the update comes from admin UI, or straight form REST / GraphQL? Something to consider



    and if you are going to look at the

    req

    from a hook, could you use the

    beforeChange

    hook instead of

    beforeOperation

    ?



    the reason i'm asking about which hook you want to use is because to update the data in a

    beforeOperation

    hook, you would need to parse different argument properties based on which type of operation you are looking at. If it's

    read

    , you will not have

    data

    or

    doc

    available, because it's

    before the operation

    . If it was

    update

    or

    create

    , then you would have the

    data

    argument, which you could then simply transform

  • default discord avatar
    eddieDevlast year

    Thanks @jmikrut! I am going to go with the custom Provider approach. What you said does make sense as I do need the data argument and was wondering how to get it. So I'll create a custom provider that uses React Router to monitor the route of the user. And when they navigate to said route, I will trigger my functions as needed. Then I can use the beforeChange hook to do what I need. Does that sound about right? Thanks for the help!

  • discord user avatar
    jmikrut
    last year

    100%

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.