Skip to content

Commit 21612cc

Browse files
committed
refactor: tags view
1 parent 39a7332 commit 21612cc

File tree

2 files changed

+66
-69
lines changed

2 files changed

+66
-69
lines changed

app/Livewire/Project/Shared/Tags.php

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,101 +3,100 @@
33
namespace App\Livewire\Project\Shared;
44

55
use App\Models\Tag;
6+
use Livewire\Attributes\Validate;
67
use Livewire\Component;
78

9+
// Refactored ✅
810
class Tags extends Component
911
{
1012
public $resource = null;
1113

12-
public ?string $new_tag = null;
14+
#[Validate('required|string|min:2')]
15+
public string $newTags;
1316

1417
public $tags = [];
1518

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 = [];
2820

2921
public function mount()
22+
{
23+
$this->loadTags();
24+
}
25+
26+
public function loadTags()
3027
{
3128
$this->tags = Tag::ownedByCurrentTeam()->get();
29+
$this->filteredTags = $this->tags->filter(function ($tag) {
30+
return ! $this->resource->tags->contains($tag);
31+
});
3232
}
3333

34-
public function addTag(string $id, string $name)
34+
public function submit()
3535
{
3636
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.");
3942

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);
4158
}
42-
$this->resource->tags()->syncWithoutDetaching($id);
4359
$this->refresh();
4460
} catch (\Exception $e) {
4561
return handleError($e, $this);
4662
}
4763
}
4864

49-
public function deleteTag(string $id)
65+
public function addTag(string $id, string $name)
5066
{
5167
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.");
5370

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;
5772
}
73+
$this->resource->tags()->attach($id);
5874
$this->refresh();
75+
$this->dispatch('success', 'Tag added.');
5976
} catch (\Exception $e) {
6077
return handleError($e, $this);
6178
}
6279
}
6380

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)
7282
{
7383
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);
8185

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();
9289
}
9390
$this->refresh();
91+
$this->dispatch('success', 'Tag deleted.');
9492
} catch (\Exception $e) {
9593
return handleError($e, $this);
9694
}
9795
}
9896

99-
public function render()
97+
public function refresh()
10098
{
101-
return view('livewire.project.shared.tags');
99+
$this->loadTags();
100+
$this->reset('newTags');
102101
}
103102
}

resources/views/livewire/project/shared/tags.blade.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<div>
22
<h2>Tags</h2>
3-
<div class="flex flex-wrap gap-2 pt-4">
4-
@if (data_get($this->resource, 'tags'))
5-
@forelse (data_get($this->resource,'tags') as $tagId => $tag)
6-
<div
7-
class="px-2 py-1 text-center rounded select-none dark:text-white w-fit bg-neutral-200 hover:bg-neutral-300 dark:bg-coolgray-100 dark:hover:bg-coolgray-200">
3+
<form wire:submit='submit' class="flex items-end gap-2">
4+
<div class="w-64">
5+
<x-forms.input label="Create new or assign existing tags"
6+
helper="You add more at once with space separated list: web api something<br><br>If the tag does not exists, it will be created."
7+
wire:model="newTags" placeholder="example: prod app1 user" />
8+
</div>
9+
<x-forms.button type="submit">Add</x-forms.button>
10+
</form>
11+
@if (data_get($this->resource, 'tags') && count(data_get($this->resource, 'tags')) > 0)
12+
<h3 class="pt-4">Assigned Tags</h3>
13+
<div class="flex flex-wrap gap-2 pt-4">
14+
@foreach (data_get($this->resource, 'tags') as $tagId => $tag)
15+
<div class="button">
816
{{ $tag->name }}
917
<svg wire:click="deleteTag('{{ $tag->id }}')" xmlns="http://www.w3.org/2000/svg" fill="none"
1018
viewBox="0 0 24 24"
@@ -13,24 +21,14 @@ class="inline-block w-3 h-3 rounded cursor-pointer stroke-current hover:bg-red-5
1321
</path>
1422
</svg>
1523
</div>
16-
@empty
17-
<div class="py-1">No tags yet</div>
18-
@endforelse
19-
@endif
20-
</div>
21-
<form wire:submit='submit' class="flex items-end gap-2 pt-4">
22-
<div class="w-64">
23-
<x-forms.input label="Create new or assign existing tags"
24-
helper="You add more at once with space separated list: web api something<br><br>If the tag does not exists, it will be created."
25-
wire:model="new_tag" />
24+
@endforeach
2625
</div>
27-
<x-forms.button type="submit">Add</x-forms.button>
28-
</form>
29-
@if (count($tags) > 0)
26+
@endif
27+
@if (count($filteredTags) > 0)
3028
<h3 class="pt-4">Exisiting Tags</h3>
3129
<div>Click to add quickly</div>
3230
<div class="flex flex-wrap gap-2 pt-4">
33-
@foreach ($tags as $tag)
31+
@foreach ($filteredTags as $tag)
3432
<x-forms.button wire:click="addTag('{{ $tag->id }}','{{ $tag->name }}')">
3533
{{ $tag->name }}</x-forms.button>
3634
@endforeach

0 commit comments

Comments
 (0)