Access collection data in Smart chart

Hi,

I’m displaying analytics on a collection object. I would like to show a smart chart where I retrieve data from an API. I need to send the name of my data object (organisation.name for example, where organisation is a record in my collection).

How can I get the record field in my component code like this (see %%RECORD_NAME%%:
async fetchData() {
const response = await this.lianaServerFetch.fetch(’/forest/api/getOrgData?org_name=%%RECORD.NAME%%’, {});
this.data = await response.json();
console.log("Record count: " + this.data.length);
}
?
Thank you,
Florian.

Hi @Florian_Sureau,

Thanks for reaching out!

If I understood well, you want to display an API-based chart for a specific record (in the Analytics tab of that record). Is that correct?

As you can see on that documentation:

For API charts, the parameter record_id is automatically passed in the HTTP body to access the record the user is currently seeing.

From this, you should be able to find your specific record :slight_smile:

Let me you if that helps you out.
Thanks!

Thank you very much @anon34731316 , I didn’t see this line in the documentation but it should be perfect.
Florian.

1 Like

Well…
I created the chart this morning, saved it, multiple times, on my dev env. Going back with the solution this afternoon, after a reload of the page, I cannot see my chart anymore.
I’ll give more details if it happens again.

Hi again @anon34731316 ,
In fact, this is not exactly what I need. I’m not creating an API chart. But a smart chart calling an API, to build my own UI.
So in this case I don’t have this parameter.

@Florian_Sureau,

Could you try something like this?

import { getRouteInfo } from 'client/utils/route-utils';
​
export default class extends Component {
  @service router;
  
  get recordId() {
	  const currentRouteInfo = this.router.currentRoute;
	  const viewEditRouteInfo = getRouteInfo(currentRouteInfo, 'project.rendering.data.collection.list.viewEdit');
	  
	  return viewEditRouteInfo.params.record_id;
  }
}

Let me know if this helps you out :pray:

Hi @anon34731316

I can get the routeInfo, but viewEditRouteInfo.params is null.

Florian.

@Florian_Sureau,

I just tried on my end with this code for the component:

import Component from '@glimmer/component';
import { getRouteInfo } from 'client/utils/route-utils';
import { inject as service } from '@ember/service';

export default class extends Component {
  @service router;
  
  get recordId() {
	  const currentRouteInfo = this.router.currentRoute;
	  const viewEditRouteInfo = getRouteInfo(currentRouteInfo, 'project.rendering.data.collection.list.viewEdit');
	  
	  return viewEditRouteInfo?.params?.record_id;
  }
}

And I am displaying it in the template like this:

{{this.recordId}}

It is working when being on the record analytics tab (but not on the editor, as the route is not the good one…).
Please tell me if it suits your need!

Thanks.

Well, the edition and code ran behind is not predictable.
I stop there.

@Florian_Sureau you’re right, this is not really handy :confused:
We need to find a way to improve the experience.

Meanwhile, you may want to use a fake value in the editor to see what it’d look like? And then replace with the recordId when making it live?

Sorry for the inconvenience!

@Florian_Sureau I just created a bug task on our side so that we can solve this issue asap.
I’ll let you know as soon as it is fixed :raised_hands:

Thanks for raising it.

Very weird behaviour:
I change my code, save, go to analytics tab, then when I look at the logs, an old version is running. I’ll send a video.
I’m not able to use it. I’ll wait for a new version.

Hello @Florian_Sureau,

We just released a fix so that you can access to the record directly in both smart chart editor and display.

You should be able to use it with @record in the template and this.args.record in the component (and obviously this record object contains the id).
I updated the documentation accordingly.

I change my code, save, go to analytics tab, then when I look at the logs, an old version is running. I’ll send a video.

Even after a browser refresh?
(The fact that you need a browser refresh does not feel right though, we need to fix that too)

Thanks!

1 Like