Files
einundzwanzig-portal/app/Http/Livewire/Chat/HighscoreChat.php
Shift 5776b01d15 Apply Laravel coding style
Shift automatically applies the Laravel coding style - which uses the PSR-12 coding style as a base with some minor additions.

You may customize the code style applied by configuring [Pint](https://laravel.com/docs/pint), [PHP CS Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer), or [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) for your project root.

For more information on customizing the code style applied by Shift, [watch this short video](https://laravelshift.com/videos/shift-code-style).
2023-02-19 18:05:16 +01:00

73 lines
1.8 KiB
PHP

<?php
namespace App\Http\Livewire\Chat;
use App\Events\ChatMessageSentEvent;
use Livewire\Component;
class HighscoreChat extends Component
{
public bool $open = false;
public array $messages = [];
public string $myNewMessage = '';
public function rules()
{
return [
'myNewMessage' => 'required|min:1|max:255',
];
}
public function getListeners()
{
return [
'toggleHighscoreChat' => 'toggle',
'echo:plebchannel,.App\Events\ChatMessageSentEvent' => 'chatMessageSent',
];
}
public function mount()
{
$this->messages = cache()->get('highscore_chat_messages', []);
}
public function toggle()
{
$this->open = ! $this->open;
}
public function chatMessageSent()
{
if (auth()->check()) {
$this->messages = cache()->get('highscore_chat_messages', []);
$this->dispatchBrowserEvent('chat-updated');
}
}
public function sendMessage()
{
$this->validate();
$newMessages = collect(cache()->get('highscore_chat_messages', []))
->push([
'fromId' => auth()->id(),
'fromName' => str(auth()->user()->name)->limit(2),
'userImg' => str(auth()->user()->profile_photo_url)->replace('background=EBF4FF', 'background=F7931A'),
'message' => $this->myNewMessage,
'time' => now()->asDateTime(),
])
->take(-21)
->toArray();
cache()->set('highscore_chat_messages', $newMessages);
event(new ChatMessageSentEvent());
$this->messages = $newMessages;
$this->myNewMessage = '';
}
public function render()
{
return view('livewire.chat.highscore-chat');
}
}