Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Fix) Bug #3752 #3782

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions app/Http/Livewire/UserNotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use App\Models\User;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Url;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Livewire\WithPagination;

Expand All @@ -29,15 +28,11 @@ class UserNotes extends Component

#TODO: Update URL attributes once Livewire 3 fixes upstream bug. See: https://github.com/livewire/livewire/discussions/7746

#[Url(history: true)]
#[Validate('required|filled')]
public string $message = '';

/**
* @var array<int, string>
*/
#[Url(history: true)]
#[Validate('array')]
public array $messages = [];

#[Url(history: true)]
Expand All @@ -49,6 +44,23 @@ class UserNotes extends Component
#[Url(history: true)]
public string $sortDirection = 'desc';

/**
* @var array<mixed>
*/
protected array $rules = [
'message' => [
'required',
'filled',
],
'messages' => [
'array',
],
'messages.*' => [
'required',
'filled',
]
];

final public function mount(): void
{
$this->messages = Note::where('user_id', '=', $this->user->id)->pluck('message', 'id')->toArray();
Expand All @@ -73,11 +85,14 @@ final public function render(): \Illuminate\Contracts\View\Factory|\Illuminate\C
]);
}

/**
* @throws \Illuminate\Validation\ValidationException
*/
final public function store(): void
{
abort_unless(auth()->user()->group->is_modo, 403);

$this->validate();
$this->validateOnly('message');

Note::create([
'user_id' => $this->user->id,
Expand All @@ -90,11 +105,15 @@ final public function store(): void
$this->dispatch('success', type: 'success', message: 'Note has successfully been posted!');
}

/**
* @throws \Illuminate\Validation\ValidationException
*/
final public function update(int $id): void
{
abort_unless(auth()->user()->group->is_modo, 403);

$this->validate();
$this->validateOnly('messages');
$this->validateOnly('messages.*');

Note::whereKey($id)->update([
'staff_id' => auth()->id(),
Expand Down
1 change: 1 addition & 0 deletions resources/views/livewire/user-notes.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class="form__textarea"
name="message"
placeholder=" "
wire:model="messages.{{ $note->id }}"
value="{{ $note->message }}"
Comment on lines 94 to +95
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall there being bugs when I did this initially when editing user notes. Your mileage may vary. I can't remember what issue I ran into exactly but I believe there were conflicts with the wire:model. Just an fyi.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me. Tested it locally and has been in prod for over a week. If something else pops up a new issue can be opened.

></textarea>
<label
class="form__label form__label--floating"
Expand Down
Loading