Skip to content

Commit 8ba63a9

Browse files
authored
Merge pull request #657 from code16/scoped-page-alerts
Scoped page alerts
2 parents cd491ea + 5c106d7 commit 8ba63a9

File tree

11 files changed

+95
-13
lines changed

11 files changed

+95
-13
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+

resources/js/Pages/Dashboard/Dashboard.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
</template>
6363

6464
<div class="container @container mx-auto">
65-
<div :class="dashboard.pageAlert ? 'pt-4' : 'pt-10'">
65+
<div :class="dashboard.pageAlert && !dashboard.pageAlert.sectionKey ? 'pt-4' : 'pt-10'">
6666
<DashboardComponent
6767
:dashboard-key="dashboardKey"
6868
:dashboard="dashboard"

resources/js/Pages/Show/Show.vue

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@
134134

135135
<WithCommands :commands="commands">
136136
<div class="@container">
137-
<div :class="show.pageAlert ? 'pt-4' : 'pt-6 @3xl:pt-10'">
138-
<template v-if="show.pageAlert">
137+
<div :class="show.pageAlert && !show.pageAlert.sectionKey ? 'pt-4' : 'pt-6 @3xl:pt-10'">
138+
<template v-if="show.pageAlert && !show.pageAlert.sectionKey">
139139
<div class="container">
140140
<PageAlert
141141
class="mb-4"
@@ -153,6 +153,14 @@
153153
v-show="show.sectionShouldBeVisible(section, locale) || i == 0"
154154
v-slot="{ collapsed, onCollapseToggle }"
155155
>
156+
<template v-if="show.pageAlert?.sectionKey && show.pageAlert.sectionKey === section.key">
157+
<div class="container">
158+
<PageAlert
159+
class="mb-4"
160+
:page-alert="show.pageAlert"
161+
/>
162+
</div>
163+
</template>
156164
<template v-if="show.sectionHasField(section, 'entityList')">
157165
<template v-for="column in section.columns">
158166
<template v-for="row in column.fields">

resources/js/dashboard/Dashboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class Dashboard {
99
layout: DashboardData['layout'];
1010
data: DashboardData['data'];
1111
filterValues: DashboardData['filterValues'];
12-
pageAlert:DashboardData['pageAlert'];
12+
pageAlert: DashboardData['pageAlert'];
1313
query: DashboardData['query'];
1414

1515
dashboardKey: string;

resources/js/dashboard/components/Dashboard.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767
<template>
6868
<WithCommands :commands="commands">
69-
<template v-if="dashboard?.pageAlert">
69+
<template v-if="dashboard && dashboard.pageAlert && !dashboard.pageAlert.sectionKey">
7070
<PageAlert
7171
class="mb-8"
7272
:page-alert="dashboard.pageAlert"
@@ -186,6 +186,12 @@
186186
</h2>
187187
</template>
188188
</template>
189+
<template v-if="dashboard.pageAlert?.sectionKey && dashboard.pageAlert.sectionKey === section.key">
190+
<PageAlert
191+
class="mb-4"
192+
:page-alert="dashboard.pageAlert"
193+
/>
194+
</template>
189195
<template v-if="dashboard.sectionVisibleFilters(section)?.length || dashboard.visibleCommands?.[section.key]?.flat().length">
190196
<div class="flex gap-2 mb-4">
191197
<template v-if="dashboard.sectionVisibleFilters(section)?.length">

resources/js/types/generated.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ export type OrderedListWidgetData = {
723723
export type PageAlertData = {
724724
level: PageAlertLevel;
725725
text: string;
726+
sectionKey: string | null;
726727
buttonLabel: string | null;
727728
buttonUrl: string | null;
728729
};

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
])

0 commit comments

Comments
 (0)