Skip to content

Commit 8eec838

Browse files
committed
Hide save form when create comment permission is not granted
1 parent 5aa52d3 commit 8eec838

File tree

6 files changed

+45
-33
lines changed

6 files changed

+45
-33
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ Update the `comment.policy` option in your `config/commentions.php` file:
148148

149149
```php
150150
'comment' => [
151-
'policy' => App\Policies\CommentPolicy::class,
151+
'model' => \Kirschbaum\Commentions\Comment::class,
152+
'policy' => \App\Policies\CommentPolicy::class,
152153
],
153154
```
154155

config/commentions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
|--------------------------------------------------------------------------
2727
*/
2828
'comment' => [
29+
'model' => \Kirschbaum\Commentions\Comment::class,
2930
'policy' => \Kirschbaum\Commentions\Policies\CommentPolicy::class,
3031
],
3132

resources/views/comment.blade.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ class="text-xs text-gray-300 ml-1"
3232
@endif
3333
</div>
3434

35-
@if ($comment->isComment() && auth()->user()?->can('update', $comment))
35+
@if ($comment->isComment())
3636
<div class="flex gap-x-1">
37-
<x-filament::icon-button
38-
icon="heroicon-s-pencil-square"
39-
wire:click="edit"
40-
size="xs"
41-
color="gray"
42-
/>
37+
@can('update', $comment)
38+
<x-filament::icon-button
39+
icon="heroicon-s-pencil-square"
40+
wire:click="edit"
41+
size="xs"
42+
color="gray"
43+
/>
44+
@endcan
4345

4446
@can('delete', $comment)
4547
<x-filament::icon-button
@@ -48,7 +50,7 @@ class="text-xs text-gray-300 ml-1"
4850
size="xs"
4951
color="gray"
5052
/>
51-
@endif
53+
@endcan
5254
</div>
5355
@endif
5456
</div>

resources/views/comments.blade.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
<div class="space-y-2" x-data="{ wasFocused: false }">
2-
<form wire:submit.prevent="save" x-cloak>
3-
{{-- tiptap editor --}}
4-
<div class="relative tip-tap-container mb-2" x-on:click="wasFocused = true" wire:ignore>
5-
<div
6-
x-data="editor(@js($commentBody), @js($this->mentions), 'comments')"
7-
>
8-
<div x-ref="element"></div>
2+
@can('create', \Kirschbaum\Commentions\Config::getCommentModel())
3+
<form wire:submit.prevent="save" x-cloak>
4+
{{-- tiptap editor --}}
5+
<div class="relative tip-tap-container mb-2" x-on:click="wasFocused = true" wire:ignore>
6+
<div
7+
x-data="editor(@js($commentBody), @js($this->mentions), 'comments')"
8+
>
9+
<div x-ref="element"></div>
10+
</div>
911
</div>
10-
</div>
1112

12-
<template x-if="wasFocused">
13-
<div>
14-
<x-filament::button
15-
wire:click="save"
16-
size="sm"
17-
>Save</x-filament::button>
13+
<template x-if="wasFocused">
14+
<div>
15+
<x-filament::button
16+
wire:click="save"
17+
size="sm"
18+
>Save</x-filament::button>
1819

19-
<x-filament::button
20-
x-on:click="wasFocused = false"
21-
wire:click="clear"
22-
size="sm"
23-
color="gray"
24-
>Cancel</x-filament::button>
25-
</div>
26-
</template>
27-
</form>
20+
<x-filament::button
21+
x-on:click="wasFocused = false"
22+
wire:click="clear"
23+
size="sm"
24+
color="gray"
25+
>Cancel</x-filament::button>
26+
</div>
27+
</template>
28+
</form>
29+
@endcan
2830

2931
<livewire:commentions::comment-list
3032
:record="$record"

src/Actions/SaveComment.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Model;
66
use Kirschbaum\Commentions\Comment;
7+
use Kirschbaum\Commentions\Config;
78
use Kirschbaum\Commentions\Contracts\Commenter;
89
use Kirschbaum\Commentions\Events\UserWasMentionedEvent;
910

@@ -14,7 +15,7 @@ class SaveComment
1415
*/
1516
public function __invoke(Model $commentable, Commenter $author, string $body): Comment
1617
{
17-
if ($author->cannot('create', Comment::class)) {
18+
if ($author->cannot('create', Config::getCommentModel())) {
1819
throw new \Exception('Cannot create comment');
1920
}
2021

src/Config.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public static function resolveAuthenticatedUser(): ?Commenter
2929
return $user;
3030
}
3131

32+
public static function getCommentModel(): string
33+
{
34+
return config('commentions.comment.model');
35+
}
36+
3237
public static function getCommenterModel(): string
3338
{
3439
return config('commentions.commenter.model');

0 commit comments

Comments
 (0)