Skip to content

Commit

Permalink
feat: Access preview data as json
Browse files Browse the repository at this point in the history
  • Loading branch information
pboivin committed Oct 3, 2023
1 parent 2be130e commit e04e1d5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions routes/preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

abort_unless($preview = CachedPreview::get($token), 404);

if (Request::wantsJson()) {
return $preview->data;
}

return $preview->render();
})->name('filament-peek.preview');
});
Expand Down
28 changes: 28 additions & 0 deletions tests/src/Integration/WithPreviewUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
->assertSee('KEY:VALUE');
});

it('can access preview data as json', function () {
actingAs(User::factory()->create());

CachedPreview::make(EditPost::class, 'preview-data', ['KEY' => 'VALUE'])
->put('test');

get('/filament-peek/preview/?token=test', ['Accept' => 'application/json'])
->assertSuccessful()
->assertJson(['KEY' => 'VALUE']);
});

it('can use internal preview url for page preview', function () {
$this->mock(Support\Cache::class)
->shouldReceive('createPreviewToken')
Expand Down Expand Up @@ -58,3 +69,20 @@
iframeContent: null
);
});

it('sets the isPeekPreviewModal flag', function () {
$this->mock(Support\Cache::class)
->shouldReceive('createPreviewToken')
->andReturn('test');

$page = Page::factory()->create(['title' => 'Test Page']);

Livewire::test(EditPage::class, ['record' => $page->id])
->assertSeeHtml('Test Page')
->callAction('preview');

$preview = CachedPreview::get('test');

/** @var TestCase $this */
$this->assertTrue($preview->data['isPeekPreviewModal']);
});

0 comments on commit e04e1d5

Please sign in to comment.