|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Livewire\ResearchSpace; |
| 4 | + |
| 5 | +use Livewire\Component; |
| 6 | +use App\Models\ResearchSpace; |
| 7 | +use App\Events\ResearchSpaceUpdated; |
| 8 | +use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
| 9 | + |
| 10 | +class CollaboratorBoard extends Component |
| 11 | +{ |
| 12 | + use AuthorizesRequests; |
| 13 | + |
| 14 | + public ResearchSpace $space; |
| 15 | + public $content = ''; |
| 16 | + public $userPermissions = []; |
| 17 | + |
| 18 | + protected $listeners = [ |
| 19 | + 'echo:research-space.{spaceId},ResearchSpaceUpdated' => 'onExternalUpdate', |
| 20 | + ]; |
| 21 | + |
| 22 | + public function mount($spaceId) |
| 23 | + { |
| 24 | + $this->space = ResearchSpace::with('collaborators.user')->findOrFail($spaceId); |
| 25 | + |
| 26 | + $this->authorize('view', $this->space); |
| 27 | + |
| 28 | + // Example initial content stored in settings (could be moved to separate table) |
| 29 | + $this->content = data_get($this->space->settings, 'board.content', ''); |
| 30 | + $this->userPermissions = []; // can be populated from collaborators |
| 31 | + } |
| 32 | + |
| 33 | + public function saveContent(string $updated) |
| 34 | + { |
| 35 | + $this->authorize('update', $this->space); |
| 36 | + |
| 37 | + $this->content = $updated; |
| 38 | + |
| 39 | + $settings = $this->space->settings ?? []; |
| 40 | + $settings['board'] = array_merge($settings['board'] ?? [], ['content' => $this->content, 'updated_at' => now()->toDateTimeString()]); |
| 41 | + $this->space->settings = $settings; |
| 42 | + $this->space->save(); |
| 43 | + |
| 44 | + // Broadcast to other collaborators immediately |
| 45 | + event(new ResearchSpaceUpdated($this->space->id, ['content' => $this->content, 'user_id' => auth()->id()])); |
| 46 | + } |
| 47 | + |
| 48 | + public function onExternalUpdate($payload) |
| 49 | + { |
| 50 | + // When we get an external broadcast, update local content |
| 51 | + $this->content = data_get($payload, 'content', $this->content); |
| 52 | + $this->emitSelf('contentUpdated'); |
| 53 | + } |
| 54 | + |
| 55 | + public function render() |
| 56 | + { |
| 57 | + return view('livewire.research-space.collaborator-board', [ |
| 58 | + 'space' => $this->space, |
| 59 | + ]); |
| 60 | + } |
| 61 | +} |
0 commit comments