#Manage module links from the Admin UI

13 messages · Page 1 of 1 (latest)

inner elk
#

I have created a custom module (brand) with associated service, workflows, API routes and admin page/components to upsert entries. I have defined links between the module and products. Using CURL I am able to associate a brand with a product using the "additional_data"-parameter sent to "/admin/products". I am currently hooking in to the updateProductsWorkflow.hooks.productsUpdated() to achieve this.

Now I would like to create a custom widget that allows editors to associate a product with a brand while editing a product in the Admin UI.

I have created a widget that injects in the "product.details.side.before"-zone. The widget is fetching all available brands from a custom API-route and maps the entries inside a <Select>-component. This is where I am unsure on how to proceed.

The props passed to the widget contains product-data of type <AdminProduct> which does not contain my "brands" field so in order to fetch the current associated brand for the product I am using the SDK to fetch the product once again with the "?fields=+brand.*" parameters. I am not sure if this is the correct way of doing things.

Now here is where I need help.

In order to edit/update or delete the associated brand (essentially defined links) with the product currently being edited should I:

  1. Create a workflow that manages linkdefinitions between the brand-module and product that is called from a custom API-route that my widget is requesting?
  2. Send a POST-request to "/admin/products" from my widget with brand-id in the additional_data-parameter and hook into the updateProductsWorkflow.hooks.productsUpdated()?
  3. Create a workflow that manages linkdefinitions between the brand and product and call it from the widget directly?
  4. Do things completely different?

Any help appreciated!

red relic
#

@inner elk Create a custom API route that updates takes brand id, data and update it using the brandModuleService. You don't even need a workflow for this use case. Workflow is generally meant for multi step processes which need rollback or advanced control

inner elk
#

@red relic Thanks, I got it working. I am unsure however on how to update the current brand link on a product instead of adding a new one (one-to-many).

This is my link definition:

export default defineLink(
{
linkable: ProductModule.linkable.product,
deleteCascade: true,
isList: false
},
BRAND_MODULE.linkable.brand,
);

This is code from my route where I create the link between the product and a brand:

const link = await linkService.create({
[Modules.PRODUCT]: {
product_id: productId,
},
[BRAND_MODULE]: {
brand_id: brandId,
},
});

If a product has a link to a brand already another one is added making the brands an array of brand-objects. How can I update instead of creating a new one if one is present?

red relic
#

Looks like you are using the same code to add and update. Update should not execute the link again. Instead you should check if a product has a brand already, if yes then directly update the existing brand entity

inner elk
#

How do I check if a product has a brand already? I can use linkService.list() but I only get a result if the current brandId is provided like below:

const existingLink = await linkService.list({
  [Modules.PRODUCT]: {
    product_id: productId,
  },
  [BRAND_MODULE]: {
    brand_id: brandId,
  },
})

The incoming data for brandId contains my new brand that needs to be set instead of the one that is linked already

red relic
inner elk
#

I'm probably missing the core concept, yes. The docs are quite clear on how to create and dismiss links between two records of different data models, but not on how to update them.

I managed to query the product to see the currently linked record but what am I supposed to to with the result?

red relic
inner elk
#

Maybe i'm being unclear. I'm not interested in updating data for the brand itself. I'm looking to update the association between a brand record and a product record.

I'm trying to assocciate a brand (Adidas) with a shoe (SneakerX). An editor should be able to edit product details for the shoe and selct a brand from a list of brand records (Adidas, Nike, Rebook). A one-to-one link should be made between the two. There should only be one associated brand with a product at once.

red relic
inner elk
#

That was my initial thought also hence the previous message regarding the linkService.list() method. However I was hoping there would be a better way to do this.

red relic
#

You could try creating github issue. If there's a hidden method to update a link without dismissing then the core team will be aware of it

inner elk
#

I'll do that. For now, I was able to resolve my initial issue by providing the currentBrandId in the POST request and dissmissing the link before creating a new one.