-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Morten Bak
committed
Aug 24, 2024
1 parent
93f0778
commit 284dfa8
Showing
7 changed files
with
287 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace App\Livewire\Admin\Plans; | ||
|
||
use App\Models\Plan; | ||
use Illuminate\Contracts\View\View; | ||
use Jantinnerezo\LivewireAlert\LivewireAlert; | ||
use Livewire\Attributes\Rule; | ||
use LivewireUI\Modal\ModalComponent; | ||
|
||
class EditPlan extends ModalComponent | ||
{ | ||
use LivewireAlert; | ||
|
||
#[Rule(['required', 'max:255', 'unique:plans,title'])] | ||
public string $title = ''; | ||
|
||
#[Rule(['required', 'max:255'])] | ||
public string $slug = ''; | ||
|
||
#[Rule(['required', 'max:255'])] | ||
public string $stripe_id = ''; | ||
|
||
public Plan $plan; | ||
|
||
public function mount(Plan $plan) | ||
{ | ||
abort_if(! auth()->check(), 403); | ||
abort_unless(auth()->user()->hasPermissionTo('edit plans'), 403); | ||
|
||
$this->plan = $plan; | ||
$this->title = $plan->title ?? ''; | ||
$this->slug = $plan->slug ?? ''; | ||
$this->stripe_id = $plan->stripe_id ?? ''; | ||
} | ||
|
||
public function save() | ||
{ | ||
$this->validate(); | ||
|
||
$this->plan->update([ | ||
'title' => $this->title, | ||
'slug' => $this->slug, | ||
'stripe_id' => $this->stripe_id, | ||
]); | ||
|
||
$this->alert('success', __('plans.update_successful')); | ||
|
||
$this->dispatch('planUpdated'); | ||
|
||
$this->closeModal(); | ||
} | ||
|
||
public function render(): View | ||
{ | ||
return view('livewire.admin.plans.edit-plan'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
return [ | ||
'edit_plan' => 'Edit plan', | ||
'delete_plan' => 'Delete plan', | ||
'confirm_deletion' => 'Are you sure you want to delete this plan?', | ||
'no_plans_found' => 'No plans was found', | ||
'plan_was_deleted' => 'The Plan was deleted', | ||
'something_went_wrong' => 'Something went wrong.', | ||
'update_successful' => 'Plan was updated', | ||
'save' => 'Save', | ||
'cancel' => 'Cancel', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<div class="p-4 w-full dark:bg-gray-900 flex flex-col space-y-4"> | ||
|
||
<h2 class="text-2xl font-bold dark:text-white"> | ||
{{ __('Edit Plan') }} | ||
</h2> | ||
|
||
<div class="form-group"> | ||
<label for="title" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white"> | ||
{{ __('Title') }} | ||
</label> | ||
<input id="title" wire:model.live="title" type="text" | ||
class="@error('title') is-invalid @enderror bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" | ||
placeholder="Enter title" required> | ||
@error('title') <span class="text-danger">{{ $message }}</span>@enderror | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label for="slug" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white"> | ||
{{ __('Slug') }} | ||
</label> | ||
<input id="slug" wire:model.live="slug" type="text" | ||
class="@error('slug') is-invalid @enderror bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" | ||
placeholder="Enter slug" required> | ||
@error('slug') <span class="text-danger">{{ $message }}</span>@enderror | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label for="stripe_id" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white"> | ||
{{ __('Stripe ID') }} | ||
</label> | ||
<input id="stripe_id" wire:model.live="stripe_id" type="text" | ||
class="@error('stripe_id') is-invalid @enderror bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" | ||
placeholder="Enter Stripe ID" required> | ||
@error('stripe_id') <span class="text-danger">{{ $message }}</span>@enderror | ||
</div> | ||
|
||
<div class="flex justify-between items-center"> | ||
<button wire:click.prevent="save" class="text-white bg-primary-700 hover:bg-primary-800 focus:ring-4 focus:outline-none focus:ring-primary-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800"> | ||
{{ __('plans.save') }} | ||
</button> | ||
<x-secondary-button wire:click="closeModal"> | ||
{{ __('plans.cancel') }} | ||
</x-secondary-button> | ||
</div> | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,87 @@ | ||
<?php | ||
|
||
use App\Livewire\Admin\Plans\EditPlan; | ||
use App\Models\Plan; | ||
use App\Models\User; | ||
use Livewire\Livewire; | ||
|
||
use function Pest\Laravel\assertDatabaseMissing; | ||
|
||
it('requires the correct access to view the component', function () { | ||
|
||
Livewire::test(EditPlan::class) | ||
->assertForbidden(); | ||
|
||
$user = User::factory()->create(); | ||
/** @var User $user */ | ||
$user->givePermissionTo('edit plans'); | ||
|
||
Livewire::actingAs($user) | ||
->test(EditPlan::class) | ||
->assertOk(); | ||
}); | ||
|
||
it('can fetch the plan from the provided plan id', function () { | ||
Livewire::test(EditPlan::class) | ||
->assertForbidden(); | ||
|
||
$user = User::factory()->create(); | ||
/** @var User $user */ | ||
$user->givePermissionTo('edit plans'); | ||
|
||
$plan = Plan::factory()->create(); | ||
|
||
Livewire::actingAs($user) | ||
->test(EditPlan::class, ['plan' => $plan]) | ||
->assertOk() | ||
->assertSet('title', $plan->title) | ||
->assertSet('slug', $plan->slug) | ||
->assertSet('stripe_id', $plan->stripe_id); | ||
|
||
}); | ||
|
||
it('has wired properties and methods', function () { | ||
$user = User::factory()->create(); | ||
/** @var User $user */ | ||
$user->givePermissionTo('edit plans'); | ||
$plan = Plan::factory() | ||
->create(); | ||
|
||
Livewire::actingAs($user) | ||
->test(EditPlan::class, ['plan' => $plan]) | ||
->assertOk() | ||
->assertPropertyWired('title') | ||
->assertPropertyWired('slug') | ||
->assertPropertyWired('stripe_id') | ||
->assertMethodWired('save'); | ||
}); | ||
|
||
it('can save a updated plan', function () { | ||
|
||
$user = User::factory()->create(); | ||
/** @var User $user */ | ||
$user->givePermissionTo('edit plans'); | ||
$plan = Plan::factory() | ||
->create(); | ||
|
||
Livewire::actingAs($user) | ||
->test(EditPlan::class, ['plan' => $plan]) | ||
->assertOk() | ||
->set('title', 'New Plan Title') | ||
->set('slug', 'New-Plan-Slug') | ||
->set('stripe_id', 'New Stripe ID') | ||
->call('save') | ||
->assertHasNoErrors(); | ||
|
||
assertDatabaseMissing('plans', [ | ||
'title' => $plan->title, | ||
'slug' => $plan->slug, | ||
'stripe_id' => $plan->stripe_id, | ||
]); | ||
|
||
expect($plan->refresh()) | ||
->title->toBe('New Plan Title') | ||
->slug->toBe('New-Plan-Slug') | ||
->stripe_id->toBe('New Stripe ID'); | ||
|
||
}); |