|
3 | 3 | namespace App\Livewire\Project\Shared;
|
4 | 4 |
|
5 | 5 | use App\Models\Tag;
|
| 6 | +use Livewire\Attributes\Validate; |
6 | 7 | use Livewire\Component;
|
7 | 8 |
|
| 9 | +// Refactored ✅ |
8 | 10 | class Tags extends Component
|
9 | 11 | {
|
10 | 12 | public $resource = null;
|
11 | 13 |
|
12 |
| - public ?string $new_tag = null; |
| 14 | + #[Validate('required|string|min:2')] |
| 15 | + public string $newTags; |
13 | 16 |
|
14 | 17 | public $tags = [];
|
15 | 18 |
|
16 |
| - protected $listeners = [ |
17 |
| - 'refresh' => '$refresh', |
18 |
| - ]; |
19 |
| - |
20 |
| - protected $rules = [ |
21 |
| - 'resource.tags.*.name' => 'required|string|min:2', |
22 |
| - 'new_tag' => 'required|string|min:2', |
23 |
| - ]; |
24 |
| - |
25 |
| - protected $validationAttributes = [ |
26 |
| - 'new_tag' => 'tag', |
27 |
| - ]; |
| 19 | + public $filteredTags = []; |
28 | 20 |
|
29 | 21 | public function mount()
|
| 22 | + { |
| 23 | + $this->loadTags(); |
| 24 | + } |
| 25 | + |
| 26 | + public function loadTags() |
30 | 27 | {
|
31 | 28 | $this->tags = Tag::ownedByCurrentTeam()->get();
|
| 29 | + $this->filteredTags = $this->tags->filter(function ($tag) { |
| 30 | + return ! $this->resource->tags->contains($tag); |
| 31 | + }); |
32 | 32 | }
|
33 | 33 |
|
34 |
| - public function addTag(string $id, string $name) |
| 34 | + public function submit() |
35 | 35 | {
|
36 | 36 | try {
|
37 |
| - if ($this->resource->tags()->where('id', $id)->exists()) { |
38 |
| - $this->dispatch('error', 'Duplicate tags.', "Tag <span class='dark:text-warning'>$name</span> already added."); |
| 37 | + $this->validate(); |
| 38 | + $tags = str($this->newTags)->trim()->explode(' '); |
| 39 | + foreach ($tags as $tag) { |
| 40 | + if (strlen($tag) < 2) { |
| 41 | + $this->dispatch('error', 'Invalid tag.', "Tag <span class='dark:text-warning'>$tag</span> is invalid. Min length is 2."); |
39 | 42 |
|
40 |
| - return; |
| 43 | + continue; |
| 44 | + } |
| 45 | + if ($this->resource->tags()->where('name', $tag)->exists()) { |
| 46 | + $this->dispatch('error', 'Duplicate tags.', "Tag <span class='dark:text-warning'>$tag</span> already added."); |
| 47 | + |
| 48 | + continue; |
| 49 | + } |
| 50 | + $found = Tag::ownedByCurrentTeam()->where(['name' => $tag])->exists(); |
| 51 | + if (! $found) { |
| 52 | + $found = Tag::create([ |
| 53 | + 'name' => $tag, |
| 54 | + 'team_id' => currentTeam()->id, |
| 55 | + ]); |
| 56 | + } |
| 57 | + $this->resource->tags()->attach($found->id); |
41 | 58 | }
|
42 |
| - $this->resource->tags()->syncWithoutDetaching($id); |
43 | 59 | $this->refresh();
|
44 | 60 | } catch (\Exception $e) {
|
45 | 61 | return handleError($e, $this);
|
46 | 62 | }
|
47 | 63 | }
|
48 | 64 |
|
49 |
| - public function deleteTag(string $id) |
| 65 | + public function addTag(string $id, string $name) |
50 | 66 | {
|
51 | 67 | try {
|
52 |
| - $this->resource->tags()->detach($id); |
| 68 | + if ($this->resource->tags()->where('id', $id)->exists()) { |
| 69 | + $this->dispatch('error', 'Duplicate tags.', "Tag <span class='dark:text-warning'>$name</span> already added."); |
53 | 70 |
|
54 |
| - $found_more_tags = Tag::where(['id' => $id, 'team_id' => currentTeam()->id])->first(); |
55 |
| - if ($found_more_tags->applications()->count() == 0 && $found_more_tags->services()->count() == 0) { |
56 |
| - $found_more_tags->delete(); |
| 71 | + return; |
57 | 72 | }
|
| 73 | + $this->resource->tags()->attach($id); |
58 | 74 | $this->refresh();
|
| 75 | + $this->dispatch('success', 'Tag added.'); |
59 | 76 | } catch (\Exception $e) {
|
60 | 77 | return handleError($e, $this);
|
61 | 78 | }
|
62 | 79 | }
|
63 | 80 |
|
64 |
| - public function refresh() |
65 |
| - { |
66 |
| - $this->resource->load(['tags']); |
67 |
| - $this->tags = Tag::ownedByCurrentTeam()->get(); |
68 |
| - $this->new_tag = null; |
69 |
| - } |
70 |
| - |
71 |
| - public function submit() |
| 81 | + public function deleteTag(string $id) |
72 | 82 | {
|
73 | 83 | try {
|
74 |
| - $this->validate([ |
75 |
| - 'new_tag' => 'required|string|min:2', |
76 |
| - ]); |
77 |
| - $tags = str($this->new_tag)->trim()->explode(' '); |
78 |
| - foreach ($tags as $tag) { |
79 |
| - if ($this->resource->tags()->where('name', $tag)->exists()) { |
80 |
| - $this->dispatch('error', 'Duplicate tags.', "Tag <span class='dark:text-warning'>$tag</span> already added."); |
| 84 | + $this->resource->tags()->detach($id); |
81 | 85 |
|
82 |
| - continue; |
83 |
| - } |
84 |
| - $found = Tag::where(['name' => $tag, 'team_id' => currentTeam()->id])->first(); |
85 |
| - if (! $found) { |
86 |
| - $found = Tag::create([ |
87 |
| - 'name' => $tag, |
88 |
| - 'team_id' => currentTeam()->id, |
89 |
| - ]); |
90 |
| - } |
91 |
| - $this->resource->tags()->syncWithoutDetaching($found->id); |
| 86 | + $found_more_tags = Tag::ownedByCurrentTeam()->find($id); |
| 87 | + if ($found_more_tags->applications()->count() == 0 && $found_more_tags->services()->count() == 0) { |
| 88 | + $found_more_tags->delete(); |
92 | 89 | }
|
93 | 90 | $this->refresh();
|
| 91 | + $this->dispatch('success', 'Tag deleted.'); |
94 | 92 | } catch (\Exception $e) {
|
95 | 93 | return handleError($e, $this);
|
96 | 94 | }
|
97 | 95 | }
|
98 | 96 |
|
99 |
| - public function render() |
| 97 | + public function refresh() |
100 | 98 | {
|
101 |
| - return view('livewire.project.shared.tags'); |
| 99 | + $this->loadTags(); |
| 100 | + $this->reset('newTags'); |
102 | 101 | }
|
103 | 102 | }
|
0 commit comments