Skip to content

Commit eef206d

Browse files
authored
fix: default template cant be deleted (monicahq#6911)
1 parent b6b78e5 commit eef206d

File tree

9 files changed

+44
-3
lines changed

9 files changed

+44
-3
lines changed

app/Domains/Settings/CreateAccount/Jobs/SetupAccount.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ private function addTemplate(): void
122122
'author_id' => $this->author->id,
123123
'name' => null,
124124
'name_translation_key' => trans_key('Default template'),
125+
'can_be_deleted' => false,
125126
];
126127

127128
$this->template = (new CreateTemplate())->execute($request);

app/Domains/Settings/ManageTemplates/Services/CreateTemplate.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function rules(): array
2121
'author_id' => 'required|uuid|exists:users,id',
2222
'name' => 'nullable|string|max:255',
2323
'name_translation_key' => 'nullable|string|max:255',
24+
'can_be_deleted' => 'nullable|boolean',
2425
];
2526
}
2627

@@ -46,6 +47,7 @@ public function execute(array $data): Template
4647
'account_id' => $data['account_id'],
4748
'name' => $data['name'] ?? null,
4849
'name_translation_key' => $data['name_translation_key'] ?? null,
50+
'can_be_deleted' => $data['can_be_deleted'] ?? true,
4951
]);
5052

5153
// A template has at least one page: the Contact information page.

app/Domains/Settings/ManageTemplates/Web/ViewHelpers/PersonalizeTemplateIndexViewHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static function dtoTemplate(Template $template): array
2929
return [
3030
'id' => $template->id,
3131
'name' => $template->name,
32+
'can_be_deleted' => $template->can_be_deleted,
3233
'url' => [
3334
'show' => route('settings.personalize.template.show', [
3435
'template' => $template->id,

app/Models/Template.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ class Template extends Model
2121
'account_id',
2222
'name',
2323
'name_translation_key',
24+
'can_be_deleted',
25+
];
26+
27+
/**
28+
* The attributes that should be cast to native types.
29+
*
30+
* @var array<string, string>
31+
*/
32+
protected $casts = [
33+
'can_be_deleted' => 'boolean',
2434
];
2535

2636
/**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::table('templates', function (Blueprint $table) {
12+
$table->boolean('can_be_deleted')->default(true)->after('name_translation_key');
13+
});
14+
}
15+
16+
public function down()
17+
{
18+
Schema::table('templates', function (Blueprint $table) {
19+
$table->dropColumn('can_be_deleted');
20+
});
21+
}
22+
};

resources/js/Pages/Settings/Personalize/Templates/Index.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@
140140
<li class="me-4 inline cursor-pointer" @click="showUpdateTemplateModal(template)">
141141
<span class="text-blue-500 hover:underline">{{ $t('Rename') }}</span>
142142
</li>
143-
<li class="inline cursor-pointer text-red-500 hover:text-red-900" @click="destroy(template)">
143+
<li
144+
v-if="template.can_be_deleted"
145+
class="inline cursor-pointer text-red-500 hover:text-red-900"
146+
@click="destroy(template)">
144147
{{ $t('Delete') }}
145148
</li>
146149
</ul>

resources/js/Pages/Settings/Personalize/Templates/Partials/Pages.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<!-- contact information page | can't be removed -->
99
<div
1010
:class="isSelectedId === data.template_page_contact_information.id ? 'border-2 bg-sky-100' : ''"
11-
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"
11+
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"
1212
@click="selectPage(data.template_page_contact_information)">
1313
<!-- detail of a page -->
1414
<div>
@@ -42,7 +42,7 @@
4242
<div
4343
v-if="renamePageModalShownId !== element.id"
4444
:class="isSelectedId === element.id ? 'border-2 bg-sky-100' : ''"
45-
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"
45+
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"
4646
@click="selectPage(element)">
4747
<!-- icon to move position -->
4848
<div class="me-2">

tests/Unit/Domains/Settings/CreateAccount/Services/SetupAccountTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function it_sets_an_account_up(): void
4848
$this->assertDatabaseHas('templates', [
4949
'account_id' => $user->account_id,
5050
'name_translation_key' => 'Default template',
51+
'can_be_deleted' => false,
5152
]);
5253
$this->assertDatabaseHas('template_pages', [
5354
'name_translation_key' => 'Contact information',

tests/Unit/Domains/Settings/ManageTemplates/Web/ViewHelpers/PersonalizeTemplateIndexViewHelperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function it_gets_the_data_needed_for_the_data_transfer_object(): void
4242
[
4343
'id' => $template->id,
4444
'name' => $template->name,
45+
'can_be_deleted' => $template->can_be_deleted,
4546
'url' => [
4647
'show' => env('APP_URL').'/settings/personalize/templates/'.$template->id,
4748
'update' => env('APP_URL').'/settings/personalize/templates/'.$template->id,

0 commit comments

Comments
 (0)