Skip to content

Feature/lar 111 create edit discussion #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions app/Actions/Discussion/CreateOrUpdateDiscussionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\Actions\Discussion;

use App\Gamify\Points\DiscussionCreated;
use App\Models\Discussion;
use Illuminate\Support\Facades\DB;

final class CreateOrUpdateDiscussionAction
{
public function handle(array $data, ?int $discussionId = null): Discussion
{
return DB::transaction(function () use ($data, $discussionId) {
/** @var Discussion $discussion */
$discussion = Discussion::query()->updateOrCreate(
['id' => $discussionId],
$data
);

if (! $discussionId) {
givePoint(new DiscussionCreated($discussion));
}

return $discussion;
});
}
}
159 changes: 159 additions & 0 deletions app/Livewire/Components/Slideovers/DiscussionForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

declare(strict_types=1);

namespace App\Livewire\Components\Slideovers;

use App\Actions\Discussion\CreateOrUpdateDiscussionAction;
use App\Exceptions\UnverifiedUserException;
use App\Livewire\Traits\WithAuthenticatedUser;
use App\Models\Discussion;
use Filament\Forms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Laravelcm\LivewireSlideOvers\SlideOverComponent;

/**
* @property Form $form
*/
final class DiscussionForm extends SlideOverComponent implements HasForms
{
use InteractsWithForms;
use WithAuthenticatedUser;

public ?Discussion $discussion = null;

public ?array $data = [];

public function mount(?int $discussionId = null): void
{
// @phpstan-ignore-next-line
$this->discussion = $discussionId
? Discussion::query()->findOrFail($discussionId)
: new Discussion;

$this->form->fill(array_merge(
$this->discussion->toArray(),
['user_id' => $this->discussion->user_id ?? Auth::id()]
));
}

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Hidden::make('user_id'),
Forms\Components\TextInput::make('title')
->label(__('validation.attributes.title'))
->helperText(__('pages/discussion.min_discussion_length'))
->required()
->live(onBlur: true)
->afterStateUpdated(function (string $operation, $state, Forms\Set $set): void {
$set('slug', Str::slug($state));
})
->minLength(10),
Forms\Components\Hidden::make('slug'),
Forms\Components\Select::make('tags')
->multiple()
->relationship(
name: 'tags',
titleAttribute: 'name',
modifyQueryUsing: fn ($query) => $query->whereJsonContains('concerns', 'discussion')
)
->searchable()
->required()
->minItems(1)
->maxItems(3),
Forms\Components\MarkdownEditor::make('body')
->fileAttachmentsDisk('public')
->toolbarButtons([
'blockquote',
'bold',
'bulletList',
'codeBlock',
'link',
])
->label(__('validation.attributes.content'))
->required()
->minLength(20),
Forms\Components\Placeholder::make('')
->content(fn () => new HtmlString(Blade::render(<<<'Blade'
<x-torchlight />
Blade))),
])
->statePath('data')
->model($this->discussion);
}

/**
* @throws UnverifiedUserException
* @throws AuthorizationException
* @throws ValidationException
* @throws \Exception
*/
public function save(): void
{
// @phpstan-ignore-next-line
if (! Auth::user()->hasVerifiedEmail()) {
throw new UnverifiedUserException(
message: __('notifications.exceptions.unverified_user')
);
}

if ($this->discussion?->id) {
$this->authorize('update', $this->discussion);
}

$this->validate();

$validated = $this->form->getState();

$discussion = app(CreateOrUpdateDiscussionAction::class)->handle(
data : $validated,
discussionId: $this->discussion?->id
);

$this->form->model($discussion)->saveRelationships();

Notification::make()
->title(
$this->discussion?->id
? __('notifications.discussion.updated')
: __('notifications.discussion.created'),
)
->success()
->send();

// @phpstan-ignore-next-line
$this->redirect(route('discussions.show', ['discussion' => $discussion ?? $this->discussion]), navigate: true);
}

public static function panelMaxWidth(): string
{
return '2xl';
}

public static function closePanelOnEscape(): bool
{
return false;
}

public static function closePanelOnClickAway(): bool
{
return false;
}

public function render(): View
{
return view('livewire.components.slideovers.discussion-form');
}
}
51 changes: 0 additions & 51 deletions app/Livewire/Discussions/Create.php

This file was deleted.

73 changes: 0 additions & 73 deletions app/Livewire/Discussions/Edit.php

This file was deleted.

34 changes: 0 additions & 34 deletions app/Traits/WithTagsAssociation.php

This file was deleted.

5 changes: 5 additions & 0 deletions lang/en/notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@

'spam_send' => 'Your report has been sent successfully',

'discussion' => [
'created' => 'Discussion was successfully created.',
'updated' => 'Discussion was successfully updated.',
'deleted' => 'Discussion was successfully deleted.',
],
];
1 change: 1 addition & 0 deletions lang/en/pages/discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
'action_text' => 'View Thread',
'thank_you_line' => 'Thank you for your participation!',
],
'min_discussion_length' => '160 characters maximum',

];
5 changes: 5 additions & 0 deletions lang/fr/notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@

'spam_send' => 'Votre signalement a été envoyé avec succès',

'discussion' => [
'created' => 'Votre discussion à été créée.',
'updated' => 'Votre discussion à été modifiée.',
'deleted' => 'La discussion a été supprimée.',
],
];
3 changes: 2 additions & 1 deletion lang/fr/pages/discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'empty' => 'Discussions sans commentaires',
'empty_description' => 'Les discussions / sujets qui n’ont pas encore eu de commentaires. Soyez le premier à apporter votre contribution.',
'total_answer' => 'total réponses',
'new_discussion' => 'Nouveau discussion',
'new_discussion' => 'Lancer',
'filter' => [
'recent' => 'Récent',
'popular' => 'Populaire',
Expand All @@ -37,5 +37,6 @@
'action_text' => 'Voir le fil',
'thank_you_line' => 'Merci pour votre participation !',
],
'min_discussion_length' => 'Maximum de 160 caractères.',

];
Loading
Loading