diff --git a/README.md b/README.md index d9d92e2..789fd94 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,94 @@ protected function getHeaderActions(): array } ``` +4. Or directly in form schemas for Edit pages (Filament 4): + +```php +use Filament\Forms\Components\ViewField; + +public static function configure(Schema $schema): Schema +{ + return $schema + ->components([ + // Your other form fields... + + ViewField::make('comments_section') + ->view('commentions::filament.forms.comments-section') + ->viewData(fn ($livewire) => [ + 'record' => $livewire->record ?? null + ]) + ->columnSpanFull() + ->hiddenLabel(), + ]); +} +``` + +To make the form comments readonly, pass the `readonly` flag in the viewData: + +```php +ViewField::make('comments_section') + ->view('commentions::filament.forms.comments-section') + ->viewData(fn ($livewire) => [ + 'record' => $livewire->record ?? null, + 'readonly' => true, // Enable readonly mode + ]) + ->columnSpanFull() + ->hiddenLabel(), +``` + +**Note:** For View pages, continue using the infolist approach (option 1) as it works perfectly in that context. + +### Readonly Mode + +You can make comments readonly by chaining the `readonly()` method on the action. In readonly mode: +- Users cannot add new comments +- Users cannot edit existing comments +- Users cannot delete comments +- Users cannot react to comments (reactions are displayed but not interactive) + +```php +// Make comments readonly for all actions +CommentsAction::make() + ->readonly() + ->mentionables(User::all()) + +CommentsTableAction::make() + ->readonly() + ->mentionables(User::all()) + +// You can also conditionally enable readonly mode +CommentsAction::make() + ->readonly(auth()->user()->cannot('create', Comment::class)) + ->mentionables(User::all()) +``` + +This is useful for scenarios like: +- Archived or closed records where no further comments should be allowed +- View-only access for certain user roles +- Historical comment viewing +- Audit trails where comments should be preserved but not modified + +#### Testing Readonly Functionality + +The package includes comprehensive tests for readonly functionality designed for Filament 4. To run the readonly-specific tests: + +```bash +# Run all readonly tests +./vendor/bin/pest tests/Livewire/Readonly* tests/Filament/ReadonlyActionsTest.php tests/Concerns/ReadonlyTraitsTest.php tests/Integration/ReadonlyIntegrationTest.php + +# Run specific test categories +./vendor/bin/pest tests/Livewire/ReadonlyCommentsTest.php # Comments component tests +./vendor/bin/pest tests/Livewire/ReadonlyCommentTest.php # Individual comment tests +./vendor/bin/pest tests/Livewire/ReadonlyReactionsTest.php # Reactions tests +./vendor/bin/pest tests/Filament/ReadonlyActionsTest.php # Filament 4 action tests +./vendor/bin/pest tests/Integration/ReadonlyIntegrationTest.php # Integration tests + +# Run trait tests +./vendor/bin/pest tests/Concerns/ReadonlyTraitsTest.php # IsReadonly trait tests +``` + +**Note**: These tests are specifically designed for Filament 4 and may not be compatible with Filament 3. + ### Subscription Management Commentions includes a subscription system that allows users to subscribe to receive notifications when new comments are added to a commentable resource. diff --git a/phpstan.neon b/phpstan.neon index f929f09..984c779 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -46,12 +46,6 @@ parameters: count: 1 path: src/RenderableComment.php - - - message: '#^Call to an undefined method PHPUnit\\Framework\\TestCase::[a-zA-Z0-9_]+\(\)\.$#' - identifier: method.notFound - count: 3 - path: tests/**/*.php - - message: '#^Undefined variable\: \$this$#' identifier: variable.undefined diff --git a/resources/lang/en/comments.php b/resources/lang/en/comments.php index caba4d3..b6d5486 100644 --- a/resources/lang/en/comments.php +++ b/resources/lang/en/comments.php @@ -3,6 +3,7 @@ return [ 'label' => 'Comments', 'no_comments_yet' => 'No comments yet.', + 'save_record_first' => 'Save the record first to enable comments.', 'user_avatar_alt' => 'User Avatar', diff --git a/resources/views/comment-list.blade.php b/resources/views/comment-list.blade.php index a8c458a..1662e4c 100644 --- a/resources/views/comment-list.blade.php +++ b/resources/views/comment-list.blade.php @@ -19,6 +19,7 @@ class="comm:w-8 comm:h-8 comm:text-gray-400 comm:dark:text-gray-500" :key="$comment->getContentHash()" :comment="$comment" :mentionables="$mentionables" + :readonly="$this->isReadonly()" /> @endforeach diff --git a/resources/views/comment.blade.php b/resources/views/comment.blade.php index d574332..51f96a1 100644 --- a/resources/views/comment.blade.php +++ b/resources/views/comment.blade.php @@ -34,7 +34,7 @@ class="comm:text-xs comm:text-gray-300 comm:ml-1" @endif - @if ($comment->isComment() && Config::resolveAuthenticatedUser()?->canAny(['update', 'delete'], $comment)) + @if (!$this->isReadonly() && $comment->isComment() && Config::resolveAuthenticatedUser()?->canAny(['update', 'delete'], $comment))
@if (Config::resolveAuthenticatedUser()?->can('update', $comment)) isComment()) @endif diff --git a/resources/views/comments-modal.blade.php b/resources/views/comments-modal.blade.php index 83c12d5..bb85d3c 100644 --- a/resources/views/comments-modal.blade.php +++ b/resources/views/comments-modal.blade.php @@ -10,5 +10,6 @@ :per-page-increment="$perPageIncrement ?? null" :sidebar-enabled="$sidebarEnabled ?? true" :show-subscribers="$showSubscribers ?? true" + :readonly="$readonly ?? false" />
diff --git a/resources/views/comments.blade.php b/resources/views/comments.blade.php index a18cc3d..e7f6dd1 100644 --- a/resources/views/comments.blade.php +++ b/resources/views/comments.blade.php @@ -3,7 +3,7 @@
{{-- Main Comments Area --}}
- @if (Config::resolveAuthenticatedUser()?->can('create', Config::getCommentModel())) + @if (!$this->isReadonly() && Config::resolveAuthenticatedUser()?->can('create', Config::getCommentModel()))
{{-- tiptap editor --}}
@@ -40,6 +40,7 @@ :per-page="$perPage ?? 5" :load-more-label="$loadMoreLabel ?? 'Show more'" :per-page-increment="$perPageIncrement ?? null" + :readonly="$this->isReadonly()" />
diff --git a/resources/views/filament/forms/comments-section.blade.php b/resources/views/filament/forms/comments-section.blade.php new file mode 100644 index 0000000..6e5e6cb --- /dev/null +++ b/resources/views/filament/forms/comments-section.blade.php @@ -0,0 +1,13 @@ +
+

{{ __('commentions::comments.label') }}

+ + @if($record ?? null) + @livewire('commentions::comments', [ + 'record' => $record, + 'mentionables' => config('commentions.mentionables.default', \App\Models\User::all()), + 'readonly' => $readonly ?? false + ]) + @else +

{{ __('commentions::comments.save_record_first', ['default' => 'Save the record first to enable comments.']) }}

+ @endif +
diff --git a/resources/views/reactions.blade.php b/resources/views/reactions.blade.php index eeff02e..5690bd0 100644 --- a/resources/views/reactions.blade.php +++ b/resources/views/reactions.blade.php @@ -2,25 +2,39 @@ {{-- Inline buttons for existing reactions --}} @foreach ($this->reactionSummary as $reactionData) - + > + {{ $reactionData['reaction'] }} + {{ $reactionData['count'] }} + + @endif @endforeach {{-- Add Reaction Button --}} -
+ @if (!$this->isReadonly()) +