Update client index when Member deleted

This commit is contained in:
philipp lang 2023-08-14 23:57:15 +02:00
parent 7e4961f3d8
commit f178d3ec86
6 changed files with 80 additions and 13 deletions

View File

@ -0,0 +1,54 @@
<?php
namespace App\Lib\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ClientMessage implements ShouldBroadcastNow
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;
public bool $reload = false;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(public string $message)
{
}
public static function make(string $message): self
{
return new static($message);
}
public function shouldReload(): self
{
$this->reload = true;
return $this;
}
public function dispatch(): void
{
event($this);
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel
*/
public function broadcastOn()
{
return new Channel('jobs');
}
}

View File

@ -2,11 +2,10 @@
namespace App\Member;
use App\Lib\Data\JobMiddleware\SendsMessages;
use App\Setting\NamiSettings;
use Lorisleiva\Actions\Concerns\AsAction;
class DeleteJob
class DeleteAction
{
use AsAction;

View File

@ -4,6 +4,7 @@ namespace App\Member;
use App\Country;
use App\Http\Controllers\Controller;
use App\Lib\Events\ClientMessage;
use App\Maildispatcher\Actions\ResyncAction;
use App\Setting\GeneralSettings;
use App\Setting\NamiSettings;
@ -21,11 +22,12 @@ class MemberController extends Controller
$filter = FilterScope::fromRequest($request->input('filter', ''));
return \Inertia::render('member/VIndex', [
'data' => MemberResource::collection(Member::search($filter->search)->query(fn ($q) => $q->select('*')
->withFilter($filter)
->with('payments.subscription')->with(['memberships' => fn ($query) => $query->active()])->with('courses')->with('subscription')->with('leaderMemberships')->with('ageGroupMemberships')
->withPendingPayment()
->ordered()
'data' => MemberResource::collection(Member::search($filter->search)->query(
fn ($q) => $q->select('*')
->withFilter($filter)
->with('payments.subscription')->with(['memberships' => fn ($query) => $query->active()])->with('courses')->with('subscription')->with('leaderMemberships')->with('ageGroupMemberships')
->withPendingPayment()
->ordered()
)->paginate(15)),
]);
}
@ -84,11 +86,12 @@ class MemberController extends Controller
public function destroy(Member $member): RedirectResponse
{
if ($member->nami_id) {
DeleteJob::dispatch($member->nami_id);
DeleteAction::dispatch($member->nami_id);
}
$member->delete();
ResyncAction::dispatch();
ClientMessage::make('Mitglied ' . $member->fullname . ' gelöscht.')->shouldReload()->dispatch();
return redirect()->back();
}

View File

@ -113,7 +113,7 @@ class MemberRequest extends FormRequest
NamiPutMemberAction::run($member->fresh(), null, null);
}
if (!$this->input('has_nami') && null !== $member->nami_id) {
DeleteJob::dispatch($member->nami_id);
DeleteAction::dispatch($member->nami_id);
}
ResyncAction::dispatch();
}

View File

@ -1,5 +1,7 @@
import {ref, computed} from 'vue';
import {router} from '@inertiajs/vue3';
import Toast, {useToast} from 'vue-toastification';
const toast = useToast();
export function useIndex(props) {
const rawProps = JSON.parse(JSON.stringify(props));
@ -59,6 +61,15 @@ export function useIndex(props) {
};
}
window.Echo.channel('jobs').listen('\\App\\Lib\\Events\\ClientMessage', (e) => {
if (e.message) {
toast.success(e.message);
}
if (e.reload) {
reload(false);
}
});
return {
data: inner.data,
reload,

View File

@ -4,7 +4,7 @@ namespace Tests\Feature\Member;
use App\Course\Models\Course;
use App\Course\Models\CourseMember;
use App\Member\DeleteJob;
use App\Member\DeleteAction;
use App\Member\Member;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\DatabaseTransactions;
@ -26,7 +26,7 @@ class DeleteTest extends TestCase
$response->assertRedirect('/member');
Queue::assertPushed(DeleteJob::class, fn ($job) => 123 === $job->namiId);
Queue::assertPushed(DeleteAction::class, fn ($job) => 123 === $job->namiId);
$this->assertDatabaseMissing('members', [
'id' => $member->id,
]);
@ -42,7 +42,7 @@ class DeleteTest extends TestCase
$response->assertRedirect('/member');
Queue::assertNotPushed(DeleteJob::class);
Queue::assertNotPushed(DeleteAction::class);
$this->assertDatabaseMissing('members', [
'id' => $member->id,
]);
@ -54,7 +54,7 @@ class DeleteTest extends TestCase
$this->withoutExceptionHandling()->login()->loginNami();
$member = Member::factory()->defaults()->inNami(123)->create();
dispatch(new DeleteJob(123));
dispatch(new DeleteAction(123));
app(MemberFake::class)->assertDeleted(123, Carbon::parse('yesterday'));
}