From efc56dffc4227a06e50f5f9410506d272c224e7c Mon Sep 17 00:00:00 2001 From: Mazarin Date: Tue, 17 May 2022 11:50:26 -0400 Subject: [PATCH] feat: change template on contact page (monicahq/chandler#86) --- app/Models/User.php | 17 +++++++++++++ .../Web/ViewHelpers/ContactShowViewHelper.php | 14 +++++++++++ resources/js/Pages/Vault/Contact/Blank.vue | 12 ++++----- resources/js/Pages/Vault/Contact/Show.vue | 9 +++++-- routes/web.php | 2 +- .../ViewHelpers/ContactShowViewHelperTest.php | 15 ++++++++++- tests/Unit/Models/UserTest.php | 25 +++++++++++++++++++ 7 files changed, 83 insertions(+), 11 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 1ff9db19787..32e66eeeb8a 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Facades\DB; use Laravel\Sanctum\HasApiTokens; class User extends Authenticatable implements MustVerifyEmail @@ -127,4 +128,20 @@ public function getNameAttribute($value): ?string return $this->first_name.' '.$this->last_name; } + + /** + * Get the contact of the user in the given vault. + * All users have a contact in the vaults. + * + * @param Vault $vault + * @return Contact + */ + public function getContactInVault(Vault $vault): Contact + { + $contact = DB::table('user_vault')->where('vault_id', $vault->id) + ->where('user_id', $this->id) + ->first(); + + return Contact::findOrFail($contact->contact_id); + } } diff --git a/domains/Contact/ManageContact/Web/ViewHelpers/ContactShowViewHelper.php b/domains/Contact/ManageContact/Web/ViewHelpers/ContactShowViewHelper.php index 8ca814b0a03..1e07171ae4f 100644 --- a/domains/Contact/ManageContact/Web/ViewHelpers/ContactShowViewHelper.php +++ b/domains/Contact/ManageContact/Web/ViewHelpers/ContactShowViewHelper.php @@ -45,7 +45,14 @@ public static function data(Contact $contact, User $user): array 'template_pages' => $templatesPagesCollection, 'contact_information' => self::getContactInformation($templatePages, $contact, $user), 'modules' => $firstPage ? self::modules($firstPage, $contact, $user) : [], + 'options' => [ + 'can_be_deleted' => $user->getContactInVault($contact->vault)->id !== $contact->id, + ], 'url' => [ + 'update_template' => route('contact.blank', [ + 'vault' => $contact->vault_id, + 'contact' => $contact->id, + ]), 'destroy' => route('contact.destroy', [ 'vault' => $contact->vault_id, 'contact' => $contact->id, @@ -63,7 +70,14 @@ public static function dataForTemplatePage(Contact $contact, User $user, Templat 'template_pages' => self::getTemplatePagesList($templatePages, $contact, $templatePage), 'contact_information' => self::getContactInformation($templatePages, $contact, $user), 'modules' => self::modules($templatePage, $contact, $user), + 'options' => [ + 'can_be_deleted' => $user->getContactInVault($contact->vault)->id !== $contact->id, + ], 'url' => [ + 'update_template' => route('contact.blank', [ + 'vault' => $contact->vault_id, + 'contact' => $contact->id, + ]), 'destroy' => route('contact.destroy', [ 'vault' => $contact->vault_id, 'contact' => $contact->id, diff --git a/resources/js/Pages/Vault/Contact/Blank.vue b/resources/js/Pages/Vault/Contact/Blank.vue index 9382e97dda3..0c29205e398 100644 --- a/resources/js/Pages/Vault/Contact/Blank.vue +++ b/resources/js/Pages/Vault/Contact/Blank.vue @@ -67,15 +67,13 @@
-

- This contact doesn't have an associated template. That means we don't know how to display this contact. -

-

- Please choose one template below to tell Monica how a contact should be displayed. +

+ Please choose one template below to tell Monica how this contact should be displayed. Templates let you + define which data should be diplayed on the contact page.

- However, it seems that there are no templates in the account yet. Please add at least template to your - account first, then associate this template with this contact. + It seems that there are no templates in the account yet. Please add at least template to your account + first, then associate this template with this contact.

diff --git a/resources/js/Pages/Vault/Contact/Show.vue b/resources/js/Pages/Vault/Contact/Show.vue index 9fa8af22520..edf8d494a5a 100644 --- a/resources/js/Pages/Vault/Contact/Show.vue +++ b/resources/js/Pages/Vault/Contact/Show.vue @@ -60,8 +60,13 @@ -