Skip to content

Commit

Permalink
fix: default template cant be deleted (#6911)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Oct 2, 2023
1 parent b6b78e5 commit eef206d
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/Domains/Settings/CreateAccount/Jobs/SetupAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private function addTemplate(): void
'author_id' => $this->author->id,
'name' => null,
'name_translation_key' => trans_key('Default template'),
'can_be_deleted' => false,
];

$this->template = (new CreateTemplate())->execute($request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function rules(): array
'author_id' => 'required|uuid|exists:users,id',
'name' => 'nullable|string|max:255',
'name_translation_key' => 'nullable|string|max:255',
'can_be_deleted' => 'nullable|boolean',
];
}

Expand All @@ -46,6 +47,7 @@ public function execute(array $data): Template
'account_id' => $data['account_id'],
'name' => $data['name'] ?? null,
'name_translation_key' => $data['name_translation_key'] ?? null,
'can_be_deleted' => $data['can_be_deleted'] ?? true,
]);

// A template has at least one page: the Contact information page.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static function dtoTemplate(Template $template): array
return [
'id' => $template->id,
'name' => $template->name,
'can_be_deleted' => $template->can_be_deleted,
'url' => [
'show' => route('settings.personalize.template.show', [
'template' => $template->id,
Expand Down
10 changes: 10 additions & 0 deletions app/Models/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class Template extends Model
'account_id',
'name',
'name_translation_key',
'can_be_deleted',
];

/**
* The attributes that should be cast to native types.
*
* @var array<string, string>
*/
protected $casts = [
'can_be_deleted' => 'boolean',
];

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::table('templates', function (Blueprint $table) {
$table->boolean('can_be_deleted')->default(true)->after('name_translation_key');
});
}

public function down()
{
Schema::table('templates', function (Blueprint $table) {
$table->dropColumn('can_be_deleted');
});
}
};
5 changes: 4 additions & 1 deletion resources/js/Pages/Settings/Personalize/Templates/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@
<li class="me-4 inline cursor-pointer" @click="showUpdateTemplateModal(template)">
<span class="text-blue-500 hover:underline">{{ $t('Rename') }}</span>
</li>
<li class="inline cursor-pointer text-red-500 hover:text-red-900" @click="destroy(template)">
<li
v-if="template.can_be_deleted"
class="inline cursor-pointer text-red-500 hover:text-red-900"
@click="destroy(template)">
{{ $t('Delete') }}
</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<!-- contact information page | can't be removed -->
<div
:class="isSelectedId === data.template_page_contact_information.id ? 'border-2 bg-sky-100' : ''"
class="mb-2 flex items-center rounded-lg border border-gray-200 bg-white px-5 py-2 hover:bg-slate-50 dark:border-gray-700 dark:bg-gray-900 hover:dark:bg-slate-800"
class="mb-2 flex items-center rounded-lg border border-gray-200 bg-white px-5 py-2 hover:cursor-pointer hover:bg-slate-50 dark:border-gray-700 dark:bg-gray-900 hover:dark:bg-slate-800"
@click="selectPage(data.template_page_contact_information)">
<!-- detail of a page -->
<div>
Expand Down Expand Up @@ -42,7 +42,7 @@
<div
v-if="renamePageModalShownId !== element.id"
:class="isSelectedId === element.id ? 'border-2 bg-sky-100' : ''"
class="mb-2 flex items-center rounded-lg border border-gray-200 bg-white py-2 pe-5 ps-2 hover:bg-slate-50 dark:border-gray-700 dark:bg-gray-900 hover:dark:bg-slate-800"
class="mb-2 flex items-center rounded-lg hover:cursor-pointer border border-gray-200 bg-white py-2 pe-5 ps-2 hover:bg-slate-50 dark:border-gray-700 dark:bg-gray-900 hover:dark:bg-slate-800"
@click="selectPage(element)">
<!-- icon to move position -->
<div class="me-2">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function it_sets_an_account_up(): void
$this->assertDatabaseHas('templates', [
'account_id' => $user->account_id,
'name_translation_key' => 'Default template',
'can_be_deleted' => false,
]);
$this->assertDatabaseHas('template_pages', [
'name_translation_key' => 'Contact information',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function it_gets_the_data_needed_for_the_data_transfer_object(): void
[
'id' => $template->id,
'name' => $template->name,
'can_be_deleted' => $template->can_be_deleted,
'url' => [
'show' => env('APP_URL').'/settings/personalize/templates/'.$template->id,
'update' => env('APP_URL').'/settings/personalize/templates/'.$template->id,
Expand Down

0 comments on commit eef206d

Please sign in to comment.