Stats do not update on save

Hi everyone,

I’m using the great stats feature and found, that the stats won’t update when changing the page data. Is this something that’s normally working out of the box?

YAML config:

...
      stats:
        type: stats
        label: Status
        size: medium
        reports: page.reports
...

Reports logic:

<?php

use Kirby\Cms\Page;

class CoursePage extends Page
{
  public function reports(): array
  {
    $participants = $this->content()->get('attendees')->toStructure()->count();
    $limit = $this->content()->get('limit')->value() ?? 8;

    return [
      [
        'label' => 'Auslastung',
        'value' => "$participants/$limit Plätze belegt",
        'icon' => 'users',
      ],
    ];
  }
}

Is see the stats box with correct values, but if I add attendees or change the limit, the stats still show the old values after save. Only after (browser) reloading the panel page or navigating back and forth in the panel shows the new calculated stats values.

image

Thankful for any pointers on this.

That is expected behavior, yes, because this data comes from the backend, therefore does not automatically update whenever you change something somewhere.

Good to know, thanks for making that clear. I just thought that the stats are being recalculated when the page is saved (which might make sense imho :innocent: ). Is there any way to trigger a stats update programmatically?

You could probably extend the stats section and call window.panel.view.refresh() on save. This will only rerender the parts that have changed according to the props.

I could not find any information about the event types in the panel, though.

Update: you can overwrite the k-stats component’s created() method and add an event for model.update. There’s no documentation for any of this, and I also couldn’t make the refresh work. I give up now, but here’s what I’ve got:

panel.plugin('my/stats', {
  components: {
    'k-stats': {
      extends: 'k-stats',
      created() {
        window.panel.events.on('model.update', () => {

          // Doesn't work
          // window.panel.view.refresh();

          // Doesn't work
          // window.panel.view.reload();

          // Doesn't work
          // window.panel.events.emit('panel.reload');

          // Annoying
          window.location.reload();

        })
      }
    },
  },
});
2 Likes

Wow, thank you so much Thomas for trying. I’ll check that out and see if I can find something that fits my case.