Skip to content

Commit

Permalink
feat: move contact to another vault (monicahq/chandler#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Dec 14, 2022
1 parent 3914433 commit 34720f4
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Domains\Contact\ManageContact\Web\Controllers;

use App\Domains\Contact\ManageContact\Services\MoveContactToAnotherVault;
use App\Domains\Contact\ManageContact\Web\ViewHelpers\ContactShowMoveViewHelper;
use App\Domains\Vault\ManageVault\Web\ViewHelpers\VaultIndexViewHelper;
use App\Http\Controllers\Controller;
use App\Models\Contact;
use App\Models\Vault;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;
use Redirect;

class ContactMoveController extends Controller
{
public function show(Request $request, int $vaultId, int $contactId)
{
$vault = Vault::findOrFail($vaultId);
$contact = Contact::findOrFail($contactId);

return Inertia::render('Vault/Contact/Move', [
'layoutData' => VaultIndexViewHelper::layoutData($vault),
'data' => ContactShowMoveViewHelper::data($contact, Auth::user()),
]);
}

public function store(Request $request, int $vaultId, int $contactId)
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::id(),
'vault_id' => $vaultId,
'other_vault_id' => $request->input('other_vault_id'),
'contact_id' => $contactId,
];

(new MoveContactToAnotherVault())->execute($data);

return Redirect::route('contact.show', [
'vault' => $request->input('other_vault_id'),
'contact' => $contactId,
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Domains\Contact\ManageContact\Web\ViewHelpers;

use App\Models\Contact;
use App\Models\User;
use App\Models\Vault;

class ContactShowMoveViewHelper
{
public static function data(Contact $contact, User $user): array
{
$vaultsCollection = $user->vaults()
->withCount('contacts')
->orderBy('name', 'asc')
->get()
->filter(fn (Vault $vault) => $vault->id !== $contact->vault_id)
->map(fn (Vault $vault) => [
'id' => $vault->id,
'name' => $vault->name,
'count' => $vault->contacts_count,
]);

return [
'vaults' => $vaultsCollection,
'contact' => [
'name' => $contact->name,
],
'url' => [
'move' => route('contact.move.store', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public static function data(Contact $contact, User $user): array
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
'move_contact' => route('contact.move.show', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
'destroy' => route('contact.destroy', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
Expand Down Expand Up @@ -128,6 +132,10 @@ public static function dataForTemplatePage(Contact $contact, User $user, Templat
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
'move_contact' => route('contact.move.show', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
'destroy' => route('contact.destroy', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
Expand Down
1 change: 1 addition & 0 deletions lang/en/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'contact_archive_cta' => 'Archive contact',
'contact_unarchive_cta' => 'Unarchive contact',
'contact_change_template_cta' => 'Change template',
'contact_move_contact_cta' => 'Move contact',
'contact_delete_cta' => 'Delete contact',
'contact_archived' => 'The contact is archived',
'contact_toggle_confirm' => 'Are you sure?',
Expand Down
117 changes: 117 additions & 0 deletions resources/js/Pages/Vault/Contact/Move.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<script setup>
import Layout from '@/Shared/Layout.vue';
import { useForm } from '@inertiajs/inertia-vue3';
const props = defineProps({
layoutData: Object,
data: Object,
});
const form = useForm({
other_vault_id: 0,
});
const move = (vault) => {
form.other_vault_id = vault.id;
form.post(props.data.url.move);
};
</script>

<template>
<layout :layout-data="layoutData" :inside-vault="true">
<!-- breadcrumb -->
<nav class="bg-white dark:bg-gray-900 sm:mt-20 sm:border-b">
<div class="max-w-8xl mx-auto hidden px-4 py-2 sm:px-6 md:block">
<div class="flex items-baseline justify-between space-x-6">
<ul class="text-sm">
<li class="mr-2 inline text-gray-600 dark:text-gray-400">
{{ $t('app.breadcrumb_location') }}
</li>
<li class="mr-2 inline">
<inertia-link :href="layoutData.vault.url.contacts" class="text-blue-500 hover:underline">
Contacts
</inertia-link>
</li>
<li class="relative mr-2 inline">
<svg
xmlns="http://www.w3.org/2000/svg"
class="icon-breadcrumb relative inline h-3 w-3"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
</svg>
</li>
<li class="inline">
{{ data.contact.name }}
</li>
</ul>
</div>
</div>
</nav>

<main class="relative sm:mt-24">
<div class="mx-auto max-w-3xl px-2 py-2 sm:py-6 sm:px-6 lg:px-8">
<h2 class="mb-6 text-center text-lg">Move {{ data.contact.name }} to another vault</h2>
<div class="mb-6 rounded border border-gray-200 bg-white dark:border-gray-700 dark:bg-gray-900">
<!-- help -->
<div
class="flex rounded-t border-b border-gray-200 bg-slate-50 px-3 py-2 dark:border-gray-700 dark:bg-slate-900 dark:bg-slate-900">
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-6 grow pr-2"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor">
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>

<p>
You can move contacts between vaults. This change is immediate. You can only move contacts to vaults you
are part of. All the contacts data will move with it.
</p>
</div>

<ul v-if="data.vaults" class="rounded-b bg-white dark:bg-gray-900">
<li
v-for="vault in data.vaults"
:key="vault.id"
class="item-list border-b border-gray-200 hover:bg-slate-50 dark:border-gray-700 dark:bg-slate-900 hover:dark:bg-slate-800">
<div class="flex items-center justify-between px-5 py-2">
<span>{{ vault.name }}</span>

<!-- actions -->
<ul class="text-sm">
<li class="inline cursor-pointer text-blue-500 hover:underline" @click="move(vault)">Choose</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
</main>
</layout>
</template>

<style lang="scss" scoped>
.item-list {
&:hover:first-child {
border-top-left-radius: 8px;
border-top-right-radius: 8px;
}
&:last-child {
border-bottom: 0;
}
&:hover:last-child {
border-bottom-left-radius: 8px;
border-bottom-right-radius: 8px;
}
}
</style>
6 changes: 6 additions & 0 deletions resources/js/Pages/Vault/Contact/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ const destroyAvatar = () => {
{{ $t('contact.contact_change_template_cta') }}
</Link>
</li>
<!-- move contact to another vault -->
<li class="mb-2">
<Link :href="data.url.move_contact" class="cursor-pointer text-blue-500 hover:underline">
{{ $t('contact.contact_move_contact_cta') }}
</Link>
</li>
<!-- delete contact -->
<li v-if="data.options.can_be_deleted">
<span class="cursor-pointer text-blue-500 hover:underline" @click="deletingContact = true">
Expand Down
5 changes: 5 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Domains\Contact\ManageContact\Web\Controllers\ContactController;
use App\Domains\Contact\ManageContact\Web\Controllers\ContactFavoriteController;
use App\Domains\Contact\ManageContact\Web\Controllers\ContactLabelController;
use App\Domains\Contact\ManageContact\Web\Controllers\ContactMoveController;
use App\Domains\Contact\ManageContact\Web\Controllers\ContactNoTemplateController;
use App\Domains\Contact\ManageContact\Web\Controllers\ContactPageController;
use App\Domains\Contact\ManageContact\Web\Controllers\ContactTemplateController;
Expand Down Expand Up @@ -188,6 +189,10 @@
Route::put('/toggle', [ContactArchiveController::class, 'update'])->name('contact.archive.update');
Route::put('/toggle-favorite', [ContactFavoriteController::class, 'update'])->name('contact.favorite.update');

// move contact to another vault
Route::get('/move', [ContactMoveController::class, 'show'])->name('contact.move.show');
Route::post('/move', [ContactMoveController::class, 'store'])->name('contact.move.store');

// template
Route::get('update-template', [ContactNoTemplateController::class, 'show'])->name('contact.blank');
Route::put('template', [ContactTemplateController::class, 'update'])->name('contact.template.update');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Tests\Unit\Domains\Contact\ManageContact\Web\ViewHelpers;

use App\Domains\Contact\ManageContact\Web\ViewHelpers\ContactShowMoveViewHelper;
use App\Models\Contact;
use App\Models\User;
use App\Models\Vault;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

class ContactShowMoveViewHelperTest extends TestCase
{
use DatabaseTransactions;

/** @test */
public function it_gets_the_data_needed_for_the_view(): void
{
$user = User::factory()->create();
$vault = Vault::factory()->create();
$user->vaults()->attach($vault->id, [
'permission' => Vault::PERMISSION_EDIT,
'contact_id' => Contact::factory()->create()->id,
]);
$otherVault = Vault::factory()->create();
$user->vaults()->attach($otherVault->id, [
'permission' => Vault::PERMISSION_EDIT,
'contact_id' => Contact::factory()->create()->id,
]);
$contact = Contact::factory()->create([
'vault_id' => $otherVault->id,
]);

$this->be($user);

$array = ContactShowMoveViewHelper::data($contact, $user);
$this->assertCount(3, $array);
$this->assertEquals(
[
'name' => $contact->name,
],
$array['contact']
);
$this->assertEquals(
[
'move' => env('APP_URL').'/vaults/'.$otherVault->id.'/contacts/'.$contact->id.'/move',
],
$array['url']
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function it_gets_the_data_needed_for_the_view(): void
'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,
'update_avatar' => env('APP_URL').'/vaults/'.$contact->vault->id.'/contacts/'.$contact->id.'/avatar',
'move_contact' => env('APP_URL').'/vaults/'.$contact->vault->id.'/contacts/'.$contact->id.'/move',
'destroy_avatar' => env('APP_URL').'/vaults/'.$contact->vault->id.'/contacts/'.$contact->id.'/avatar',
],
$array['url']
Expand Down

0 comments on commit 34720f4

Please sign in to comment.