Skip to content

Commit

Permalink
feat: change template on contact page (monicahq/chandler#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored May 17, 2022
1 parent c84dd13 commit efc56df
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 11 deletions.
17 changes: 17 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
12 changes: 5 additions & 7 deletions resources/js/Pages/Vault/Contact/Blank.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,13 @@
</svg>

<div>
<p class="mb-2">
This contact doesn't have an associated template. That means we don't know how to display this contact.
</p>
<p v-if="data.templates.length > 0">
Please choose one template below to tell Monica how a contact should be displayed.
<p v-if="data.templates.length > 0" class="mb-2">
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.
</p>
<p v-else>
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.
</p>
</div>
</div>
Expand Down
9 changes: 7 additions & 2 deletions resources/js/Pages/Vault/Contact/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@
</div>
</div>

<ul class="text-sm">
<li>
<ul class="text-xs">
<li class="mb-2">
<inertia-link :href="data.url.update_template" class="cursor-pointer text-sky-500 hover:text-blue-900"
>Change template</inertia-link
>
</li>
<li v-if="data.options.can_be_deleted">
<span class="cursor-pointer text-sky-500 hover:text-blue-900" @click="destroy">Delete contact</span>
</li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
Route::get('/edit', [ContactController::class, 'edit'])->name('contact.edit');
Route::post('', [ContactController::class, 'update'])->name('contact.update');
Route::delete('', [ContactController::class, 'destroy'])->name('contact.destroy');
Route::get('no-template', [ContactNoTemplateController::class, 'show'])->name('contact.blank');
Route::get('update-template', [ContactNoTemplateController::class, 'show'])->name('contact.blank');
Route::put('template', [ContactTemplateController::class, 'update'])->name('contact.template.update');

Route::get('tabs/{slug}', [ContactPageController::class, 'show'])->name('contact.page.show');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Template;
use App\Models\TemplatePage;
use App\Models\User;
use App\Models\Vault;
use function env;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
Expand All @@ -21,6 +22,16 @@ public function it_gets_the_data_needed_for_the_view(): void
{
$contact = Contact::factory()->create();
$user = User::factory()->create();
$vault = Vault::factory()->create([
'account_id' => $user->account_id,
]);
$contact = Contact::factory()->create([
'vault_id' => $vault->id,
]);
$vault->users()->save($user, [
'permission' => 1,
'contact_id' => $contact->id,
]);
$template = Template::factory()->create();
TemplatePage::factory()->create([
'template_id' => $template->id,
Expand All @@ -37,14 +48,15 @@ public function it_gets_the_data_needed_for_the_view(): void
$array = ContactShowViewHelper::data($contact, $user);

$this->assertEquals(
5,
6,
count($array)
);

$this->assertArrayHasKey('contact_name', $array);
$this->assertArrayHasKey('template_pages', $array);
$this->assertArrayHasKey('contact_information', $array);
$this->assertArrayHasKey('modules', $array);
$this->assertArrayHasKey('options', $array);
$this->assertArrayHasKey('url', $array);

$this->assertEquals(
Expand All @@ -62,6 +74,7 @@ public function it_gets_the_data_needed_for_the_view(): void
);
$this->assertEquals(
[
'update_template' => env('APP_URL').'/vaults/'.$contact->vault->id.'/contacts/'.$contact->id.'/update-template',
'destroy' => env('APP_URL').'/vaults/'.$contact->vault->id.'/contacts/'.$contact->id,
],
$array['url']
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/Models/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Unit\Models;

use App\Models\Contact;
use App\Models\ContactTask;
use App\Models\Note;
use App\Models\User;
Expand Down Expand Up @@ -81,4 +82,28 @@ public function it_returns_the_name_attribute(): void
$rachel->name,
);
}

/** @test */
public function it_returns_the_contact_in_the_vault(): void
{
$rachel = User::factory()->create([
'first_name' => 'Dwight',
'last_name' => 'Schrute',
]);
$vault = Vault::factory()->create([
'account_id' => $rachel->account_id,
]);
$contact = Contact::factory()->create([
'vault_id' => $vault->id,
]);
$vault->users()->save($rachel, [
'permission' => 1,
'contact_id' => $contact->id,
]);

$this->assertEquals(
$contact->id,
$rachel->getContactInVault($vault)->id,
);
}
}

0 comments on commit efc56df

Please sign in to comment.