diff --git a/app/Events/Deadline/DeadlineCreated.php b/app/Events/Deadline/DeadlineCreated.php new file mode 100644 index 0000000..c0ada19 --- /dev/null +++ b/app/Events/Deadline/DeadlineCreated.php @@ -0,0 +1,40 @@ + + */ + public function broadcastOn(): array + { + return [ + new PrivateChannel('Deadline'), + ]; + } +} diff --git a/app/Events/Vote/VoteCreated.php b/app/Events/Vote/VoteCreated.php new file mode 100644 index 0000000..2be01d8 --- /dev/null +++ b/app/Events/Vote/VoteCreated.php @@ -0,0 +1,42 @@ + + */ + public function broadcastOn(): array + { + return [ + new PrivateChannel('Vote'), + ]; + } +} diff --git a/app/Models/Deadline.php b/app/Models/Deadline.php index 39bc034..54aa4e0 100644 --- a/app/Models/Deadline.php +++ b/app/Models/Deadline.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Events\Deadline\DeadlineCreated; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -16,6 +17,13 @@ class Deadline extends Model 'target_chapter', ]; + protected static function booted() :void + { + static::created(static function ($deadline) { + event(new DeadlineCreated($deadline)); + }); + } + public function bookRecommendation() { return $this->belongsTo(BookRecommendation::class); diff --git a/app/Models/Vote.php b/app/Models/Vote.php index e0db14b..c2cd869 100644 --- a/app/Models/Vote.php +++ b/app/Models/Vote.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Events\Vote\VoteCreated; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -14,6 +15,13 @@ class Vote extends Model 'book_recommendation_id', ]; + protected static function booted() :void + { + static::created(static function ($vote) { + broadcast(new VoteCreated($vote))->toOthers(); + }); + } + public function user() { return $this->belongsTo(User::class); diff --git a/nuxt/components/book-recommendations/BookRecommendationTable.vue b/nuxt/components/book-recommendations/BookRecommendationTable.vue index 1220c2f..912280d 100644 --- a/nuxt/components/book-recommendations/BookRecommendationTable.vue +++ b/nuxt/components/book-recommendations/BookRecommendationTable.vue @@ -83,6 +83,12 @@ onMounted(() => { bookRecommendationStore.createRecommendationWS(e.bookRecommendation) }) authStore.socketId = $echo.socketId() + + $echo.private(`Vote`) + .listen('.VoteCreated', (e) => { + console.log(e) + bookRecommendationStore.createVoteWS(e.vote) + }) }) diff --git a/nuxt/components/deadline/DeadlineTable.vue b/nuxt/components/deadline/DeadlineTable.vue index fca17f5..667f8b2 100644 --- a/nuxt/components/deadline/DeadlineTable.vue +++ b/nuxt/components/deadline/DeadlineTable.vue @@ -4,6 +4,8 @@ import ConfirmUserDeadline from '~/components/modal/ConfirmUserDeadline.vue' const props = defineProps<{ bookRecommendationId: number }>() +const { $echo } = useNuxtApp() +const authStore = useAuthStore() const { refresh: deadlineRefresh, status: deadlineStatus, data: deadlines } = useFetch(() => `book-recommendations/${props.bookRecommendationId}/deadlines`) @@ -40,6 +42,13 @@ const rows = computed(() => { } }) }) + +onMounted(() => { + $echo.private(`Deadline`) + .listen('.DeadlineCreated', (e) => { + deadlineRefresh() + }) +})