laravel reverb working

This commit is contained in:
guillaume91 2024-05-28 16:38:27 +02:00
parent 336bafd6f4
commit faa2b5a782
19 changed files with 197 additions and 56 deletions

View file

@ -1,30 +0,0 @@
<?php
namespace App\Events;
use App\Models\ProjectDownload;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class DownloadStatusPending implements ShouldBroadcast
{
public ProjectDownload $projectDownload;
/**
* Create a new event instance.
*/
/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('channel-name'),
];
}
}

View file

@ -0,0 +1,33 @@
<?php
namespace App\Events;
use App\Models\ProjectDownload;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Queue\SerializesModels;
class ProjectDownloadStatus implements ShouldBroadcastNow
{
use SerializesModels;
public ProjectDownload $projectDownload;
/**
* Create a new event instance.
*/
public function __construct($projectDownload)
{
$this->projectDownload = $projectDownload;
}
/**
* Get the channels the event should broadcast on.
*
*/
public function broadcastOn(): array
{
return [new PrivateChannel('download.'.$this->projectDownload->id)];
}
}

View file

@ -0,0 +1,34 @@
<?php
namespace App\Events;
use App\Models\ProjectMosaic;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Queue\SerializesModels;
class ProjectMosaicStatus implements ShouldBroadcastNow
{
use SerializesModels;
public ProjectMosaic $projectMosaic;
/**
* Create a new event instance.
*/
public function __construct(ProjectMosaic $projectMosaic)
{
//
$this->projectMosaic = $projectMosaic;
}
/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [new PrivateChannel('mosaic.'.$this->projectMosaic->id)];
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace App\Events;
use App\Models\ProjectReport;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
class ProjectReportStatus implements ShouldBroadcast
{
use SerializesModels;
public ProjectReport $projectReport;
/**
* Create a new event instance.
*/
public function __construct(ProjectReport $projectReport)
{
//
$this->projectReport = $projectReport;
}
/**
* Get the channels the event should broadcast on.
*
* @return array<int, Channel>
*/
public function broadcastOn(): array
{
return [new PrivateChannel('report.'.$this->projectReport->id)];
}
}

View file

@ -5,10 +5,16 @@
use App\Livewire\Forms\MailingForm;
use App\Models\ProjectReport;
use Illuminate\Support\Facades\Storage;
use Livewire\Attributes\Reactive;
use Livewire\Component;
class ReportRow extends Component
{
protected $listeners = [
// 'Badge:refresh' => '$refresh',
];
#[Reactive]
public ProjectReport $report;
public MailingForm $mailingForm;

View file

@ -21,6 +21,10 @@ class Download extends Component
public $search = '';
public $listeners = [
'Badge:refresh' => '$refresh',
];
public function mount(Project $project)

View file

@ -20,6 +20,9 @@ class Mosaic extends Component
public $showCreateModal = false;
public $search = "";
protected $listeners = [
'Badge:refresh' => '$refresh',
];
public function mount(Project $project)
{

View file

@ -20,7 +20,9 @@ class Report extends Component
public $showReportModal = false;
public $listeners = ['refresh' => '$refresh'];
public $listeners = [
'Badge:refresh' => '$refresh'
];
public function openCreateReportModal()
{
@ -97,11 +99,13 @@ public function render()
{
$query = Project::find($this->project->id)
->reports()
->with('project')
->orderBy('year', 'desc')
->orderBy('week', 'desc');
$query = $this->applySearch($query);
$reports = $query->paginate(10, pageName: 'reportPage');
return view('livewire.projects.tabs.report')
->with(compact('reports'));
}

View file

@ -2,6 +2,7 @@
namespace App\Models;
use App\Events\ProjectDownloadStatus;
use App\Traits\HasStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@ -15,10 +16,19 @@ class ProjectDownload extends Model
protected $fillable = [
'name',
'path',
'status'
];
public function project(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Project::class);
}
protected static function booted(): void
{
parent::booted();
static::updated(function (ProjectDownload $projectDownload) {
event(new ProjectDownloadStatus($projectDownload));
});
}
}

View file

@ -2,6 +2,7 @@
namespace App\Models;
use App\Events\ProjectMosaicStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@ -15,6 +16,7 @@ class ProjectMosaic extends Model
'path',
'week',
'year',
'status'
];
public function project()
@ -22,4 +24,12 @@ public function project()
return $this->belongsTo(Project::class);
}
protected static function booted(): void
{
parent::booted();
static::updated(function (ProjectMosaic $projectMosaic) {
event(new ProjectMosaicStatus($projectMosaic));
});
}
}

View file

