Skip to content

Commit

Permalink
fix: display avatars in search (#2075)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Nov 24, 2018
1 parent beb77d5 commit 4698c9a
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
UNRELEASED CHANGES:

* Add ability to create tasks that are not linked to any contacts
* Fix avatar display in searches

RELEASED VERSIONS:

Expand Down
15 changes: 13 additions & 2 deletions app/Http/Controllers/Contacts/RelationshipsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function create(Request $request, Contact $contact)
$existingContacts = auth()->user()->account->contacts()
->real()
->active()
->select(['id', 'first_name', 'last_name'])
->select(['id', 'first_name', 'last_name', 'middle_name', 'nickname'])
->sortedBy('name')
->take(100)
->get();
Expand All @@ -37,7 +37,7 @@ public function create(Request $request, Contact $contact)
}
$arrayContacts->push([
'id' => $existingContact->id,
'name' => $existingContact->name,
'complete_name' => $existingContact->name,
]);
}

Expand Down Expand Up @@ -74,6 +74,16 @@ public function store(Request $request, Contact $contact)
{
// case of linking to an existing contact
if ($request->get('relationship_type') == 'existing') {
$validator = Validator::make($request->all(), [
'existing_contact_id' => 'required|integer',
'relationship_type_id' => 'required|integer',
]);

if ($validator->fails()) {
return back()
->withInput()
->withErrors($validator);
}
$partner = Contact::where('account_id', $request->user()->account_id)
->findOrFail($request->get('existing_contact_id'));
$contact->setRelationship($partner, $request->get('relationship_type_id'));
Expand All @@ -88,6 +98,7 @@ public function store(Request $request, Contact $contact)
'last_name' => 'max:100',
'gender_id' => 'required|integer',
'birthdayDate' => 'date_format:Y-m-d',
'relationship_type_id' => 'required|integer',
]);

