Skip to content

Commit bbf64a9

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

File tree

6 files changed

+77
-14
lines changed

6 files changed

+77
-14
lines changed

demo/app/Sharp/Authors/Commands/VisitFacebookProfileCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function buildCommandConfig(): void
2020

2121
public function execute(mixed $instanceId, array $data = []): array
2222
{
23-
return $this->link('https://facebook.com');
23+
return $this->link('https://facebook.com', targetBlank: true);
2424
}
2525

2626
public function authorizeFor(mixed $instanceId): bool

docs/guide/commands.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,18 @@ Here is the full list of available methods:
169169

170170
### Command return types
171171

172-
Finally, let's review the return possibilities: after a Command has been executed, the code must return something to tell to the front what to do next. There are height of them:
172+
Finally, let's review the return possibilities: after a Command has been executed, the code must return something to tell to the front what to do next. There are eight of them:
173173

174-
- `return $this->info('some text', reload: true)`: displays the entered text in a modal. The second argument, optional (default is `false`), is a boolean to also mark Sharp to reload the page.
174+
- `return $this->info(string $message, bool $reload = false)`: displays the entered text in a modal. The second argument allows reloading the page first.
175175
- `return $this->reload()`: reload the current page (with context).
176-
- `return $this->refresh(1)`*: refresh only the instance with an id on `1`. We can pass an id array also to refresh more than one instance.
177-
- `return $this->view('view.name', ['some'=>'params'])`: display a view right in Sharp; useful for page previews.
178-
- `return $this->html('...')`: display an HTML content.
179-
- `return $this->link('/path/to/redirect')`: redirect to the given path.
180-
- `return $this->download('path', 'diskName')`: the browser will download the specified file.
181-
- `return $this->streamDownload('path', 'name')`: the browser will stream the specified file.
182-
183-
\* `refresh()` is only useful in an Entity List case (in a Dashboard or a Show Page, it will be treated as a `reload()`). In order to make it work properly, you have to slightly adapt the `getListData()` of your Entity List implementation, making use of `$this->queryParams->specificIds()`:
176+
- `return $this->refresh(mixed $ids)`*: refresh only instance(s) with an id in `$ids`, which can be either a single id or an array.
177+
- `return $this->view(string $bladeView, array $params = [])`: display a view right in Sharp; useful for page previews.
178+
- `return $this->html(string $htmlContent)`: display an HTML content.
179+
- `return $this->link(string $link, bool $targetBlank = false)`: redirect to the given path. The second argument, optional (default is `false`), is a boolean to open the link in a new tab.
180+
- `return $this->download(string $filePath, ?string $fileName = null, ?string $diskName = null)`: the browser will download the specified file.
181+
- `return $this->streamDownload(string $fileContent, string $fileName)`: the browser will stream the specified file.
182+
183+
\* `refresh()` is only useful in an Entity List case (in a Dashboard or a Show Page, it will be treated as a `reload()`). To make it work properly, you have to slightly adapt the `getListData()` of your Entity List implementation, making use of `$this->queryParams->specificIds()`:
184184

185185
```php
186186
class OrderList extends SharpEntityList

src/Data/Commands/CommandResponseData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
// download & streamDownload actions returns the file directly in the response
1010
#[LiteralTypeScriptType(
11-
'{ action: "'.CommandAction::Link->value.'", link: string } | '.
11+
'{ action: "'.CommandAction::Link->value.'", link: string, targetBlank: boolean } | '.
1212
'{ action: "'.CommandAction::Info->value.'", message: string, reload: boolean } | '.
1313
'{ action: "'.CommandAction::Refresh->value.'", items?: Array<{ [key: string]: any }> } | '.
1414
'{ action: "'.CommandAction::Reload->value.'" } | '.

src/EntityList/Commands/Command.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ protected function info(string $message, bool $reload = false): array
4444
];
4545
}
4646

47-
protected function link(string $link): array
47+
protected function link(string $link, bool $targetBlank = false): array
4848
{
4949
return [
5050
'action' => CommandAction::Link->value,
5151
'link' => $link,
52+
'targetBlank' => $targetBlank,
5253
];
5354
}
5455

src/EntityList/Commands/EntityState.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function streamDownload(string $fileContent, string $fileName): array
4646
throw new SharpInvalidConfigException('StreamDownload return type is not supported for a state.');
4747
}
4848

49-
protected function link(string $link): array
49+
protected function link(string $link, bool $targetBlank = false): array
5050
{
5151
throw new SharpInvalidConfigException('Link return type is not supported for a state.');
5252
}

tests/Http/Api/Commands/ApiEntityListEntityCommandControllerTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,68 @@ public function getListData(): array|\Illuminate\Contracts\Support\Arrayable
211211
]);
212212
});
213213

214+
it('allows to call an link entity command', function () {
215+
fakeListFor('person', new class() extends PersonList
216+
{
217+
protected function getEntityCommands(): ?array
218+
{
219+
return [
220+
'cmd' => new class() extends EntityCommand
221+
{
222+
public function label(): ?string
223+
{
224+
return 'entity';
225+
}
226+
227+
public function execute(array $data = []): array
228+
{
229+
return $this->link('https://sharp.code16.fr');
230+
}
231+
},
232+
];
233+
}
234+
});
235+
236+
$this->postJson(route('code16.sharp.api.list.command.entity', ['person', 'cmd']))
237+
->assertOk()
238+
->assertJson([
239+
'action' => 'link',
240+
'link' => 'https://sharp.code16.fr',
241+
'targetBlank' => false,
242+
]);
243+
});
244+
245+
it('allows to call an link + targetBlank entity command', function () {
246+
fakeListFor('person', new class() extends PersonList
247+
{
248+
protected function getEntityCommands(): ?array
249+
{
250+
return [
251+
'cmd' => new class() extends EntityCommand
252+
{
253+
public function label(): ?string
254+
{
255+
return 'entity';
256+
}
257+
258+
public function execute(array $data = []): array
259+
{
260+
return $this->link('https://sharp.code16.fr', targetBlank: true);
261+
}
262+
},
263+
];
264+
}
265+
});
266+
267+
$this->postJson(route('code16.sharp.api.list.command.entity', ['person', 'cmd']))
268+
->assertOk()
269+
->assertJson([
270+
'action' => 'link',
271+
'link' => 'https://sharp.code16.fr',
272+
'targetBlank' => true,
273+
]);
274+
});
275+
214276
it('allows to call a form entity command and it handles 422', function () {
215277
fakeListFor('person', new class() extends PersonList
216278
{

0 commit comments

Comments
 (0)