@ -2,6 +2,7 @@
namespace App\Models;
use App\Events\ProjectReportStatus;
use App\Jobs\ProjectDownloadTiffJob;
use App\Jobs\ProjectMosiacGeneratorJob;
use App\Traits\HasStatus;
@ -14,7 +15,7 @@
class ProjectReport extends Model
{
use HasStatus;
protected $fillable = ['name', 'path', 'week', 'year'];
protected $fillable = ['name', 'path', 'week', 'year','status'];
@ -74,5 +75,13 @@ public function deleteMe()
$this->delete();
}
protected static function booted(): void
{
parent::booted();
static::updated(function (ProjectReport $projectReport) {
event(new ProjectReportStatus($projectReport));
});
}
}

View file

@ -13,9 +13,11 @@ class Badge extends Component
*/
public array $colorClasses;
public Status $status;
public int $id;
public function __construct($status = null)
public function __construct($status = null, $id = 0)
{
$this->id ??= $id ;
$this->status = Status::tryFrom($status) ?? Status::Success;
$this->colorClasses = match($this->status) {
Status::Success => ['bg' => 'bg-green-100', 'text' => 'text-green-700'],

View file

@ -4,14 +4,17 @@ import focus from "@alpinejs/focus";
// import Clipboard from "@ryangjchandler/alpine-clipboard";
// import collapse from "@alpinejs/collapse";
import ui from "@alpinejs/ui";
Alpine.plugin(ui)
Alpine.plugin(focus)
Alpine.plugin(ui);
Alpine.plugin(focus);
// Alpine.plugin(Clipboard);
// Alpine.plugin(Intersect);
// Alpine.plugin(collapse);
import htabs from './history-tabs';
htabs(Alpine)
window.Alpine = Alpine
htabs(Alpine);
window.Alpine = Alpine;
Alpine.start();
Livewire.start();

View file

@ -2,3 +2,4 @@ import './bootstrap';
import('./alpine');
import flatpckr from 'flatpickr';
window.flatpckr = flatpckr;

View file

@ -1,3 +1,11 @@
<span {{ $attributes }} class="inline-flex items-center rounded-md {{ $colorClasses['bg'] }} px-1.5 py-0.5 text-xs font-medium {{ $colorClasses['text'] }}">
<span {{ $attributes }} class="inline-flex items-center rounded-md {{ $colorClasses['bg'] }} px-1.5 py-0.5 text-xs font-medium {{ $colorClasses['text'] }}"
@if($attributes['type'])
x-init="Echo.private(`{{$attributes['type']}}.@js($id)`).listen('Project{{ucfirst($attributes['type'])}}Status', (e) => {
console.log(e.project{{ucfirst($attributes['type'])}}.status);
$wire.dispatch('Badge:refresh');
});"
x-destroy="Echo.leaveChannel(`{{$attributes['type']}}.@js($id)`);"
@endif
>
{{ $status }}
</span>

View file

@ -5,11 +5,12 @@
<td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6 lg:pl-8">{{ $report->name }}</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
@if($report->status == \App\Enums\Status::Pending)
<x-badge status="pending" wire:poll.1s=""></x-badge>
@else
<x-badge :status="$report->status"></x-badge>
@endif
{{-- @if($report->status == \App\Enums\Status::Pending)--}}
{{-- <x-badge status="pending" wire:poll.1s=""></x-badge>--}}
{{-- @else--}}
{{-- <x-badge :status="$report->status"></x-badge>--}}
{{-- @endif--}}
<x-badge :status="$report->status" :id="$report->id" type="report"></x-badge>
</td>
<td class="py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6 lg:pr-8 flex justify-end">
<x-menu>

View file

@ -50,8 +50,9 @@ class="px-3 py-3.5 text-right pr-4 sm:pr-8 lg:pr-8 text-sm font-semibold text-gr
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
{{ $download->path }}
</td>
<td class="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6 lg:pr-8">
<x-badge :status="$download->status"></x-badge>
<td class="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6 lg:pr-8"
>
<x-badge :status="$download->status" :id="$download->id" type="download"></x-badge>
</td>
</tr>
@endforeach

View file

@ -1,8 +1,4 @@
<div
@if($project->hasPendingMosaic())
wire:poll.1s=""
@endif
>
<div>
<div class="px-4 ">
<div class="sm:flex sm:flex-col sm:items-center">
<div class="flex justify-between w-full my-4">
@ -49,7 +45,7 @@ class="px-3 py-3.5 text-right text-sm font-semibold text-gray-900 sm:pr-8 lg:pr-
{{ $mosaic->year }}-{{ $mosaic->week}}
</td>
<td class="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6 lg:pr-8">
<x-badge :status="$mosaic->status"></x-badge>
<x-badge :status="$mosaic->status" :id="$mosaic->id" type="mosaic"></x-badge>
</td>
</tr>
@endforeach

View file

@ -16,3 +16,14 @@
Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Broadcast::channel('download.{downloadId}', function ($user, $downloadId) {
return true;
});
Broadcast::channel('mosaic.{mosaicId}', function ($user, $mosaicId) {
return true;
});
Broadcast::channel('report.{reportId}', function ($user, $reportId) {
return true;
});