Skip to content

Commit ff850df

Browse files
committed
fix bug on add reply
2 parents 60bc9af + 1d3678c commit ff850df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1728
-1687
lines changed

app/Actions/Replies/CreateReply.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Actions\Replies;
4+
5+
use App\Events\CommentWasAdded;
6+
use App\Gamify\Points\ReplyCreated;
7+
use App\Models\Reply;
8+
use App\Models\User;
9+
use Illuminate\Database\Eloquent\Model;
10+
use Lorisleiva\Actions\Concerns\AsAction;
11+
12+
final class CreateReply
13+
{
14+
use AsAction;
15+
16+
public function handle(string $body, User $user, Model $model): Reply
17+
{
18+
$reply = new Reply(['body' => $body]);
19+
$reply->authoredBy($user);
20+
$reply->to($model);
21+
$reply->save();
22+
23+
$user->givePoint(new ReplyCreated($model, $user));
24+
25+
// On envoie un event pour une nouvelle réponse à tous les abonnés de la discussion
26+
event(new CommentWasAdded($reply, $model));
27+
28+
return $reply;
29+
}
30+
}

app/Actions/Replies/LikeReply.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Actions\Replies;
4+
5+
use App\Models\Reaction;
6+
use App\Models\Reply;
7+
use App\Models\User;
8+
use Lorisleiva\Actions\Concerns\AsAction;
9+
10+
final class LikeReply
11+
{
12+
use AsAction;
13+
14+
public function handle(User $user, Reply $reply, string $reaction = 'love'): void
15+
{
16+
$react = Reaction::where('name', $reaction)->first();
17+
18+
$user->reactTo($reply, $react);
19+
}
20+
}

app/Console/Commands/CreateAdminUser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ protected function createUser(): void
4444
];
4545

4646
try {
47+
/** @var User $user */
4748
$user = User::query()->create($userData);
4849

4950
$user->assignRole('admin');
50-
} catch (\Exception | QueryException $e) {
51+
} catch (\Exception|QueryException $e) {
5152
$this->error($e->getMessage());
5253
}
5354
}

app/Console/Kernel.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ class Kernel extends ConsoleKernel
1616
\App\Console\Commands\Cleanup\DeleteOldUnverifiedUsers::class,
1717
];
1818

19-
/**
20-
* Define the application's command schedule.
21-
*
22-
* @param \Illuminate\Console\Scheduling\Schedule $schedule
23-
* @return void
24-
*/
25-
protected function schedule(Schedule $schedule)
19+
protected function schedule(Schedule $schedule): void
2620
{
2721
$schedule->command('media-library:delete-old-temporary-uploads')->daily();
2822
$schedule->command('lcm:delete-old-unverified-users')->daily();
@@ -36,15 +30,8 @@ protected function schedule(Schedule $schedule)
3630
}
3731
}
3832

39-
/**
40-
* Register the commands for the application.
41-
*
42-
* @return void
43-
*/
44-
protected function commands()
33+
protected function commands(): void
4534
{
4635
$this->load(__DIR__.'/Commands');
47-
48-
require base_path('routes/console.php');
4936
}
5037
}

