Add date range filter to LogsController and TableComponent

This commit is contained in:
Matteo Gheza 2024-01-10 16:07:19 +01:00
parent b38b30ff40
commit cc266c8e39
4 changed files with 26 additions and 3 deletions

View File

@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Models\Log;
use App\Models\User;
use Illuminate\Http\Request;
use Carbon\Carbon;
class LogsController extends Controller
{
@ -23,6 +24,19 @@ class LogsController extends Controller
"logs.id", "logs.action", "logs.editor_id", "logs.changed_id", "logs.created_at", "logs.source_type",
"changed_user.name as changed", "editor_user.name as editor", "editor_user.hidden as editor_hidden"
];
if($request->has('from')) {
try {
$from = Carbon::parse($request->input('from'));
$query->whereDate('logs.created_at', '>=', $from->toDateString());
} catch (\Carbon\Exceptions\InvalidFormatException $e) { }
}
if($request->has('to')) {
try {
$to = Carbon::parse($request->input('to'));
$query->whereDate('logs.created_at', '<=', $to->toDateString());
} catch (\Carbon\Exceptions\InvalidFormatException $e) { }
}
if($request->user()->hasPermission("logs-limited-read")) {
$query = $query->where(function ($query) {

View File

@ -37,13 +37,15 @@ export class TableComponent implements OnInit, OnDestroy {
"editor_id"
];
enableDateRangePickerTypes: string[] = ['services', 'trainings'];
enableDateRangePickerTypes: string[] = ['services', 'trainings', 'logs'];
range: (Date | undefined)[] | undefined = undefined;
lastRange: (Date | undefined)[] | undefined = undefined;
rangePicked = false;
filterStart: Date | undefined;
filterEnd: Date | undefined;
@Input() initialStartFilter: Date | undefined;
_maxPaginationSize: number = 10;
_rowsPerPage: number = 20;
@ -103,7 +105,7 @@ export class TableComponent implements OnInit, OnDestroy {
this.data = data.filter((row: any) => typeof row.hidden !== 'undefined' ? !row.hidden : true);
this.originalData = this.data;
this.totalElements = this.data.length;
if(this.currentPage == 1) this.displayedData = this.data.slice(0, this.rowsPerPage);
this.displayedData = this.data.slice((this.currentPage - 1) * this.rowsPerPage, this.currentPage * this.rowsPerPage);
if(this.sourceType === 'list') {
this.api.availableUsers = this.data.filter((row: any) => row.available).length;
}
@ -172,6 +174,12 @@ export class TableComponent implements OnInit, OnDestroy {
ngOnInit(): void {
console.log(this.sourceType);
if(this.initialStartFilter !== undefined) {
this.filterStart = this.initialStartFilter;
this.filterEnd = new Date();
this.rangePicked = true;
this.range = [this.filterStart, this.filterEnd];
}
this.loadTableData();
this.loadDataInterval = setInterval(() => {
if(typeof (window as any).skipTableReload !== 'undefined' && (window as any).skipTableReload) {

View File

@ -1,2 +1,2 @@
<owner-image></owner-image>
<app-table [sourceType]="'logs'" [refreshInterval]="1200000"></app-table>
<app-table [sourceType]="'logs'" [refreshInterval]="1200000" [initialStartFilter]="initialStartFilter"></app-table>

View File

@ -6,6 +6,7 @@ import { Component, OnInit } from '@angular/core';
styleUrls: ['./logs.component.scss']
})
export class LogsComponent implements OnInit {
initialStartFilter = new Date(new Date().setDate(new Date().getDate() - 30));
constructor() { }