Skip to content

Commit

Permalink
feat: manage contact information type (monicahq/chandler#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Jan 5, 2022
1 parent b8b1bc5 commit 3ef57dc
Show file tree
Hide file tree
Showing 19 changed files with 472 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace App\Http\Controllers\Settings\Personalize\ContactInformationTypes;

use Inertia\Inertia;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Vault\ViewHelpers\VaultIndexViewHelper;
use App\Services\Account\ManageContactInformationTypes\CreateContactInformationType;
use App\Services\Account\ManageContactInformationTypes\UpdateContactInformationType;
use App\Services\Account\ManageContactInformationTypes\DestroyContactInformationType;
use App\Http\Controllers\Settings\Personalize\ContactInformationTypes\ViewHelpers\PersonalizeContactInformationTypeIndexViewHelper;

class PersonalizeContatInformationTypesController extends Controller
{
public function index()
{
return Inertia::render('Settings/Personalize/ContactInformationTypes/Index', [
'layoutData' => VaultIndexViewHelper::layoutData(),
'data' => PersonalizeContactInformationTypeIndexViewHelper::data(Auth::user()->account),
]);
}

public function store(Request $request)
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'name' => $request->input('name'),
'protocol' => $request->input('protocol'),
];

$contactInformationType = (new CreateContactInformationType)->execute($data);

return response()->json([
'data' => PersonalizeContactInformationTypeIndexViewHelper::dtoContactInformationType($contactInformationType),
], 201);
}

public function update(Request $request, int $contactInformationTypeId)
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'contact_information_type_id' => $contactInformationTypeId,
'name' => $request->input('name'),
'protocol' => $request->input('protocol'),
];

$contactInformationType = (new UpdateContactInformationType)->execute($data);

return response()->json([
'data' => PersonalizeContactInformationTypeIndexViewHelper::dtoContactInformationType($contactInformationType),
], 200);
}

public function destroy(Request $request, int $contactInformationTypeId)
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'contact_information_type_id' => $contactInformationTypeId,
];

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

return response()->json([
'data' => true,
], 200);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Http\Controllers\Settings\Personalize\ContactInformationTypes\ViewHelpers;

use App\Models\Account;
use App\Models\ContactInformationType;

class PersonalizeContactInformationTypeIndexViewHelper
{
public static function data(Account $account): array
{
$types = $account->contactInformationTypes()
->orderBy('name', 'asc')
->get();

$collection = collect();
foreach ($types as $type) {
$collection->push(self::dtoContactInformationType($type));
}

return [
'contact_information_types' => $collection,
'url' => [
'settings' => route('settings.index'),
'personalize' => route('settings.personalize.index'),
'contact_information_type_store' => route('settings.personalize.contact_information_type.store'),
],
];
}

public static function dtoContactInformationType(ContactInformationType $type): array
{
return [
'id' => $type->id,
'name' => $type->name,
'protocol' => $type->protocol,
'can_be_deleted' => $type->can_be_deleted,
'url' => [
'update' => route('settings.personalize.contact_information_type.update', [
'type' => $type->id,
]),
'destroy' => route('settings.personalize.contact_information_type.destroy', [
'type' => $type->id,
]),
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static function data(): array
'manage_pronouns' => route('settings.personalize.pronoun.index'),
'manage_address_types' => route('settings.personalize.address_type.index'),
'manage_pet_categories' => route('settings.personalize.pet_category.index'),
'manage_contact_information_types' => route('settings.personalize.contact_information_type.index'),
],
];
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/ContactInformationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ContactInformationType extends Model
'account_id',
'name',
'protocol',
'type', // used to make sure phone and email fields can't be deleted
'type', // used to make sure phone and email fields can't be deleted, and also to find the email address of the contact quickly
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
🏖
</span> All the address types
</h3>
<pretty-button v-if="!createAddressTypeModalShown" :text="'Add a label'" :icon="'plus'" @click="showAddressTypeModal" />
<pretty-button v-if="!createAddressTypeModalShown" :text="'Add an address type'" :icon="'plus'" @click="showAddressTypeModal" />
</div>

<!-- modal to create a new group type -->
Expand All @@ -75,7 +75,7 @@

<div class="p-5 flex justify-between">
<pretty-span :text="'Cancel'" :classes="'mr-3'" @click="createAddressTypeModalShown = false" />
<pretty-button :text="'Create label'" :state="loadingState" :icon="'plus'" :classes="'save'" />
<pretty-button :text="'Create address type'" :state="loadingState" :icon="'plus'" :classes="'save'" />
</div>
</form>

Expand Down
Loading

0 comments on commit 3ef57dc

Please sign in to comment.