feat: basic log page

This commit is contained in:
Nicholas Ciechanowski 2023-10-10 23:48:05 +11:00
parent faaed8fa1b
commit e3bb7cb69f
12 changed files with 131 additions and 16 deletions

View File

@ -3,6 +3,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @mixin IdeHelperLog
@ -15,4 +16,19 @@ class Log extends Model
'requested_quote_id',
'content',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function quote(): BelongsTo
{
return $this->belongsTo(Quote::class);
}
public function requestedQuote(): BelongsTo
{
return $this->belongsTo(RequestedQuote::class);
}
}

View File

@ -3,6 +3,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
@ -20,13 +21,19 @@ class Quote extends Model
Log::create([
'user_id' => auth()?->user()?->id,
'quote_id' => $this->id,
'content' => 'Quote requested.'
'content' => 'Quote requested.',
]);
}
protected $fillable = [
'quote',
];
public function logs(): hasMany
{
return $this->hasMany(Log::class);
}
public static function boot(): void
{
parent::boot();
@ -35,7 +42,7 @@ class Quote extends Model
Log::create([
'user_id' => auth()?->user()?->id,
'quote_id' => $model->id,
'content' => 'Quote created.'
'content' => 'Quote created.',
]);
});
@ -43,7 +50,7 @@ class Quote extends Model
Log::create([
'user_id' => auth()?->user()?->id,
'quote_id' => $model->id,
'content' => 'Quote deleted.'
'content' => 'Quote deleted.',
]);
});
}

View File

@ -3,6 +3,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
@ -17,12 +18,17 @@ class RequestedQuote extends Model
'user_id',
];
public function logs(): hasMany
{
return $this->hasMany(Log::class);
}
public function approve(): void
{
Log::create([
'user_id' => auth()?->user()?->id,
'requested_quote_id' => $this->id,
'content' => 'Quote approved.'
'content' => 'Quote approved.',
]);
Quote::create([
@ -37,7 +43,7 @@ class RequestedQuote extends Model
Log::create([
'user_id' => auth()?->user()?->id,
'requested_quote_id' => $this->id,
'content' => 'Quote rejected.'
'content' => 'Quote rejected.',
]);
$this->delete();

View File

@ -3,6 +3,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
@ -61,6 +62,16 @@ class User extends Authenticatable
'password' => 'hashed',
];
public function logs(): hasMany
{
return $this->hasMany(Log::class);
}
public function fullName(): string
{
return "{$this->firstname} {$this->lastname}";
}
public static function boot(): void
{
parent::boot();
@ -71,7 +82,7 @@ class User extends Authenticatable
self::created(function ($model) {
Log::create([
'user_id' => $model->id,
'content' => 'User created.'
'content' => 'User created.',
]);
});
}

View File

@ -0,0 +1,9 @@
<x-app-layout>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="p-6 text-nexi-black dark:text-gray-100">
<livewire:pages.admin.logs/>
</div>
</div>
</div>
</x-app-layout>

View File

@ -38,12 +38,7 @@
<x-sidebar-nav :href="route('admin.send')" route="send">Send Quote</x-sidebar-nav>
</li>
<li>
{{-- TODO: fill with SVG icons to make it pretty --}}
{{-- <x-sidebar-nav :href="route('a-2')" route="a-2">Second Page</x-sidebar-nav>--}}
</li>
<li>
{{-- TODO: fill with SVG icons to make it pretty --}}
{{-- <x-sidebar-nav :href="route('a-3')" route="a-3">Third Page</x-sidebar-nav>--}}
<x-sidebar-nav :href="route('admin.logs')" route="admin.log">Logs</x-sidebar-nav>
</li>
</ul>
</li>

View File

@ -0,0 +1,67 @@
<?php
use App\Models\Log;
use Illuminate\Database\Eloquent\Collection;
use Livewire\Attributes\Layout;
use Livewire\Volt\Component;
new #[Layout('layouts.app')] class extends Component
{
public Collection $logs;
public function mount(): void
{
$this->getLogs();
}
public function getLogs(): void
{
// TODO: look into pagination for this.
$this->logs = Log::with(['user', 'quote', 'requestedQuote'])->get()->sortByDesc('created_at');
}
}; ?>
<div class="px-4 sm:px-6 lg:px-8">
<div class="-mx-4 mt-10 ring-1 ring-gray-300 sm:mx-0 sm:rounded-lg bg-nexi-primary dark:bg-zinc-800 transition-colors duration-300">
<table class="min-w-full divide-y divide-gray-300">
<thead>
<tr>
<th scope="col"
class="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-nexi-black dark:text-gray-200 sm:pl-6">
User
</th>
<th scope="col"
class="px-3 py-3.5 text-left text-sm font-semibold text-nexi-black dark:text-gray-200">
Logs
</th>
<th scope="col"
class="px-3 py-3.5 text-left text-sm font-semibold text-nexi-black dark:text-gray-200">
Type
</th>
</tr>
</thead>
<tbody>
@foreach ($logs as $log)
<tr>
<td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm text-nexi-black dark:text-gray-200 sm:pl-0">
{{ $log?->user->fullName() ?? 'N/A' }}
</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-nexi-black dark:text-gray-200">
{{ $log->content }}
</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-nexi-black dark:text-gray-200">
@if (!empty($log->quote_id))
Quote - {{ $log->quote_id }}
@elseif (!empty($log->requested_quote_id))
Requsted Quote - ID: {{ $log->requested_quote_id }}
@else
N/A
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>

View File

@ -20,7 +20,7 @@ new #[Layout('layouts.app')] class extends Component
Log::create([
'user_id' => auth()?->user()?->id,
'content' => "Quote sent. {$validated['quote']}"
'content' => "Quote sent. {$validated['quote']}",
]);
$this->quote = '';

View File

@ -42,7 +42,7 @@ new #[Layout('layouts.guest')] class extends Component
Log::create([
'user_id' => auth()?->user()?->id,
'content' => "Quote sent. {$quote}"
'content' => "Quote sent. {$quote}",
]);
$this->redirect(

View File

@ -61,7 +61,7 @@ new #[Layout('layouts.guest')] class extends Component
Log::create([
'user_id' => auth()?->user()?->id,
'content' => "Quote sent. {$quote}"
'content' => "Quote sent. {$quote}",
]);
$this->redirectRoute('login', navigate: true);

View File

@ -31,7 +31,7 @@ new class extends Component
Log::create([
'user_id' => auth()?->user()?->id,
'content' => "Quote sent. {$quote}"
'content' => "Quote sent. {$quote}",
]);
$this->reset('current_password', 'password', 'password_confirmation');

View File

@ -29,6 +29,10 @@ Route::view('send', 'admin.send')
->middleware(['auth'])
->name('admin.send');
Route::view('/admin/logs', 'admin.logs')
->middleware(['auth'])
->name('admin.logs');
Route::middleware('guest')->group(function () {
Volt::route('login', 'pages.auth.login')
->name('login');