Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #25 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Multiple fixes
  • Loading branch information
DarkGhostHunter committed Nov 16, 2021
2 parents 70daedd + 43b5952 commit 93b9c47
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 145 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.github export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
Expand Down
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ You can install the package via composer:
composer require darkghosthunter/laralerts
```

If you don't have anything to start with in your frontend, you can use [Laravel Jetstream](https://jetstream.laravel.com/), or go the classic way and use [Bootstrap](https://getbootstrap.com), [Bulma.io](https://bulma.io/), [UI kit](https://getuikit.com/), [TailwindCSS](https://tailwindcss.com/) and [INK](http://ink.sapo.pt/).
If you don't have anything to start with in your frontend, you can use [Laravel Jetstream](https://jetstream.laravel.com/), or go the classic way and use [Bootstrap](https://getbootstrap.com), [Bulma.io](https://bulma.io/), [UI kit](https://getuikit.com/), [TailwindCSS](https://tailwindcss.com/) and [INK](http://ink.sapo.pt/), among many others.

## Usage

Laralerts allows you to set a list of Alerts in your application and render them in the frontend.
Laralerts allows you to set a list of Alerts in your application and render them in the frontend in just a few minutes.

The default Alert renderer uses Bootstrap code to transform each alert into [Bootstrap Alerts](https://getbootstrap.com/docs/5.0/components/alerts/). If you're not using Bootstrap, you can [create your own](#creating-a-custom-renderer) for your particular framework.

### Quickstart

To set an Alert in your frontend, you can use the `alert()` helper, or the `Alert` Facade. A good place to use them is before sending a View or Redirect response to the browser, like in your HTTP Controllers.
To set an Alert in your frontend, you can use the `alert()` helper, or the `Alert` Facade. A good place to use them is before sending a response to the browser, like in your HTTP Controllers.

If you're sending a redirect, Laralerts will automatically flash the alert so the next request can show it.

```php
<?php
Expand Down Expand Up @@ -220,7 +222,7 @@ alert()->message('You can disregard this')->dismiss(false);
### Conditional Alerts

You can also push an Alert if a condition evaluates to true or false by using `when()` and `unless()`, respectively.
You can also push an Alert if a condition evaluates to true or false by using `when()` and `unless()`, respectively. Further method calls will be sent to the void.