app/Filament/Pages/Dashboard.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Filament\Pages;
4+
5+
use App\Filament\Widgets\BlogPostsOverview;
6+
use Filament\Pages\Dashboard as BasePage;
7+
8+
class Dashboard extends BasePage
9+
{
10+
protected function getWidgets(): array
11+
{
12+
return [
13+
BlogPostsOverview::class,
14+
];
15+
}
16+
17+
protected function getColumns(): int|array
18+
{
19+
return 5;
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Filament\Widgets;
4+
5+
use App\Models\Article;
6+
use Filament\Widgets\Widget;
7+
use Illuminate\Support\Facades\Cache;
8+
9+
class BlogPostsOverview extends Widget
10+
{
11+
protected static string $view = 'filament.widgets.blog-posts-overview';
12+
13+
protected int|string|array $columnSpan = 5;
14+
15+
protected function getViewData(): array
16+
{
17+
$latestArticles = Cache::remember('last-posts', now()->addHour(), fn () => Article::latest()->limit(3)->get());
18+
19+
return [
20+
'latestArticles' => $latestArticles,
21+
];
22+
}
23+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace App\Http\Livewire\Discussions;
4+
5+
use App\Actions\Replies\CreateReply;
6+
use App\Models\Discussion;
7+
use Filament\Notifications\Notification;
8+
use Illuminate\Contracts\View\View;
9+
use Livewire\Component;
10+
11+
class AddComment extends Component
12+
{
13+
public $discussion;
14+
15+
public bool $isRoot = true;
16+
17+
public bool $isReply = false;
18+
19+
public string $body = '';
20+
21+
protected $listeners = ['reloadComment' => '$refresh'];
22+
23+
public function mount(Discussion $discussion): void
24+
{
25+
$this->discussion = $discussion;
26+
}
27+
28+
public function saveComment(): void
29+
{
30+
$this->validate(
31+
['body' => 'required'],
32+
['body.required' => __('Votre commentaire ne peut pas être vide')]
33+
);
34+
35+
$comment = CreateReply::run($this->body, auth()->user(), $this->discussion);
36+
37+
$this->emitSelf('reloadComment');
38+
39+
$this->emitUp('reloadComments');
40+
41+
$this->dispatchBrowserEvent('scrollToComment', ['id' => $comment->id]);
42+
43+
Notification::make()
44+
->title(__('Votre commentaire a été ajouté!'))
45+
->success()
46+
->duration(5000)
47+
->send();
48+
49+
$this->reset();
50+
}
51+
52+
public function render(): View
53+
{
54+
return view('livewire.discussions.add-comment');
55+
}
56+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Http\Livewire\Discussions;
4+
5+
use App\Actions\Replies\LikeReply;
6+
use App\Models\Reply;
7+
use Filament\Notifications\Notification;
8+
use Illuminate\Contracts\View\View;
9+
use Livewire\Component;
10+
11+
class Comment extends Component
12+
{
13+
public Reply $comment;
14+
15+
protected $listeners = ['reloadComment' => '$refresh'];
16+
17+
public function delete(): void
18+
{
19+
$this->comment->delete();
20+
21+
Notification::make()
22+
->title(__('Votre commentaire a été supprimé.'))
23+
->success()
24+
->duration(5000)
25+
->send();
26+
27+
$this->emitUp('reloadComment');
28+
}
29+
30+
public function toggleLike(): void
31+
{
32+
LikeReply::run(auth()->user(), $this->comment);
33+
34+
$this->emitSelf('reloadComment');
35+
}
36+
37+
public function render(): View
38+
{
39+
return view('livewire.discussions.comment', [
40+
'count' => $this->comment->getReactionsSummary()->sum('count'),
41+
]);
42+
}
43+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Http\Livewire\Discussions;
4+
5+
use App\Models\Discussion;
6+
use App\Models\Reply;
7+
use Illuminate\Contracts\View\View;
8+
use Illuminate\Support\Collection;
9+
use Livewire\Component;
10+
11+
class Comments extends Component
12+
{
13+
public $discussion;
14+
15+
public $listeners = ['reloadComments' => '$refresh'];
16+
17+
public function mount(Discussion $discussion): void
18+
{
19+
$this->discussion = $discussion;
20+
}
21+
22+
public function getCommentsProperty(): Collection
23+
{
24+
$replies = collect();
25+
26+
foreach ($this->discussion->replies->load(['allChildReplies', 'author']) as $reply) {
27+
/** @var Reply $reply */
28+
if ($reply->allChildReplies->isNotEmpty()) {
29+
foreach ($reply->allChildReplies as $childReply) {
30+
$replies->add($childReply);
31+
}
32+
}
33+
34+
$replies->add($reply);
35+
}
36+
37+
return $replies;
38+
}
39+
40+
public function render(): View
41+
{
42+
return view('livewire.discussions.comments');
43+
}
44+
}

app/Http/Livewire/Discussions/Subscribe.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Models\Discussion;
66
use App\Models\Subscribe as SubscribeModel;
77
use App\Policies\DiscussionPolicy;
8+
use Filament\Notifications\Notification;
89
use Illuminate\Contracts\View\View;
910
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
1011
use Illuminate\Support\Facades\Auth;
@@ -28,7 +29,13 @@ public function subscribe()
2829
$subscribe->user()->associate(Auth::user());
2930
$this->discussion->subscribes()->save($subscribe);
3031

31-
// @ToDo Mettre un nouveau system de notification avec Livewire $this->notification()->success('Abonnement', 'Vous êtes maintenant abonné à cette discussion.');
32+
Notification::make()
33+
->title(__('Abonnement'))
34+
->body(__('Vous êtes maintenant abonné à cette discussion.'))
35+
->success()
36+
->duration(5000)
37+
->send();
38+
3239
$this->emitSelf('refresh');
3340
}
3441

@@ -40,7 +47,13 @@ public function unsubscribe()
4047
->where('user_id', Auth::id())
4148
->delete();
4249

43-
// @ToDo Mettre un nouveau system de notification $this->notification()->success('Désabonnement', 'Vous êtes maintenant désabonné de cette discussion.');
50+
Notification::make()
51+
->title(__('Désabonnement'))
52+
->body(__('Vous êtes maintenant désabonné à cette discussion.'))
53+
->success()
54+
->duration(5000)
55+
->send();
56+
4457
$this->emitSelf('refresh');
4558
}
4659

app/Models/Discussion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function isLocked(): bool
125125

126126
public function getCountAllRepliesWithChildAttribute(): int
127127
{
128-
$count = $this->replies()->count();
128+
$count = $this->replies->count();
129129

130130
foreach ($this->replies()->withCount('allChildReplies')->get() as $reply) {
131131
$count += $reply->all_child_replies_count;

app/Models/Reply.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function solutionTo(): HasOne
6262
return $this->hasOne(Thread::class, 'solution_reply_id');
6363
}
6464

65-
public function wasJustPublished()
65+
public function wasJustPublished(): bool
6666
{
6767
return $this->created_at->gt(Carbon::now()->subMinute());
6868
}
@@ -72,7 +72,7 @@ public function excerpt(int $limit = 100): string
7272
return Str::limit(strip_tags(md_to_html($this->body)), $limit);
7373
}
7474

75-
public function mentionedUsers()
75+
public function mentionedUsers(): array
7676
{
7777
preg_match_all('/@([a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w))/', $this->body, $matches);
7878

app/Providers/AppServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Models\Reply;
99
use App\Models\Thread;
1010
use App\Models\User;
11+
use App\View\Composers\AuthUserComposer;
1112
use App\View\Composers\ChannelsComposer;
1213
use App\View\Composers\InactiveDiscussionsComposer;
1314
use App\View\Composers\ModeratorsComposer;
@@ -28,7 +29,7 @@
2829
use Spatie\Health\Checks\Checks\UsedDiskSpaceCheck;
2930
use Spatie\Health\Facades\Health;
3031

31-
class AppServiceProvider extends ServiceProvider
32+
final class AppServiceProvider extends ServiceProvider
3233
{
3334
public function register(): void
3435
{
@@ -84,6 +85,7 @@ public function bootViewsComposer(): void
8485
View::composer('discussions._contributions', TopContributorsComposer::class);
8586
View::composer('discussions._contributions', InactiveDiscussionsComposer::class);
8687
View::composer('components.profile-users', ProfileUsersComposer::class);
88+
View::composer('*', AuthUserComposer::class);
8789
}
8890

8991
public function bootEloquentMorphs(): void

app/Providers/AuthServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Illuminate\Notifications\DatabaseNotification as Notification;
1717
use Illuminate\Support\Facades\Gate;
1818

19-
class AuthServiceProvider extends ServiceProvider
19+
final class AuthServiceProvider extends ServiceProvider
2020
{
2121
/**
2222
* The policy mappings for the application.

app/Providers/BroadcastServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Support\Facades\Broadcast;
66
use Illuminate\Support\ServiceProvider;
77

8-
class BroadcastServiceProvider extends ServiceProvider
8+
final class BroadcastServiceProvider extends ServiceProvider
99
{
1010
/**
1111
* Bootstrap any application services.

0 commit comments

Comments
 (0)