if ($validator->fails()) {
Expand Down
13 changes: 2 additions & 11 deletions app/Http/Controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Models\Relationship\Relationship;
use Barryvdh\Debugbar\Facade as Debugbar;
use Illuminate\Support\Facades\Validator;
use App\Http\Resources\Contact\ContactShort as ContactResource;

class ContactsController extends Controller
{
Expand Down Expand Up @@ -528,17 +529,7 @@ public function search(Request $request)
$results = SearchHelper::searchContacts($needle, 20, 'created_at');

if (count($results) !== 0) {
foreach ($results as $key => $result) {
if ($result->is_partial) {
$real = $result->getRelatedRealContact();

$results[$key]->hash = $real->hashID();
} else {
$results[$key]->hash = $result->hashID();
}
}

return $results;
return ContactResource::collection($results);
} else {
return ['noResults' => trans('people.people_search_no_results')];
}
Expand Down
17 changes: 16 additions & 1 deletion app/Http/Resources/Contact/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function toArray($request)
return [
'id' => $this->id,
'object' => 'contact',
'hash_id' => $this->hashID(),
'hash_id' => $this->getHashId(),
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'nickname' => $this->nickname,
Expand Down Expand Up @@ -103,4 +103,19 @@ public function toArray($request)
'updated_at' => DateHelper::getTimestamp($this->updated_at),
];
}

private function getHashId()
{
$hashid = '';
if ($this->is_partial) {
$realContact = $this->getRelatedRealContact();
if ($realContact) {
$hashid = $realContact->hashID();
}
} else {
$hashid = $this->hashID();
}

return $hashid;
}
}
2 changes: 1 addition & 1 deletion app/Http/Resources/Contact/ContactShort.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function toArray($request)
return [
'id' => $this->id,
'object' => 'contact',
'hash_id' => $this->hashID(),
'hash_id' => $this->is_partial ? $this->getRelatedRealContact()->hashID() : $this->hashID(),
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'nickname' => $this->nickname,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/Contact/ContactWithContactFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function toArray($request)
return [
'id' => $this->id,
'object' => 'contact',
'hash_id' => $this->hashId(),
'hash_id' => $this->is_partial ? $this->getRelatedRealContact()->hashID() : $this->hashID(),
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'nickname' => $this->nickname,
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/js/app.js": "/js/app.js?id=02badd76534e3e1fb95d",
"/js/app.js": "/js/app.js?id=c9329e2fddd091f2cbef",
"/css/app-ltr.css": "/css/app-ltr.css?id=c7e1476a934d695151e8",
"/css/app-rtl.css": "/css/app-rtl.css?id=5de2b0fcc43e6e846494",
"/css/stripe.css": "/css/stripe.css?id=64c68c04c4e475fcc7c6",
Expand Down
10 changes: 3 additions & 7 deletions resources/assets/js/components/people/ContactSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div>
<p class="mb2" :class="{ b: required }" v-if="title">{{ title }}</p>
<input type="hidden" :name="name" :value="selected ? selected.id : ''">
<vSelect placeholder="search" :label="'name'" @search="search" :options="computedOption" v-model="selected"></vSelect>
<vSelect :placeholder="this.placeholder" :label="'complete_name'" @search="search" :options="computedOption" v-model="selected"></vSelect>
</div>
</template>

Expand Down Expand Up @@ -34,7 +34,7 @@
return {
src : '/people/search',
filterable : false,
selected: { name: this.placeholder, id: '' },
selected: null,
newOptions: [],
}
},
Expand All @@ -54,13 +54,9 @@
}).then(function(response) {
let data = [];
response.data.data.forEach(function (contact) {
if (contact.hash === vm.userContactId) {
if (contact.id === vm.userContactId) {
return;
}
let middleName = contact.middle_name || '';
let lastName = contact.last_name || '';
contact.name = contact.first_name + (middleName ? ' ' + middleName : '') + (lastName ? ' ' + lastName : '');
data.push(contact);
});
Expand Down
17 changes: 5 additions & 12 deletions resources/assets/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ function Search(form, input, resultsContainer, showResults) {
data.forEach(function (contact) {
let person = {};
person.id = contact.id;
person.url = `/people/${contact.hash}`;
person.url = `/people/${contact.hash_id}`;

const middleName = contact.middle_name || '';
const lastName = contact.last_name || '';
// Unify first, middle and last name in one string (depending on availability).
person.name = contact.first_name + (middleName ? ' ' + middleName : '') + (lastName ? ' ' + lastName : '');
person.name = contact.complete_name;

// Figure out which avatar to use and create the appropriate HTML.
person.avatar = getAvatar(contact);
Expand All @@ -51,14 +48,10 @@ function Search(form, input, resultsContainer, showResults) {
function getAvatar(contact) {
let avatar;

if (contact.avatar_url !== null) {
avatar = `<img src="${contact.avatar_url}" class="avatar">`;
if (contact.information.avatar.has_avatar) {
avatar = `<img src="${contact.information.avatar.avatar_url}" class="avatar">`;
} else {
let initials = contact.first_name.substring(0, 1);
initials += contact.middle_name ? contact.middle_name.substring(0, 1) : '';
initials += contact.last_name ? contact.last_name.substring(0, 1) : '';
initials = initials.toUpperCase();
avatar = `<div class="avatar avatar-initials" style="background-color: ${contact.default_avatar_color}">${initials}</div>`;
avatar = `<div class="avatar avatar-initials" style="background-color: ${contact.information.avatar.default_avatar_color}">${contact.initials}</div>`;
}

return avatar;
Expand Down
3 changes: 1 addition & 2 deletions resources/views/people/relationship/new.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@
:name="'existing_contact_id'"
:placeholder="'{{ trans('people.relationship_form_associate_dropdown_placeholder') }}'"
:default-options="{{ $existingContacts }}"
:user-contact-id="'{{ $contact->hashID() }}'"
:inputId="'{{ $contact->hashID() }}'">
:user-contact-id="'{{ $contact->id }}'">
</contact-select>
@endif
</div>
Expand Down

0 comments on commit 4698c9a

Please sign in to comment.