Skip to content

Commit

Permalink
feat: [LAR-28] add reply form to create and update reply to thread
Browse files Browse the repository at this point in the history
  • Loading branch information
mckenziearts committed Nov 4, 2024
1 parent e2e14c8 commit dfd59e0
Show file tree
Hide file tree
Showing 40 changed files with 603 additions and 638 deletions.
29 changes: 29 additions & 0 deletions app/Actions/Forum/CreateReplyAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\Actions\Forum;

use App\Contracts\ReplyInterface;
use App\Events\ReplyWasCreated;
use App\Gamify\Points\ReplyCreated;
use App\Models\Reply;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

final class CreateReplyAction
{
public function execute(string $body, ReplyInterface $model): void
{
/** @var User $user */
$user = Auth::user();

$reply = new Reply(['body' => $body]);
$reply->authoredBy($user);
$reply->to($model);
$reply->save();

givePoint(new ReplyCreated($model, $user));
event(new ReplyWasCreated($reply));
}
}
9 changes: 2 additions & 7 deletions app/Gamify/Points/ArticleCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@
namespace App\Gamify\Points;

use App\Models\Article;
use App\Models\User;
use QCod\Gamify\PointType;

final class ArticleCreated extends PointType
{
public int $points = 50;

protected string $payee = 'user';

public function __construct(Article $subject)
{
$this->subject = $subject;
}

public function payee(): User
{
// @phpstan-ignore-next-line
return $this->getSubject()->user;
}
}
5 changes: 3 additions & 2 deletions app/Gamify/Points/BestReply.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

namespace App\Gamify\Points;

use App\Models\Reply;
use QCod\Gamify\PointType;

final class BestReply extends PointType
{
public int $points = 20;

protected string $payee = 'author';
protected string $payee = 'user';

public function __construct(mixed $subject)
public function __construct(Reply $subject)
{
$this->subject = $subject;
}
Expand Down
9 changes: 2 additions & 7 deletions app/Gamify/Points/DiscussionCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@
namespace App\Gamify\Points;

use App\Models\Discussion;
use App\Models\User;
use QCod\Gamify\PointType;

final class DiscussionCreated extends PointType
{
public int $points = 20;

protected string $payee = 'user';

public function __construct(Discussion $subject)
{
$this->subject = $subject;
}

public function payee(): User
{
// @phpstan-ignore-next-line
return $this->getSubject()->user;
}
}
11 changes: 2 additions & 9 deletions app/Gamify/Points/PostCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,16 @@
namespace App\Gamify\Points;

use App\Models\Article;
use App\Models\User;
use QCod\Gamify\PointType;

/**
* @method Article getSubject()
*/
final class PostCreated extends PointType
{
public int $points = 50;

protected string $payee = 'user';

public function __construct(Article $subject)
{
$this->subject = $subject;
}

public function payee(): User
{
return $this->getSubject()->user;
}
}
11 changes: 2 additions & 9 deletions app/Gamify/Points/ThreadCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,16 @@
namespace App\Gamify\Points;

use App\Models\Thread;
use App\Models\User;
use QCod\Gamify\PointType;

/**
* @method Thread getSubject()
*/
final class ThreadCreated extends PointType
{
public int $points = 55;

protected string $payee = 'user';

public function __construct(Thread $subject)
{
$this->subject = $subject;
}

public function payee(): User
{
return $this->getSubject()->user;
}
}
66 changes: 0 additions & 66 deletions app/Livewire/Forum/CreateReply.php

This file was deleted.

94 changes: 56 additions & 38 deletions app/Livewire/Forum/Reply.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,83 @@

namespace App\Livewire\Forum;

use App\Exceptions\CouldNotMarkReplyAsSolution;
use App\Gamify\Points\BestReply;
use App\Models\Reply as ReplyModel;
use App\Models\Thread;
use Filament\Actions\Action;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\On;
use Livewire\Component;

final class Reply extends Component
final class Reply extends Component implements HasActions, HasForms
{
use InteractsWithActions;
use InteractsWithForms;

public ReplyModel $reply;

public Thread $thread;

public function edit(): void
public function editAction(): Action
{
$this->authorize('update', $this->reply);

$this->validate();

// $this->reply->update(['body' => $this->body]);

Notification::make()
->title(__('Réponse modifiée'))
->body(__('Vous avez modifié cette solution avec succès.'))
->success()
->duration(5000)
->send();

$this->dispatch('reply.updated');
return Action::make('edit')
->label(__('actions.edit'))
->color('gray')
->authorize('update', $this->reply)
->action(
fn () => $this->dispatch(
'replyForm',
replyId: $this->reply->id
)->to(ReplyForm::class)
);
}

/**
* @throws CouldNotMarkReplyAsSolution
*/
public function markAsSolution(): void
public function deleteAction(): Action
{
$this->authorize('manage', $this->thread);

if ($this->thread->isSolved()) {
undoPoint(new BestReply($this->thread->solutionReply));
}

$this->thread->markSolution($this->reply, Auth::user()); // @phpstan-ignore-line

givePoint(new BestReply($this->reply));

Notification::make()
->title(__('notifications.thread.best_reply'))
->success()
->duration(5000)
->send();
return Action::make('delete')
->label(__('actions.delete'))
->color('danger')
->authorize('delete', $this->reply)
->requiresConfirmation()
->action(function (): void {
$this->reply->delete();

$this->redirectRoute('forum.show', $this->thread, navigate: true);
});
}

$this->dispatch('thread.save.{$thread->id}');
public function solutionAction(): Action
{
return Action::make('solution')
->label(__('pages/forum.mark_answer'))
->color('success')
->authorize('manage', $this->thread)
->action(function (): void {
if ($this->thread->isSolved()) {
undoPoint(new BestReply($this->thread->solutionReply));
}

$this->thread->markSolution($this->reply, Auth::user()); // @phpstan-ignore-line

givePoint(new BestReply($this->reply));

Notification::make()
->title(__('notifications.thread.best_reply'))
->success()
->duration(5000)
->send();

$this->redirect(route('forum.show', $this->thread).$this->reply->getPathUrl(), navigate: true);
});
}

#[On('reply.updated')]
#[On('reply.save.{reply.id}')]
public function render(): View
{
return view('livewire.forum.reply');
Expand Down
Loading

0 comments on commit dfd59e0

Please sign in to comment.