```php
<?php
Expand All @@ -238,15 +240,17 @@ alert()->unless(Auth::user()->mailbox()->isNotEmpty())

### Persistent Alerts

Since alerts are [flashed into the session](https://laravel.com/docs/8.x/session#flash-data), these last only for next response, or after the next redirect. To make any alert persistent you can use the `persistAs()` method with a key to identify the alert. This ensures subsequent calls using the same persistence key don't duplicate (but overwrites) the same Alert.
Alerts only last for the actual request. On Redirects, these are [flashed into the session](https://laravel.com/docs/8.x/session#flash-data) so these are available on the next request.

To make any alert persistent you can use the `persistAs()` method with a key to identify the alert.

```php
alert()->message('Your disk size is almost full')->types('danger')->persistAs('disk.full');
```

> Only the last alert persisted with the same key will be persisted.
> When setting a persitant alert with the same key, it will be replaced for the latter.
Once you're done, you can delete the persistent Alert using `abandon()` directly from the helper with the name of the persisted Alert. It will return `true` if the persisted Alert is found, or `false` if not. For example, we can abandon the previous alert of the disk of the user is no longer full.
Once you're done, you can delete the persistent Alert using `abandon()` directly from the helper with the name of the persisted Alert. It will return `true` if the persisted Alert is found, or `false` if not. For example, we can abandon the previous alert if the disk is no longer full.

```php
if ($disk->notFull()) {
Expand All @@ -266,14 +270,15 @@ alert()->message('Remember, you can follow your order in your {dashboard}.')
->to('dashboard', '/dashboard/orders')
```

Link can also work over translated messages, as long these have a word in curly braces.
Links can also work over translated messages, as long these have a word in curly braces.

```php
<?php
// You can see your package status in the {tracking}.

alert()->trans('user.dashboard.tracking.order', ['order' => $order->tracking_number])
->types('success')
->route('tracking', 'Orders.Tracking', ['order' => 45])
->route('tracking', 'orders.tracking', ['order' => 45])
```

If you have more than one link, you can chain multiple links to a message.
Expand Down Expand Up @@ -338,7 +343,7 @@ This key is also used when [sending JSON alerts](#sending-json-alerts).

## Renderers

Alerts get rendered by a Renderer, which takes the Alert data and transforms them into an HTML string. This makes swapping a frontend framework easier, and allows greater flexibility when rendering HTML.
Alerts get rendered by a Renderer, which takes the Alert data and transforms it into an HTML string. This makes swapping a frontend framework easier, and allows greater flexibility when rendering HTML.

### Creating a custom renderer

Expand Down
3 changes: 3 additions & 0 deletions src/Bag.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ public function collect(): Collection
*/
public function markPersisted(string $key, int $index): static
{
// Find if there is a key already for the persisted alert and replace it.
$this->abandon($key);

$this->persisted[$key] = $index;

return $this;
Expand Down
21 changes: 12 additions & 9 deletions src/Http/Middleware/StoreAlertsInSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DarkGhostHunter\Laralerts\Alert;
use DarkGhostHunter\Laralerts\Bag;
use Illuminate\Contracts\Session\Session;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

use function app;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function handle(Request $request, Closure $next): mixed

$response = $next($request);

$this->bagAlertsToSession();
$this->bagAlertsToSession($response instanceof RedirectResponse);

return $response;
}
Expand All @@ -70,23 +71,25 @@ protected function sessionAlertsToBag(): void
/**
* Move the alerts back to the session.
*
* @param bool $flashIfRedirect
* @return void
*/
protected function bagAlertsToSession(): void
protected function bagAlertsToSession(bool $flashIfRedirect): void
{
[$persistent, $nonPersistent] = $this->bag->collect()->partition(function (Alert $alert): bool {
return in_array($alert->index, $this->bag->getPersisted(), true);
});
[$persistent, $nonPersistent] = $this->bag->collect()
->partition(function (Alert $alert): bool {
return in_array($alert->index, $this->bag->getPersisted(), true);
});

// Persistent keys will be put persistently into the session.
if ($persistent->isNotEmpty()) {
$this->session->put("$this->key.persistent", $persistent->all());
}

// Those not persistent will be flashed. These will live during the
// current request or the next if the actual one is a redirection.
// Once done these will magically disappear from the alerts bag.
if ($persistent->isNotEmpty()) {
// Non-persistent will be flashed if the response is as redirection.
// This way we allow the next response from the app to have these
// alerts without having to manually flash them from the app.
if ($flashIfRedirect && $nonPersistent->isNotEmpty()) {
$this->session->flash("$this->key.alerts", $nonPersistent->all());
}

Expand Down
2 changes: 0 additions & 2 deletions src/Renderers/BootstrapRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,4 @@ public function render(Collection $alerts): string
->make(static::VIEW)
->with('alerts', $alerts->map([$this, 'compileAlert']))->render();
}


}
9 changes: 9 additions & 0 deletions tests/AlertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ public function test_creates_default_instance(): void
static::assertFalse($alert->isDismissible());
}

public function test_alert_can_receive_empty_message(): void
{
static::assertSame('', alert()->new()->getMessage());
static::assertSame('', alert()->message('')->getMessage());
static::assertSame('', alert()->raw('')->getMessage());
static::assertSame('', alert()->trans('')->getMessage());
static::assertSame('', alert()->transChoice('', 10)->getMessage());
}

public function test_alert_set_escaped_message(): void
{
$alert = alert()->new();
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/Middleware/AppendAlertsToJsonResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Tests\Http\Middleware;

use Illuminate\Foundation\Testing\Concerns\InteractsWithViews;
use Orchestra\Testbench\TestCase;
use Tests\RegistersPackage;
use Tests\TestsView;

class AppendAlertsToJsonResponseTest extends TestCase
{
use RegistersPackage;
use TestsView;
use InteractsWithViews;

public function test_adds_json_using_config_key(): void
{
Expand Down
Loading

0 comments on commit 93b9c47

Please sign in to comment.