Added HasUserNotifications trait to show messages to the user
parent
e42284deca
commit
72f4839130
@ -0,0 +1,19 @@
|
||||
{% import 'macros/base.twig' as m %}
|
||||
|
||||
{{ msg() }}
|
||||
|
||||
{% for message in errors|default([]) %}
|
||||
{{ m.alert(__(message), 'danger') }}
|
||||
{% endfor %}
|
||||
|
||||
{% for message in warnings|default([]) %}
|
||||
{{ m.alert(__(message), 'warning') }}
|
||||
{% endfor %}
|
||||
|
||||
{% for message in information|default([]) %}
|
||||
{{ m.alert(__(message), 'info') }}
|
||||
{% endfor %}
|
||||
|
||||
{% for message in messages|default([]) %}
|
||||
{{ m.alert(__(message), 'success') }}
|
||||
{% endfor %}
|
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Controllers;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
trait HasUserNotifications
|
||||
{
|
||||
/**
|
||||
* @param string|array $value
|
||||
* @param string $type
|
||||
*/
|
||||
protected function addNotification($value, $type = 'messages')
|
||||
{
|
||||
session()->set(
|
||||
$type,
|
||||
array_merge(session()->get($type, []), [$value])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getNotifications(): array
|
||||
{
|
||||
$return = [];
|
||||
foreach (['errors', 'warnings', 'information', 'messages'] as $type) {
|
||||
$return[$type] = Collection::make(Arr::flatten(session()->get($type, [])));
|
||||
session()->remove($type);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Controllers;
|
||||
|
||||
use Engelsystem\Test\Unit\Controllers\Stub\HasUserNotificationsImplementation;
|
||||
use Engelsystem\Test\Unit\TestCase;
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
|
||||
class HasUserNotificationsTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Controllers\HasUserNotifications::getNotifications
|
||||
* @covers \Engelsystem\Controllers\HasUserNotifications::addNotification
|
||||
*/
|
||||
public function testNotifications()
|
||||
{
|
||||
$session = new Session(new MockArraySessionStorage());
|
||||
$this->app->instance('session', $session);
|
||||
|
||||
$notify = new HasUserNotificationsImplementation();
|
||||
$notify->add('Foo', 'errors');
|
||||
$notify->add('Bar', 'warnings');
|
||||
$notify->add(['Baz', 'Lorem'], 'information');
|
||||
$notify->add(['Hm', ['Uff', 'sum']], 'messages');
|
||||
|
||||
$this->assertEquals([
|
||||
'errors' => new Collection(['Foo']),
|
||||
'warnings' => new Collection(['Bar']),
|
||||
'information' => new Collection(['Baz', 'Lorem']),
|
||||
'messages' => new Collection(['Hm', 'Uff', 'sum']),
|
||||
], $notify->get());
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Controllers\Stub;
|
||||
|
||||
use Engelsystem\Controllers\HasUserNotifications;
|
||||
|
||||
class HasUserNotificationsImplementation
|
||||
{
|
||||
use HasUserNotifications;
|
||||
|
||||
/**
|
||||
* @param string|array $value
|
||||
* @param string $type
|
||||
*/
|
||||
public function add($value, $type = 'messages')
|
||||
{
|
||||
$this->addNotification($value, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function get(): array
|
||||
{
|
||||
return $this->getNotifications();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue