Add search functionality and display active/inactive subscribers in Admin Dashboard

This commit is contained in:
Andras Bacsai 2024-04-02 15:00:01 +02:00
parent b9b58b8985
commit 75b9f4fcbf
2 changed files with 56 additions and 10 deletions

View File

@ -8,7 +8,31 @@
class Index extends Component
{
public $users = [];
public $active_subscribers = [];
public $inactive_subscribers = [];
public $search = '';
public function submitSearch() {
if ($this->search !== "") {
$this->inactive_subscribers = User::whereDoesntHave('teams', function ($query) {
$query->whereRelation('subscription', 'stripe_subscription_id', '!=', null);
})->where(function ($query) {
$query->where('name', 'like', "%{$this->search}%")
->orWhere('email', 'like', "%{$this->search}%");
})->get()->filter(function ($user) {
return $user->id !== 0;
});
$this->active_subscribers = User::whereHas('teams', function ($query) {
$query->whereRelation('subscription', 'stripe_subscription_id', '!=', null);
})->where(function ($query) {
$query->where('name', 'like', "%{$this->search}%")
->orWhere('email', 'like', "%{$this->search}%");
})->get()->filter(function ($user) {
return $user->id !== 0;
});
} else {
$this->getSubscribers();
}
}
public function mount()
{
if (!isCloud()) {
@ -17,7 +41,15 @@ public function mount()
if (auth()->user()->id !== 0) {
return redirect()->route('dashboard');
}
$this->users = User::whereHas('teams', function ($query) {
$this->getSubscribers();
}
public function getSubscribers() {
$this->inactive_subscribers = User::whereDoesntHave('teams', function ($query) {
$query->whereRelation('subscription', 'stripe_subscription_id', '!=', null);
})->get()->filter(function ($user) {
return $user->id !== 0;
});
$this->active_subscribers = User::whereHas('teams', function ($query) {
$query->whereRelation('subscription', 'stripe_subscription_id', '!=', null);
})->get()->filter(function ($user) {
return $user->id !== 0;

View File

@ -1,17 +1,31 @@
<div>
<h1>Admin Dashboard</h1>
<h3 class="pt-4">Who am I now?</h3>
{{ auth()->user()->name }}
<h3 class="pt-4">Users</h3>
<div class="pb-4">{{ auth()->user()->name }}</div>
<form wire:submit="submitSearch" class="flex flex-col gap-2 lg:flex-row">
<x-forms.input wire:model="search" placeholder="Search for a user" />
<x-forms.button type="submit">Search</x-forms.button>
</form>
<h3 class="pt-4">Active Subscribers</h3>
<div class="flex flex-wrap gap-2">
<div class="dark:text-white cursor-pointer w-96 box-without-bg bg-coollabs-100" wire:click="switchUser('0')">
Root
</div>
@foreach ($users as $user)
<div class="w-96 box" wire:click="switchUser('{{ $user->id }}')">
@forelse ($active_subscribers as $user)
<div class="flex gap-2 box" wire:click="switchUser('{{ $user->id }}')">
<p>{{ $user->name }}</p>
<p>{{ $user->email }}</p>
</div>
@endforeach
@empty
<p>No active subscribers</p>
@endforelse
</div>
<h3 class="pt-4">Inactive Subscribers</h3>
<div class="flex flex-col flex-wrap gap-2">
@forelse ($inactive_subscribers as $user)
<div class="flex gap-2 box" wire:click="switchUser('{{ $user->id }}')">
<p>{{ $user->name }}</p>
<p>{{ $user->email }}</p>
</div>
@empty
<p>No inactive subscribers</p>
@endforelse
</div>
</div>