From 85df94915ef6f55015a6b25be3316b615951894c Mon Sep 17 00:00:00 2001 From: fsociety Date: Thu, 24 Oct 2024 19:24:31 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20refactor(proposal):=20streamline?= =?UTF-8?q?=20vote=20handling=20and=20improve=20Nostr=20login/logout=20fun?= =?UTF-8?q?ctions=20in=20project=20proposal=20view.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../[ProjectProposal:slug].blade.php | 110 +++++++++--------- 1 file changed, 58 insertions(+), 52 deletions(-) diff --git a/resources/views/pages/association/project-support/[ProjectProposal:slug].blade.php b/resources/views/pages/association/project-support/[ProjectProposal:slug].blade.php index faf3d26..c88385e 100644 --- a/resources/views/pages/association/project-support/[ProjectProposal:slug].blade.php +++ b/resources/views/pages/association/project-support/[ProjectProposal:slug].blade.php @@ -23,50 +23,66 @@ state([ 'isAllowed' => false, 'currentPubkey' => null, 'currentPleb' => null, - 'reasons' => fn() - => Vote::query() - ->where('project_proposal_id', $this->projectProposal->id) - ->where('value', false) - ->get(), + 'reasons' => fn() => $this->getReasons(), 'ownVoteExists' => false, - 'boardVotes' => fn() - => Vote::query() - ->where('project_proposal_id', $this->projectProposal->id) - ->whereHas('einundzwanzigPleb', fn($q) => $q->whereIn('npub', config('einundzwanzig.config.current_board'))) - ->where('value', true) - ->get(), - 'otherVotes' => fn() - => Vote::query() - ->where('project_proposal_id', $this->projectProposal->id) - ->whereDoesntHave( - 'einundzwanzigPleb', - fn($q) => $q->whereIn('npub', config('einundzwanzig.config.current_board')), - ) - ->where('value', true) - ->get(), + 'boardVotes' => fn() => $this->getBoardVotes(), + 'otherVotes' => fn() => $this->getOtherVotes(), ]); on([ - 'nostrLoggedIn' => function ($pubkey) { - $this->currentPubkey = $pubkey; - $this->currentPleb = \App\Models\EinundzwanzigPleb::query()->where('pubkey', $pubkey)->first(); - if ($this->currentPleb->association_status->value < 2) { - return $this->js('alert("Du bist hierzu nicht berechtigt.")'); - } - $this->isAllowed = true; - $this->ownVoteExists = Vote::query() - ->where('project_proposal_id', $this->projectProposal->id) - ->where('einundzwanzig_pleb_id', $this->currentPleb->id) - ->exists(); - }, - 'nostrLoggedOut' => function () { - $this->isAllowed = false; - $this->currentPubkey = null; - $this->currentPleb = null; - }, + 'nostrLoggedIn' => fn($pubkey) => $this->handleNostrLoggedIn($pubkey), + 'nostrLoggedOut' => fn() => $this->handleNostrLoggedOut(), ]); -$approve = function () { +$approve = fn() => $this->handleApprove(); +$notApprove = fn() => $this->handleNotApprove(); + +$getReasons = function () { + return Vote::query() + ->where('project_proposal_id', $this->projectProposal->id) + ->where('value', false) + ->get(); +}; + +$getBoardVotes = function () { + return Vote::query() + ->where('project_proposal_id', $this->projectProposal->id) + ->whereHas('einundzwanzigPleb', fn($q) => $q->whereIn('npub', config('einundzwanzig.config.current_board'))) + ->where('value', true) + ->get(); +}; + +$getOtherVotes = function () { + return Vote::query() + ->where('project_proposal_id', $this->projectProposal->id) + ->whereDoesntHave( + 'einundzwanzigPleb', + fn($q) => $q->whereIn('npub', config('einundzwanzig.config.current_board')) + ) + ->where('value', true) + ->get(); +}; + +$handleNostrLoggedIn = function ($pubkey) { + $this->currentPubkey = $pubkey; + $this->currentPleb = \App\Models\EinundzwanzigPleb::query()->where('pubkey', $pubkey)->first(); + if ($this->currentPleb->association_status->value < 2) { + return $this->js('alert("Du bist hierzu nicht berechtigt.")'); + } + $this->isAllowed = true; + $this->ownVoteExists = Vote::query() + ->where('project_proposal_id', $this->projectProposal->id) + ->where('einundzwanzig_pleb_id', $this->currentPleb->id) + ->exists(); +}; + +$handleNostrLoggedOut = function () { + $this->isAllowed = false; + $this->currentPubkey = null; + $this->currentPleb = null; +}; + +$handleApprove = function () { Vote::query()->updateOrCreate([ 'project_proposal_id' => $this->projectProposal->id, 'einundzwanzig_pleb_id' => $this->currentPleb->id, @@ -75,17 +91,11 @@ $approve = function () { ]); $this->form->reset(); $this->ownVoteExists = true; - $this->boardVotes = Vote::query() - ->where('project_proposal_id', $this->projectProposal->id) - ->where('value', false) - ->get(); - $this->otherVotes = Vote::query() - ->where('project_proposal_id', $this->projectProposal->id) - ->where('value', false) - ->get(); + $this->boardVotes = $this->getBoardVotes(); + $this->otherVotes = $this->getOtherVotes(); }; -$notApprove = function () { +$handleNotApprove = function () { $this->form->validate(); Vote::query()->updateOrCreate([ @@ -97,17 +107,13 @@ $notApprove = function () { ]); $this->form->reset(); $this->ownVoteExists = true; - $this->reasons = Vote::query() - ->where('project_proposal_id', $this->projectProposal->id) - ->where('value', false) - ->get(); + $this->reasons = $this->getReasons(); }; ?> - > @volt