jeroennoten / Laravel-AdminLTE

Easy AdminLTE integration with Laravel
MIT License
3.78k stars 1.08k forks source link

[FEATURE] Dynamic notifications in the sidebar #1264

Closed Dereavy closed 3 months ago

Dereavy commented 3 months ago

Is your request related to a problem? Please describe.

A clear and concise description of what the problem is. Ex. I'm always frustrated when...

i would like to update the notifications in the sidebar menu. I couldn't find any documentation on how to update the notification labels in the sidebar, I tried following the documentation and added the update_cfg property to the sidebar menu item and setting a notification route etc.. however the controller was never reached.

There is also a missing option when it comes to dynamically editing the menu. You have "add_in", "add_after" and "add_before". "add_in" only adds new items to the submenu. There is no option to edit/modify an existing menu in a dynamic way, I've described my own implementation / proposed solution below.

Describe the solution you'd like

A clear and concise description of what you want to happen or the enhancement you want. I'd like to have the update_cfg property work for the sidebar, so it's possible to update the notification labels.

Describe alternatives you've considered

A clear and concise description of any alternative solutions or features you've considered.

The alternative solution I am currently using:

I modified the "/vendor/jeroennoten/laravel-adminlte/src/Menu/Builder.php" class so that it had an extra "edit" method so that I could dynamically edit the notification labels.

class Builder
{
// ...
protected const EDIT = 3;
// ...
public function edit($itemKey, ...$newItems)
    {
        $this->addItem($itemKey, self::EDIT, ...$newItems);
    }
// ...
protected function addItem($itemKey, $where, ...$items)
    { ...
    } else if ($where === self::EDIT) {
            $targetPath = implode('.', $itemPath);
            $targetArr = Arr::get($this->menu, $targetPath, []);
            $targetArr=array_merge($targetArr, ...$items);

Then in the AppServiceProvider I could then modify the notifications as needed :

            $event->menu->edit('key',[
                'label'       => 4,
                'label_color' => 'danger',
            ]);

With this method, I have a way to update and modify notifications in the sidebar. This method can be improved to actually replace existing values since it just adds the new values for the time being.

Additional context

Add any other context or screenshots about the feature request here.

dfsmania commented 3 months ago

Hi @Dereavy , first and unfortunately there isn't any built-in navbar notification like item for the sidebar.

Second, the menu is only built when a request to a new page takes place or when you refresh the page, under that situation the package triggers the BuldingMenu event that you can use to build the menu dynamically by listening to that event. The add_in, add_before, etc, are extra methods that you may use to mix a static menu (defined on the configuration) with some dynamic modifications.

If you need a menu entry in the sidebar that is updated periodically, without client interaction, then you are going to need AJAX calls from the client-side to the server-side, so when the AJAX call respond you can use Javascript/jquery to update the related DOM elements in the client-side.

Also, note the new method edit that you've created is somehow redundant, because executing the next actions when listening to the BuildingMenu event:

// Assuming next item is defined in the configuration
/*
[
    'key' => 'pages',
    'text' => 'Pages',
    'url' => 'admin/pages',
    'icon' => 'far fa-fw fa-file',
]
*/
...
...
...
$event->menu->edit('pages', [
    'label' => 4,
    'label_color' => 'danger',
]);

Is just the same as creating the entire menu entry dynamically with add_after, add_before, etc...

$event->menu->add_after('some_key', [
    'key' => 'pages',
    'text' => 'Pages',
    'url' => 'admin/pages',
    'icon' => 'far fa-fw fa-file',
    'label' => 4,
    'label_color' => 'danger',
]);

I'm not really sure what are you trying to achieve, or what's your use case here. But, if you need a sidebar menu entry that is updated periodically, then you are going to need to create a custom sidebar menu item and AJAX calls using Javascript to update that element. To create custom sidebar item you may use the issue #926 as reference

Dereavy commented 3 months ago

In this image is a calendar event from the live preview : image The idea would be for when the user loads the page, the amount on the notification (2) is up to date with whatever it is supposed to represent (for example new events or pages).

To achieve this, I am now using the add_after or add_before since I didn't know they could overwrite, I used to think they would just insert a new menu item instead of overwriting an existing one.

Although it is possible to update with AJAX, since the feature exists for the navbar, I was just wondering if it was possible to have a built-in navbar notification like item for the sidebar.

Thank you for taking the time to respond.

dfsmania commented 3 months ago

Just a note about:

To achieve this, I am now using the add_after or add_before since I didn't know they could overwrite, I used to think they would just insert a new menu item instead of overwriting an existing one.

They don't overwrite, those methods adds a new item after or before an existing one, but only during the BuildingMenu process. You need to note that when you request a new view that extends the provided template, a request-response cycle takes place between the client and the server, in that cycle, the menu is re-generated completely again.