Skip to content

Commit 0b02d52

Browse files
committed
back + tests + doc
1 parent 99f71ef commit 0b02d52

File tree

6 files changed

+75
-8
lines changed

6 files changed

+75
-8
lines changed

demo/app/Sharp/Dashboard/DemoDashboard.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ protected function buildPageAlert(PageAlert $pageAlert): void
135135
{
136136
$pageAlert
137137
->setLevelInfo()
138+
->onSection('stats-section')
138139
->setMessage(
139140
sprintf(
140141
'Graphs below are delimited by period %s - %s (and yes, visits figures are randomly generated)',

demo/app/Sharp/Posts/PostShow.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,13 @@ protected function buildPageAlert(PageAlert $pageAlert): void
118118
{
119119
$pageAlert
120120
->setLevelInfo()
121-
->setMessage(function (array $data) {
122-
return $data['publication']['is_planned']
123-
? sprintf(
124-
'This post is planned for publication, on %s',
125-
$data['publication']['published_at'],
126-
)
127-
: null;
128-
});
121+
->setMessage(fn (array $data) => $data['publication']['is_planned']
122+
? sprintf(
123+
'This post is planned for publication, on %s',
124+
$data['publication']['published_at'],
125+
)
126+
: null
127+
);
129128
}
130129

131130
public function getInstanceCommands(): ?array

docs/guide/page-alerts.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,33 @@ class MyEntityList extends SharpEntityList
9191
}
9292
```
9393

94+
## Attach the page alert to a specific section (Show Page and Dashboard only)
95+
96+
The `onSection()` method allows you to specify the section where the alert should be displayed (instead of the default, the top of the page):
97+
98+
```php
99+
class MyShow extends SharpShow
100+
{
101+
// ...
102+
103+
protected function buildShowLayout(ShowLayout $showLayout): void
104+
{
105+
$showLayout
106+
->addSection(function (ShowLayoutSection $section) {
107+
$section
108+
->setKey('content')
109+
->addColumn(/* ... */);
110+
})
111+
->addSection(/* ... */);
112+
}
113+
114+
protected function buildPageAlert(PageAlert $pageAlert): void
115+
{
116+
$pageAlert
117+
->setMessage('This page has been edited recently.')
118+
->onSection('content');
119+
}
120+
}
121+
```
122+
123+

src/Data/PageAlertData.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ final class PageAlertData extends Data
1212
public function __construct(
1313
public PageAlertLevel $level,
1414
public string $text,
15+
public ?string $sectionKey = null,
1516
public ?string $buttonLabel = null,
1617
public ?string $buttonUrl = null,
1718
) {}

src/Utils/PageAlerts/PageAlert.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class PageAlert
1212
protected PageAlertLevel $pageAlertLevel = PageAlertLevel::Info;
1313
protected ?string $text;
1414
protected array $data;
15+
protected ?string $sectionKey = null;
1516
protected ?string $buttonLabel = null;
1617
protected ?string $buttonUrl = null;
1718

@@ -54,6 +55,13 @@ public function setLevelSecondary(): self
5455
return $this->setLevel(PageAlertLevel::Secondary);
5556
}
5657

58+
public function onSection(string $sectionKey): self
59+
{
60+
$this->sectionKey = $sectionKey;
61+
62+
return $this;
63+
}
64+
5765
public function setMessage(Closure|string $message): self
5866
{
5967
$this->text = $message instanceof Closure
@@ -77,6 +85,7 @@ final public function toArray(): ?array
7785
? Arr::whereNotNull([
7886
'level' => $this->pageAlertLevel,
7987
'text' => $this->text,
88+
'sectionKey' => $this->sectionKey,
8089
'buttonLabel' => $this->buttonLabel,
8190
'buttonUrl' => $this->buttonUrl,
8291
])

tests/Http/ShowControllerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,33 @@ public function buildPageAlert(PageAlert $pageAlert): void
311311
);
312312
});
313313

314+
it('allows to configure a page alert on a specific section', function () {
315+
fakeShowFor('person', new class() extends PersonShow
316+
{
317+
public function buildPageAlert(PageAlert $pageAlert): void
318+
{
319+
$pageAlert
320+
->setLevelInfo()
321+
->onSection('my-section')
322+
->setMessage('My page alert')
323+
->setButton('My button', 'https://example.com');
324+
}
325+
});
326+
327+
$this->get('/sharp/s-list/person/s-show/person/1')
328+
->assertOk()
329+
->assertInertia(fn (Assert $page) => $page
330+
->where('show.pageAlert', [
331+
'level' => PageAlertLevel::Info->value,
332+
'text' => 'My page alert',
333+
'sectionKey' => 'my-section',
334+
'buttonLabel' => 'My button',
335+
'buttonUrl' => 'https://example.com',
336+
])
337+
->etc()
338+
);
339+
});
340+
314341
it('allows to configure a page alert with a closure as content', function () {
315342
fakeShowFor('person', new class() extends PersonShow
316343
{

0 commit comments

Comments
 (0)