From acb73078d9ac44d48fd79d2e4fd2042f342549c9 Mon Sep 17 00:00:00 2001 From: Chri$ Date: Wed, 8 Jan 2025 14:56:20 +0100 Subject: [PATCH] feat:[lar-153] add mention user in discussion comment --- .../Components/Discussion/Comments.php | 63 ++++++++++++++++++- .../components/discussion/comments.blade.php | 34 +++++++++- 2 files changed, 94 insertions(+), 3 deletions(-) diff --git a/app/Livewire/Components/Discussion/Comments.php b/app/Livewire/Components/Discussion/Comments.php index 58b524cf..6e9bcd86 100644 --- a/app/Livewire/Components/Discussion/Comments.php +++ b/app/Livewire/Components/Discussion/Comments.php @@ -30,9 +30,29 @@ final class Comments extends Component implements HasForms public ?array $data = []; + public Collection $users; + + public string $query = ''; + + public bool $showSuggestions = false; + + public Collection $mentionables; + public function mount(): void { $this->form->fill(); + + $this->mentionables = $this->users = $this->discussion + ->replies() + ->with('user') + ->get() + ->pluck('user') + ->unique('id') + ->map(fn ($user) => [ + 'id' => $user->id, + 'key' => $user->name, + 'username' => $user->username, + ]); } public function form(Form $form): Form @@ -43,11 +63,48 @@ public function form(Form $form): Form ->hiddenLabel() ->placeholder(__('pages/discussion.placeholder')) ->rows(3) - ->required(), + ->required() + ->debounce() + ->extraAttributes(['x-ref' => 'textarea']) + ->afterStateUpdated(function ($state, callable $set): void { + + if ($state) { + $this->query = $state; + $this->searchUsers($this->query); + } + + }), ]) ->statePath('data'); } + public function searchUsers(string $query): void + { + if (str_contains($query, '@')) { + $mention = explode('@', $query); + $search = end($mention); + + $this->users = $this->mentionables->filter(fn ($user) => str_contains(strtolower($user['key']), strtolower($search)) || + str_contains(strtolower($user['username']), strtolower($search))); + + if ($this->users->isNotEmpty()) { + $this->showSuggestions = true; + $this->dispatch('showSuggestionEvent'); + } + + } else { + $this->users = collect([]); + $this->showSuggestions = false; + } + + } + + #[On('setBodyForm')] + public function setBodyForm(?string $query): void + { + $this->form->fill(['body' => $query]); + } + public function save(): void { $this->validate(); @@ -91,6 +148,8 @@ public function comments(): Collection public function render(): View { - return view('livewire.components.discussion.comments'); + return view('livewire.components.discussion.comments')->with([ + 'users' => $this->users, + ]); } } diff --git a/resources/views/livewire/components/discussion/comments.blade.php b/resources/views/livewire/components/discussion/comments.blade.php index 4306e412..bcb17074 100644 --- a/resources/views/livewire/components/discussion/comments.blade.php +++ b/resources/views/livewire/components/discussion/comments.blade.php @@ -10,12 +10,44 @@ @endif @auth -
+ +
{{ $this->form }} +
+
    + +
+
+
{{ __('actions.save') }}