Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ludoguenet committed Mar 9, 2024
1 parent 7e85236 commit 28e33d2
Show file tree
Hide file tree
Showing 23 changed files with 156 additions and 14 deletions.
9 changes: 9 additions & 0 deletions app/Http/Controllers/Api/Admin/ValidateNudgeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use App\Http\Controllers\Controller;
use App\Models\Nudge;
use App\Models\User;
use App\Notifications\Nudge\NudgeValidated;
use Illuminate\Http\Response;

class ValidateNudgeController extends Controller
Expand All @@ -17,6 +19,13 @@ public function __invoke(
'validated' => ! $nudge->validated,
]);

if ($nudge->refresh()->validated === true) {
/** @var User $author */
$author = $nudge->user;

$author->notify(new NudgeValidated($nudge));
}

return response()->noContent();
}
}
4 changes: 4 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\DatabaseNotificationCollection;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

/**
* @property DatabaseNotificationCollection $unreadNotifications
*/
class User extends Authenticatable implements MustVerifyEmail
{
use HasApiTokens, HasFactory, Notifiable;
Expand Down
44 changes: 44 additions & 0 deletions app/Notifications/Nudge/NudgeValidated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace App\Notifications\Nudge;

use App\Models\Nudge;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;

class NudgeValidated extends Notification
{
use Queueable;

/**
* Create a new notification instance.
*/
public function __construct(private readonly Nudge $nudge)
{
//
}

/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['database'];
}

/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'title' => $this->nudge->title,
];
}
}
33 changes: 33 additions & 0 deletions app/Providers/ViewServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace App\Providers;

use App\Models\User;
use Illuminate\Support\Facades;
use Illuminate\Support\ServiceProvider;

class ViewServiceProvider extends ServiceProvider
{
public function register(): void
{
// ...
}

public function boot(): void
{
Facades\View::composer('dashboard', function ($view) {
/** @var User $user */
$user = auth()->user();
$user->unreadNotifications->markAsRead();
});

Facades\View::composer('layouts.navigation', function ($view) {
/** @var ?User $user */
$user = auth()->user();

$view->with(['notificationCount' => (int) ($user ? $user->refresh()->unreadNotifications->count() : 0)]);
});
}
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\ViewServiceProvider::class,
])->toArray(),

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('notifications');
}
};
9 changes: 6 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
colors="true"
>
<testsuites>
<testsuite name="Console">
<directory>tests/Console</directory>
</testsuite>
<testsuite name="Http">
<directory>tests/Http</directory>
</testsuite>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory>tests/Feature</directory>
</testsuite>
</testsuites>
<source>
<include>
Expand Down
23 changes: 17 additions & 6 deletions resources/views/layouts/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
<!-- Settings Dropdown -->
<div class="hidden sm:flex sm:items-center sm:ms-6">
@auth
<a href="{{ route('dashboard') }}" type="button" class="relative mr-1 flex-shrink-0 rounded-full bg-white text-gray-400 hover:text-gray-500 focus:outline-none focus:ring-2 focus:ring-green-500 focus:ring-offset-2 cursor-pointer">
<span class="absolute -inset-1.5"></span>
<span class="sr-only">View notifications</span>
<svg class="size-5" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true">
<path stroke-linecap="round" stroke-linejoin="round" d="M14.857 17.082a23.848 23.848 0 005.454-1.31A8.967 8.967 0 0118 9.75v-.7V9A6 6 0 006 9v.75a8.967 8.967 0 01-2.312 6.022c1.733.64 3.56 1.085 5.455 1.31m5.714 0a24.255 24.255 0 01-5.714 0m5.714 0a3 3 0 11-5.714 0" />
</svg>

@if ($notificationCount !== 0)
<span class="absolute right-0 top-0 block h-2 w-2 rounded-full bg-green-400 ring-2 ring-white"></span>
@endif
</a>
<x-dropdown align="right" width="48">
<x-slot name="trigger">
<button class="inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-gray-500 bg-white hover:text-gray-700 focus:outline-none transition ease-in-out duration-150">
Expand All @@ -45,9 +56,9 @@
</x-dropdown-link>

@if(auth()->user()->isAdmin())
<x-dropdown-link :href="route('admin.index')">
{{ __('Admin Dashboard') }}
</x-dropdown-link>
<x-dropdown-link :href="route('admin.index')">
{{ __('Admin Dashboard') }}
</x-dropdown-link>
@endif

<!-- Authentication -->
Expand Down Expand Up @@ -96,9 +107,9 @@
{{ __('Dashboard') }}
</x-responsive-nav-link>
@if(auth()->user()->isAdmin())
<x-responsive-nav-link :href="route('admin.index')" :active="request()->routeIs('admin.index')">
{{ __('Admin Dashboard') }}
</x-responsive-nav-link>
<x-responsive-nav-link :href="route('admin.index')" :active="request()->routeIs('admin.index')">
{{ __('Admin Dashboard') }}
</x-responsive-nav-link>
@endif
<x-responsive-nav-link :href="route('nudges.create')" :active="request()->routeIs('nudges.create')">
{{ __('Share your nudge!') }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,3 @@
->get(route('admin.index'))
->assertRedirect();
});

it('can edit a nudge')->todo();
5 changes: 5 additions & 0 deletions tests/Http/Admin/EditNudgeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

it('can edit a nudge')->todo();
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

use function Pest\Laravel\actingAs;

it('can create a nudge')->todo();

it('can validate nudges', function () {
$user = User::factory()->admin()->create();
$nudge = Nudge::factory()->create();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions tests/Http/Nudge/CreateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

it('can create a nudge')->todo();
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

uses(TestCase::class, RefreshDatabase::class)->in('Feature');
uses(TestCase::class, RefreshDatabase::class)->in('Http');

0 comments on commit 28e33d2

Please sign in to comment.