Skip to content

Commit

Permalink
Send message complete fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Raccoon254 committed May 19, 2024
1 parent ca6edb5 commit f4fc780
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 3 deletions.
47 changes: 46 additions & 1 deletion app/Livewire/SendMessageInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace App\Livewire;

use App\Models\Attachment;
use App\Models\Message;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use Livewire\Component;
use Livewire\WithFileUploads;
Expand All @@ -11,13 +15,54 @@ class SendMessageInput extends Component
{
use WithFileUploads;

public $message;
public Message $message;
public string $newMessage = '';
public array $attachments = [];
public User $recipient;
public mixed $loggedInUser;

public function mount($selectedUser): void
{
$this->recipient = $selectedUser;
$this->loggedInUser = Auth::user();
}

public function removeAttachment($name): void
{
$this->attachments = $this->attachments->filter(function ($attachment) use ($name) {
return $attachment->getClientOriginalName() !== $name;
});
}

public function sendMessage(): void
{
if (empty($this->newMessage) && empty($this->attachments)) {
return;
}

$message = Message::create([
'sender_id' => $this->loggedInUser->id,
'receiver_id' => $this->recipient->id,
'content' => $this->newMessage,
]);

if ($this->attachments) {
foreach ($this->attachments as $attachment) {
$path = $attachment->store('public/attachments');
Attachment::create([
'message_id' => $message->id,
'name' => $attachment->getClientOriginalName(),
'path' => $path,
'type' => $attachment->getMimeType(),
'size' => $attachment->getSize(),
]);
}
}

$this->attachments = [];
$this->newMessage = '';

$this->dispatch('messagesSent');
}

public function render(): View
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"php": "^8.2",
"laravel/framework": "^11.0",
"laravel/tinker": "^2.9",
"livewire/livewire": "^3.4"
"livewire/livewire": "^3.4",
"symfony/filesystem": "^7.0"
},
"require-dev": {
"fakerphp/faker": "^1.23",
Expand Down
66 changes: 65 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f4fc780

Please sign in to comment.