Skip to content

Commit

Permalink
feat: created a view for collection and added widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
sriramkanakam87 committed May 2, 2024
1 parent 709e21c commit c4d7282
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
11 changes: 11 additions & 0 deletions app/Filament/Dashboard/Resources/CollectionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Filament\Tables\Table;
use Illuminate\Support\Str;
use Tapp\FilamentAuditing\RelationManagers\AuditsRelationManager;
use App\Filament\Dashboard\Resources\CollectionResource\Widgets\CollectionStats;


class CollectionResource extends Resource
{
Expand Down Expand Up @@ -78,6 +80,7 @@ public static function table(Table $table): Table
//
])
->actions([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
])
->bulkActions([
Expand All @@ -102,6 +105,14 @@ public static function getPages(): array
'index' => Pages\ListCollections::route('/'),
'create' => Pages\CreateCollection::route('/create'),
'edit' => Pages\EditCollection::route('/{record}/edit'),
'view' => Pages\ViewCollection::route('/{record}'),
];
}

public static function getWidgets(): array
{
return [
CollectionStats::class,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Filament\Dashboard\Resources\CollectionResource\Pages;

use App\Filament\Dashboard\Resources\CollectionResource;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;
use App\Filament\Dashboard\Resources\CollectionResource\Widgets\CollectionStats;

class ViewCollection extends ViewRecord
{
protected static string $resource = CollectionResource::class;

protected function getHeaderWidgets(): array
{
return [
CollectionStats::class,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Filament\Dashboard\Resources\CollectionResource\Widgets;

use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Illuminate\Support\Facades\Cache;
use App\Models\Collection;

class CollectionStats extends BaseWidget
{
public ?Collection $record = null;

protected function getStats(): array
{
return [
// Stat::make('Entries', $this->record->entries->count())
// ->description('Total count')
// ->color('primary'),
// Stat::make('Total Entries', Cache::rememberForever('stats.collections', function () {
// return Collection::count();
// })),
Stat::make('Total Entries', Cache::rememberForever('stats.collections'.$this->record->id.'entries.count', function () {
return $this->record->entries->count();
})),
Stat::make('Total Molecules', Cache::rememberForever('stats.collections'.$this->record->id.'molecules.count', function () {
return $this->record->molecules->count();
})),
Stat::make('Total Citations', Cache::rememberForever('stats.collections'.$this->record->id.'citations.count', function () {
return $this->record->citations->count();
})),
Stat::make('Total Organisms', Cache::rememberForever('stats.collections'.$this->record->id.'organisms.count', function () {
// refactor the below with eloquent relations if possible
$molecules = $this->record->molecules;
$count = 0;
foreach($molecules as $molecule) {
$count += $molecule->organisms()->count();
}
return $count;
})),
Stat::make('Total Geo Locations', Cache::rememberForever('stats.collections'.$this->record->id.'geo_locations.count', function () {
// refactor the below with eloquent relations if possible
$molecules = $this->record->molecules;
$count = 0;
foreach($molecules as $molecule) {
$count += $molecule->geoLocations()->count();
}
return $count;
})),
];
}
}
4 changes: 2 additions & 2 deletions app/Models/Molecule.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ public function collections(): BelongsToMany
return $this->belongsToMany(Collection::class)->withPivot('url', 'reference', 'mol_filename', 'structural_comments')->withTimestamps();
}

public function organisms()
public function organisms(): BelongsToMany
{
return $this->belongsToMany(Organism::class)->withPivot('id', 'organism_parts')->withTimestamps();
}

public function geoLocations()
public function geoLocations(): BelongsToMany
{
return $this->belongsToMany(GeoLocation::class)->withPivot('locations')->withTimestamps();
}
Expand Down

0 comments on commit c4d7282

Please sign in to comment.