omnia-digital / livewire-calendar

Laravel Livewire component to show Events in a good looking monthly calendar
MIT License
74 stars 9 forks source link

How to add custom filters on page with omnia-digital/livewire-calendar ? #30

Closed sergeynilov closed 1 month ago

sergeynilov commented 4 months ago

In laravel 10 app / livewire/livewire 3.4.10 I use omnia-digital/livewire-calendar 3.1 and I need to add custom filters to show only data by selected filters.

I published livewire-calendar files and in file resources/views/livewire/admin/calendar/before.blade.php added several select inputs :

    <div >
        ...
        <div class="editor_field_block_wrapper flex">
            <div class="w-6/12">
                <div class="editor_field_block_device_splitter">
                    <div class="w-4/12 pb-0 pl-2 md:pt-3 ">
                        <label for="filter_user_id" class="editor_field_block_device_label">User id</label>
                    </div>
                    <div class="xl:w-8/12 w-full p-1 pl-2 pr-2 mt-1">
                        <select wire:model.blur="filters.user_id" class="editor_form_input" tabindex="10"
                                id="filter_user_id">
                            <option value=""> - Select all -</option>
                            @foreach($staffUsersSelectionItems as $key=>$label)
                                <option value="{{$key}}">{{$label}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>
            <div class="w-6/12">
                <div class="editor_field_block_device_splitter">
                    <div class="w-4/12 pb-0 pl-2 md:pt-3 ">
                        <label for="filter_priority" class="editor_field_block_device_label">Priority</label>
                    </div>
                    <div class="xl:w-8/12 w-full p-1 pl-2 pr-2 mt-1">
                        <select wire:model.blur="filters.priority" class="editor_form_input" tabindex="20"
                                id="filter_priority">
                            <option value=""> - Select all -</option>
                            @foreach($taskPrioritySelectionItems as $key=>$label)
                                <option value="{{$key}}">{{$label}}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>
        </div>
        ...

and in component app/Livewire/Admin/TasksDeadlineAtCalendar.php I added array of filters and try to reload data when any filter select inputs is changed :

class TasksDeadlineAtCalendar extends LivewireCalendar
{
    public $tasks = [];
    public array $taskPrioritySelectionItems = [];
    public $staffUsersSelectionItems = [];
    public $taskCategoriesSelectionItems = [];

    #[Session]
    public array $filters
        = [
            'user_id' => '',
            'task_category_id' => '',
        ];

    public function render()
    {
        return parent::render();
    }

    public function events() : Collection
    {
        $this->readDbData(); // Read data from db
        return $this->tasks;
    }

    public function updated($property, $value)
    {
//        dd("updated::" . $property);
        $this->readDbData();
        $this->events(); // I try to call events redraw - but that does not work
    }

    public function readDbData()
    {
        // Prepare data for select inputs
        $this->taskPrioritySelectionItems = TaskPriorityEnum::getSelectionItems();
        $staffUsersIds = ModelHasPermission::get()->pluck('model_id');
        $this->staffUsersSelectionItems = User::whereIn('id', $staffUsersIds)->orderBy('name')->get()->pluck('name', 'id');

        $this->taskCategoriesSelectionItems = TaskCategory::orderBy('name')->get()->pluck('name', 'id');
        $this->tasks = Task
            ::getByUserId($this->filters['user_id'])
            ->onlyActiveWithDeadlineAt()
            ->getByPriority(TaskPriorityEnum::tryFrom($this->filters['priority']))
            ->getByTaskCategoryId(TaskPriorityEnum::tryFrom($this->filters['task_category_id']))
            ->get()
            ->map(function ($task) {
                return ['id' => $task->id, 'title' => $task->title, 'description' => $task->content, 'date' => $task->deadline_at ];
            });
    }

data from db are read ok when page is opened with empty filter select inputs. But when one of filter select inputs is changed "updated" is called and data from db are read, but I try to call events redraw - but that does not work.

How can I implement it ?

joshtorres commented 4 months ago

Hello sorry for my delay. Since this doesn't seem to be an issue with the repo and more a question of how to use it, please create this in the discussions page. I don't have time to look into it this week but I'll take a look when as soon as I get a chance.