Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions demo/app/Sharp/Dashboard/DemoDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
Expand Down
15 changes: 7 additions & 8 deletions demo/app/Sharp/Posts/PostShow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions docs/guide/page-alerts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
```


1 change: 1 addition & 0 deletions src/Data/PageAlertData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {}
Expand Down
9 changes: 9 additions & 0 deletions src/Utils/PageAlerts/PageAlert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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,
])
Expand Down
27 changes: 27 additions & 0 deletions tests/Http/ShowControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Loading