Skip to content

Commit

Permalink
feat: Update CachedPreview
Browse files Browse the repository at this point in the history
  • Loading branch information
pboivin committed Sep 30, 2023
1 parent e54dede commit fbcfe86
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
4 changes: 1 addition & 3 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Route;
use Pboivin\FilamentPeek\CachedPreview;
Expand All @@ -9,9 +8,8 @@
Route::get('preview', function () {
abort_unless($token = Request::query('token'), 404);

abort_unless($preview = Cache::get("filament-peek-preview-{$token}"), 404);
abort_unless($preview = CachedPreview::get($token), 404);

/** @var CachedPreview $preview*/
return $preview->render();
})->name('filament-peek.preview');
});
14 changes: 14 additions & 0 deletions src/CachedPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace Pboivin\FilamentPeek;

use Illuminate\Support\Facades\Cache;

class CachedPreview
{
public static ?string $cacheStore = null;

public function __construct(
public string $pageClass,
public string $view,
Expand All @@ -23,4 +27,14 @@ public function render(): string
{
return $this->pageClass::renderPreviewModalView($this->view, $this->data);
}

public function put(string $token, int $ttl = 60): bool
{
return Cache::store(static::$cacheStore)->put("filament-peek-preview-{$token}", $this, $ttl);
}

public static function get(string $token): ?CachedPreview
{
return Cache::store(static::$cacheStore)->get("filament-peek-preview-{$token}");
}
}
6 changes: 1 addition & 5 deletions src/Pages/Concerns/HasPreviewModal.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ public function openPreviewModal(): void
if ($useInternalUrl) {
$token = md5("preview-" . Auth::user()->getAuthIdentifier() . (Auth::user()->password ?? ''));

Cache::put(
"filament-peek-preview-{$token}",
CachedPreview::make(static::class, $view, $this->previewModalData),
60
);
CachedPreview::make(static::class, $view, $this->previewModalData)->put($token);

$previewModalUrl = route('filament-peek.preview', ['token' => $token]);
} else {
Expand Down

0 comments on commit fbcfe86

Please sign in to comment.