Skip to content

Commit

Permalink
fix: fix tags list filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Apr 28, 2021
1 parent 78d2c65 commit 9e37bfa
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 55 deletions.
58 changes: 17 additions & 41 deletions app/Http/Controllers/ContactsController.php
Expand Up @@ -88,31 +88,22 @@ private function contacts(Request $request, bool $active)
$nbArchived = $contacts->count();
}

$tagsCount = Tag::contactsCount();
$tags = null;
$url = '';
$count = 1;

if ($request->input('tag1')) {
if ($request->input('tags')) {
$tags = $request->input('tags');

// get contacts with selected tags
$tags = collect();
$tags = $tagsCount->filter(function ($t) use ($tags) {
return in_array($t->name, $tags);
});

while ($request->input('tag'.$count)) {
$tag = Tag::where([
'account_id' => auth()->user()->account_id,
'name_slug' => $request->input('tag'.$count),
]);
if ($tag->count() > 0) {
$tag = $tag->get();
$url .= $tags->map(function ($t) {
return 'tags[]='.urlencode($t->name).'&';
})->join('');

if (! $tags->contains($tag[0])) {
$tags = $tags->concat($tag);
}

$url .= 'tag'.$count.'='.$tag[0]->name_slug.'&';
}
$count++;
}
if ($tags->count() === 0) {
return redirect()->route('people.index');
} else {
Expand Down Expand Up @@ -140,7 +131,7 @@ private function contacts(Request $request, bool $active)
->withHasArchived($nbArchived > 0)
->withArchivedContacts($nbArchived)
->withTags($tags)
->withTagsCount(Tag::contactsCount())
->withTagsCount($tagsCount)
->withUrl($url)
->withTagCount($count)
->withTagLess($request->input('no_tag') ?? false);
Expand Down Expand Up @@ -693,8 +684,6 @@ public function list(Request $request)
}

$tags = null;
$url = '';
$count = 1;

$contacts = $user->account->contacts()->real();

Expand All @@ -710,30 +699,17 @@ public function list(Request $request)
$contacts = $contacts->alive();
}

if ($request->input('no_tag')) {
// get tag less contacts
$contacts = $contacts->tags('NONE');
} elseif ($request->input('tag1')) {
// get contacts with selected tags
$tags = collect();

while ($request->input('tag'.$count)) {
$tag = Tag::where([
'account_id' => $accountId,
'name_slug' => $request->input('tag'.$count),
])->get();

if (! ($tags->contains($tag[0]))) {
$tags = $tags->concat($tag);
}

$url = $url.'tag'.$count.'='.$tag[0]->name_slug.'&';
if ($request->input('tags')) {
$tags = Tag::where('account_id', $accountId)
->whereIn('name', $request->input('tags'))
->get();

$count++;
}
if ($tags->count() > 0) {
$contacts = $contacts->tags($tags);
}
} elseif ($request->input('no_tag')) {
// get tag less contacts
$contacts = $contacts->tags('NONE');
}

// get the number of contacts per page
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Contact/Tag.php
Expand Up @@ -47,7 +47,7 @@ public function contacts()
*/
public static function contactsCount()
{
return DB::table('contact_tag')->selectRaw('COUNT(tag_id) AS contact_count, name, name_slug')
return DB::table('contact_tag')->selectRaw('COUNT(tag_id) AS contact_count, name, tag_id AS id')
->join('tags', function ($join) {
$join->on('tags.id', '=', 'contact_tag.tag_id')
->on('tags.account_id', '=', 'contact_tag.account_id');
Expand Down
2 changes: 1 addition & 1 deletion public/js/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/mix-manifest.json
@@ -1,5 +1,5 @@
{
"/js/app.js": "/js/app.js?id=2c20668056d05d81becf",
"/js/app.js": "/js/app.js?id=11141790d706a6dc4921",
"/js/stripe.js": "/js/stripe.js?id=20c59848535dab33ed9e",
"/js/manifest.js": "/js/manifest.js?id=895f51a7a2cbcfaf0d03",
"/css/app-rtl.css": "/css/app-rtl.css?id=e976d220373bd82dfe46",
Expand Down
21 changes: 11 additions & 10 deletions resources/js/components/people/Tags.vue
Expand Up @@ -8,21 +8,26 @@
background-color: #4AAE9B;
color: white;
}
.tag-link,
.tag-link:hover{
text-decoration: none;
}
</style>

<template>
<div class="tc">
<!-- list of existing tags -->
<ul>
<li v-for="tag in contactTags" :key="tag.id" class="di mr2">
<span class="bg-white ph2 pb1 pt0 dib br3 b--light-gray ba mb2">
<span v-show="!editMode" class="pointer" @click="navigateTo(tag)">
{{ tag.name }}
</span>
<span v-show="editMode">
<a v-if="!editMode" :href="`people?tags[]=${encodeURIComponent(tag.name)}`" class="tag-link bg-white ph2 pb1 pt0 dib br3 b--light-gray ba mb2">
{{ tag.name }}
</a>
<span v-else class="bg-white ph2 pb1 pt0 dib br3 b--light-gray ba mb2">
<span>
{{ tag.name }}
</span>
<span v-show="editMode" class="pointer" @click="removeTag(tag)">
<span class="pointer" @click="removeTag(tag)">
×
</span>
</span>
Expand Down Expand Up @@ -209,10 +214,6 @@ export default {
});
},
navigateTo(tag) {
window.location.href = 'people?tag1=' + tag.name_slug;
},
handleClickOutside(evt) {
if (!this.$el.contains(evt.target)) {
this.isOpen = false;
Expand Down
2 changes: 1 addition & 1 deletion resources/views/people/index.blade.php
Expand Up @@ -135,7 +135,7 @@
@foreach ($tagsCount as $tag)
@if ($tag->contact_count > 0)
<li>
<span class="pretty-tag"><a href="{{ route('people.index') }}?{{$url}}tag{{$tagCount}}={{ $tag->name_slug }}">{{ $tag->name }}</a></span>
<span class="pretty-tag"><a href="{{ route('people.index') }}?{{$url}}tags[]={{ urlencode($tag->name) }}">{{ $tag->name }}</a></span>
<span class="number-contacts-per-tag">{{ trans_choice('people.people_list_contacts_per_tags', $tag->contact_count, ['count' => $tag->contact_count]) }}</span>
</li>
@endif
Expand Down
32 changes: 32 additions & 0 deletions tests/Feature/ContactTest.php
Expand Up @@ -220,6 +220,38 @@ public function test_user_can_list_contacts_with_tag()
'id' => $contact->id,
'complete_name' => $contact->first_name.' '.$contact->last_name,
]);
$response->assertJsonCount(1, 'contacts');
}

public function test_user_can_list_contacts_with_tagname()
{
$user = $this->signIn();

factory(Contact::class, 10)->state('named')->create([
'account_id' => $user->account_id,
]);
$contact = Contact::where('account_id', $user->account_id)
->inRandomOrder()
->first();

$tag = factory(Tag::class)->create([
'account_id' => $user->account_id,
'name' => 'c++',
]);
$contact->tags()->sync([
$tag->id => [
'account_id' => $user->account_id,
],
]);

$response = $this->get('/people/list?tags[]='.urlencode($tag->name));

$response->assertSuccessful();
$response->assertJsonFragment([
'id' => $contact->id,
'complete_name' => $contact->first_name.' '.$contact->last_name,
]);
$response->assertJsonCount(1, 'contacts');
}

public function test_user_can_see_contacts()
Expand Down

0 comments on commit 9e37bfa

Please sign in to comment.