From 0b02d52d127bcd0069042b0b43e0134afedb1e9c Mon Sep 17 00:00:00 2001 From: philippe Date: Fri, 31 Oct 2025 15:02:16 +0100 Subject: [PATCH 1/2] back + tests + doc --- demo/app/Sharp/Dashboard/DemoDashboard.php | 1 + demo/app/Sharp/Posts/PostShow.php | 15 +++++------ docs/guide/page-alerts.md | 30 ++++++++++++++++++++++ src/Data/PageAlertData.php | 1 + src/Utils/PageAlerts/PageAlert.php | 9 +++++++ tests/Http/ShowControllerTest.php | 27 +++++++++++++++++++ 6 files changed, 75 insertions(+), 8 deletions(-) diff --git a/demo/app/Sharp/Dashboard/DemoDashboard.php b/demo/app/Sharp/Dashboard/DemoDashboard.php index fa6fb5b28..9eaaca480 100644 --- a/demo/app/Sharp/Dashboard/DemoDashboard.php +++ b/demo/app/Sharp/Dashboard/DemoDashboard.php @@ -135,6 +135,7 @@ protected function buildPageAlert(PageAlert $pageAlert): void { $pageAlert ->setLevelInfo() + ->onSection('stats-section') ->setMessage( sprintf( 'Graphs below are delimited by period %s - %s (and yes, visits figures are randomly generated)', diff --git a/demo/app/Sharp/Posts/PostShow.php b/demo/app/Sharp/Posts/PostShow.php index af8e10dbe..28eb3afd8 100644 --- a/demo/app/Sharp/Posts/PostShow.php +++ b/demo/app/Sharp/Posts/PostShow.php @@ -118,14 +118,13 @@ protected function buildPageAlert(PageAlert $pageAlert): void { $pageAlert ->setLevelInfo() - ->setMessage(function (array $data) { - return $data['publication']['is_planned'] - ? sprintf( - 'This post is planned for publication, on %s', - $data['publication']['published_at'], - ) - : null; - }); + ->setMessage(fn (array $data) => $data['publication']['is_planned'] + ? sprintf( + 'This post is planned for publication, on %s', + $data['publication']['published_at'], + ) + : null + ); } public function getInstanceCommands(): ?array diff --git a/docs/guide/page-alerts.md b/docs/guide/page-alerts.md index 0ec69c165..c23c6a3a2 100644 --- a/docs/guide/page-alerts.md +++ b/docs/guide/page-alerts.md @@ -91,3 +91,33 @@ class MyEntityList extends SharpEntityList } ``` +## Attach the page alert to a specific section (Show Page and Dashboard only) + +The `onSection()` method allows you to specify the section where the alert should be displayed (instead of the default, the top of the page): + +```php +class MyShow extends SharpShow +{ + // ... + + protected function buildShowLayout(ShowLayout $showLayout): void + { + $showLayout + ->addSection(function (ShowLayoutSection $section) { + $section + ->setKey('content') + ->addColumn(/* ... */); + }) + ->addSection(/* ... */); + } + + protected function buildPageAlert(PageAlert $pageAlert): void + { + $pageAlert + ->setMessage('This page has been edited recently.') + ->onSection('content'); + } +} +``` + + diff --git a/src/Data/PageAlertData.php b/src/Data/PageAlertData.php index 55820c3b3..66222e48b 100644 --- a/src/Data/PageAlertData.php +++ b/src/Data/PageAlertData.php @@ -12,6 +12,7 @@ final class PageAlertData extends Data public function __construct( public PageAlertLevel $level, public string $text, + public ?string $sectionKey = null, public ?string $buttonLabel = null, public ?string $buttonUrl = null, ) {} diff --git a/src/Utils/PageAlerts/PageAlert.php b/src/Utils/PageAlerts/PageAlert.php index c282c3903..f9b9a2a23 100644 --- a/src/Utils/PageAlerts/PageAlert.php +++ b/src/Utils/PageAlerts/PageAlert.php @@ -12,6 +12,7 @@ class PageAlert protected PageAlertLevel $pageAlertLevel = PageAlertLevel::Info; protected ?string $text; protected array $data; + protected ?string $sectionKey = null; protected ?string $buttonLabel = null; protected ?string $buttonUrl = null; @@ -54,6 +55,13 @@ public function setLevelSecondary(): self return $this->setLevel(PageAlertLevel::Secondary); } + public function onSection(string $sectionKey): self + { + $this->sectionKey = $sectionKey; + + return $this; + } + public function setMessage(Closure|string $message): self { $this->text = $message instanceof Closure @@ -77,6 +85,7 @@ final public function toArray(): ?array ? Arr::whereNotNull([ 'level' => $this->pageAlertLevel, 'text' => $this->text, + 'sectionKey' => $this->sectionKey, 'buttonLabel' => $this->buttonLabel, 'buttonUrl' => $this->buttonUrl, ]) diff --git a/tests/Http/ShowControllerTest.php b/tests/Http/ShowControllerTest.php index 2cebddb42..6c4600299 100644 --- a/tests/Http/ShowControllerTest.php +++ b/tests/Http/ShowControllerTest.php @@ -311,6 +311,33 @@ public function buildPageAlert(PageAlert $pageAlert): void ); }); +it('allows to configure a page alert on a specific section', function () { + fakeShowFor('person', new class() extends PersonShow + { + public function buildPageAlert(PageAlert $pageAlert): void + { + $pageAlert + ->setLevelInfo() + ->onSection('my-section') + ->setMessage('My page alert') + ->setButton('My button', 'https://example.com'); + } + }); + + $this->get('/sharp/s-list/person/s-show/person/1') + ->assertOk() + ->assertInertia(fn (Assert $page) => $page + ->where('show.pageAlert', [ + 'level' => PageAlertLevel::Info->value, + 'text' => 'My page alert', + 'sectionKey' => 'my-section', + 'buttonLabel' => 'My button', + 'buttonUrl' => 'https://example.com', + ]) + ->etc() + ); +}); + it('allows to configure a page alert with a closure as content', function () { fakeShowFor('person', new class() extends PersonShow { From 5c106d7aa6824f1e048fbe1e4ef5534d8741971a Mon Sep 17 00:00:00 2001 From: antoine Date: Thu, 6 Nov 2025 15:47:09 +0100 Subject: [PATCH 2/2] section page alert front --- resources/js/Pages/Dashboard/Dashboard.vue | 2 +- resources/js/Pages/Show/Show.vue | 12 ++++++++++-- resources/js/dashboard/Dashboard.ts | 2 +- resources/js/dashboard/components/Dashboard.vue | 8 +++++++- resources/js/types/generated.d.ts | 11 ++++++----- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/resources/js/Pages/Dashboard/Dashboard.vue b/resources/js/Pages/Dashboard/Dashboard.vue index d2b919134..c1f934ca8 100644 --- a/resources/js/Pages/Dashboard/Dashboard.vue +++ b/resources/js/Pages/Dashboard/Dashboard.vue @@ -62,7 +62,7 @@
-
+
-
-