Skip to content

Commit

Permalink
Merge pull request #288 from Steinbeck-Lab/fix-attach-citation-search…
Browse files Browse the repository at this point in the history
…by-doi

Fix: attach citation by enabling search by DOI
  • Loading branch information
CS76 authored Nov 22, 2024
2 parents 728fc92 + 0726de4 commit 91461df
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 89 deletions.
88 changes: 1 addition & 87 deletions app/Filament/Dashboard/Resources/CitationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
use App\Filament\Dashboard\Resources\CitationResource\RelationManagers\CollectionRelationManager;
use App\Filament\Dashboard\Resources\CitationResource\RelationManagers\MoleculeRelationManager;
use App\Models\Citation;
use Closure;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\HtmlString;
use Tapp\FilamentAuditing\RelationManagers\AuditsRelationManager;

class CitationResource extends Resource
Expand All @@ -35,87 +29,7 @@ class CitationResource extends Resource
public static function form(Form $form): Form
{
return $form
->schema([
Section::make()
->schema([
TextInput::make('failMessage')
->default('')
->hidden()
->disabled(),
TextInput::make('doi')
->label('DOI')
->live(onBlur: true)
->afterStateUpdated(function ($set, $state) {
if (doiRegxMatch($state)) {
$citationDetails = fetchDOICitation($state);
if ($citationDetails) {
$set('title', $citationDetails['title']);
$set('authors', $citationDetails['authors']);
$set('citation_text', $citationDetails['citation_text']);
$set('failMessage', 'Success');
} else {
$set('failMessage', 'No citation found. Please fill in the details manually');
}
} else {
$set('failMessage', 'Invalid DOI');
}
})
->helperText(function ($get) {

if ($get('failMessage') == 'Fetching') {
return new HtmlString('<svg class="animate-spin -ml-1 mr-3 h-5 w-5 text-dark inline" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg> ');
} elseif ($get('failMessage') != 'Success') {
return new HtmlString('<span style="color:red">'.$get('failMessage').'</span>');
} else {
return null;
}
})
->required()
->unique()
->rules([
fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
if ($get('failMessage') != 'No citation found. Please fill in the details manually') {
$fail($get('failMessage'));
}
},
])
->validationMessages([
'unique' => 'The DOI already exists.',
]),
]),

Section::make()
->schema([
TextInput::make('title')
->disabled(function ($get, string $operation) {
if ($operation = 'edit' || $get('failMessage') == 'No citation found. Please fill in the details manually') {
return false;
} else {
return true;
}
}),
TextInput::make('authors')
->disabled(function ($get, string $operation) {
if ($operation = 'edit' || $get('failMessage') == 'No citation found. Please fill in the details manually') {
return false;
} else {
return true;
}
}),
Textarea::make('citation_text')
->label('Citation text / URL')
->disabled(function ($get, string $operation) {
if ($operation = 'edit' || $get('failMessage') == 'No citation found. Please fill in the details manually') {
return false;
} else {
return true;
}
}),
])->columns(1),
]);
->schema(Citation::getForm());
}

public static function table(Table $table): Table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Dashboard\Resources\MoleculeResource\RelationManagers;

use App\Models\Citation;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
Expand All @@ -28,7 +29,10 @@ public function table(Table $table): Table
//
])
->headerActions([
Tables\Actions\AttachAction::make(),
Tables\Actions\AttachAction::make()
->recordSelectSearchColumns(['title', 'authors', 'doi']),
Tables\Actions\CreateAction::make()
->form(Citation::getForm()),
])
->actions([
Tables\Actions\EditAction::make(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function table(Table $table): Table
->headerActions([
Tables\Actions\AttachAction::make()
->multiple()
->recordSelectSearchColumns(['title', 'authors']),
->recordSelectSearchColumns(['title', 'authors', 'doi']),
])
->actions([
Tables\Actions\DetachAction::make(),
Expand Down
92 changes: 92 additions & 0 deletions app/Models/Citation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

namespace App\Models;

use Closure;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\HtmlString;
use OwenIt\Auditing\Contracts\Auditable;

class Citation extends Model implements Auditable
Expand Down Expand Up @@ -52,4 +58,90 @@ public function transformAudit(array $data): array
{
return changeAudit($data);
}

public static function getForm(): array
{
return [
Section::make()
->schema([
TextInput::make('failMessage')
->default('')
->hidden()
->disabled(),
TextInput::make('doi')
->label('DOI')
->live(onBlur: true)
->afterStateUpdated(function ($set, $state) {
if (doiRegxMatch($state)) {
$set('failMessage', 'Fetching');
$citationDetails = fetchDOICitation($state);
if ($citationDetails) {
$set('title', $citationDetails['title']);
$set('authors', $citationDetails['authors']);
$set('citation_text', $citationDetails['citation_text']);
$set('failMessage', 'Success');
} else {
$set('failMessage', 'No citation found. Please fill in the details manually');
}
} else {
$set('failMessage', 'Invalid DOI');
}
})
->helperText(function ($get) {

if ($get('failMessage') == 'Fetching') {
return new HtmlString('<svg class="animate-spin -ml-1 mr-3 h-5 w-5 text-dark inline" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg> ');
} elseif ($get('failMessage') != 'Success') {
return new HtmlString('<span style="color:red">'.$get('failMessage').'</span>');
} else {
return null;
}
})
->required()
->unique()
->rules([
fn (Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
if ($get('failMessage') != 'Success') {
$fail($get('failMessage'));
}
},
])
->validationMessages([
'unique' => 'The DOI already exists.',
]),
]),

Section::make()
->schema([
TextInput::make('title')
->disabled(function ($get, string $operation) {
if ($operation = 'edit' || $get('failMessage') == 'No citation found. Please fill in the details manually') {
return false;
} else {
return true;
}
}),
TextInput::make('authors')
->disabled(function ($get, string $operation) {
if ($operation = 'edit' || $get('failMessage') == 'No citation found. Please fill in the details manually') {
return false;
} else {
return true;
}
}),
Textarea::make('citation_text')
->label('Citation text / URL')
->disabled(function ($get, string $operation) {
if ($operation = 'edit' || $get('failMessage') == 'No citation found. Please fill in the details manually') {
return false;
} else {
return true;
}
}),
])->columns(1),
];
}
}

0 comments on commit 91461df

Please sign in to comment.