Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ousid committed Nov 8, 2023
1 parent 60355f4 commit e4fab70
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 50 deletions.
14 changes: 6 additions & 8 deletions resources/views/components/turnstile.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@php
$id = $getId();
$statePath = $getStatePath();
$fieldWrapperView = $getFieldWrapperView();
Expand All @@ -13,6 +12,7 @@
<div x-data="{
state: $wire.entangle('{{ $statePath }}').defer
}"
wire:ignore
x-init="(() => {
let options= {
callback: function (token) {
Expand All @@ -27,14 +27,12 @@
window.onloadTurnstileCallback = () => {
turnstile.render($refs.turnstile, options)
}
})"
wire:ignore
})()"
>
<div id="turnstile-widget"
data-sitekey="{{config('turnstile.turnstile_site_key')}}"
:data-theme="$theme"
:data-language="$language"
:data-size="$size"
<div data-sitekey="{{config('turnstile.turnstile_site_key')}}"
data-theme="{{ $theme }}"
data-language="{{ $language }}"
data-size="{{ $size }}"
x-ref="turnstile"
>
</div>
Expand Down
4 changes: 3 additions & 1 deletion src/Forms/Components/Turnstile.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ protected function setUp(): void

$this->label('');

$this->rules(['required', new TurnstileCheck()]);
$this->required();

$this->rule(new TurnstileCheck());

$this->dehydrated(false);
}
Expand Down
37 changes: 4 additions & 33 deletions tests/Fixtures/ContactUs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@

use Coderflex\FilamentTurnstile\Forms\Components\Turnstile;
use Coderflex\FilamentTurnstile\Tests\Models\Contact;
use Filament\Actions\Action;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Pages\Concerns\InteractsWithFormActions;
use Filament\Pages\SimplePage;
use Illuminate\Contracts\View\View;
use Filament\Forms\FormsComponent;

class ContactUs extends SimplePage
class ContactUs extends FormsComponent
{
use InteractsWithActions;
use InteractsWithFormActions;

public ?array $data = [];

public function mount(): void
Expand Down Expand Up @@ -58,30 +51,8 @@ public function send()
Contact::create($this->form->getState());
}

/**
* @return array<Action | ActionGroup>
*/
protected function getFormActions(): array
{
return [
$this->getSendFormAction(),
];
}

protected function getSendFormAction(): Action
{
return Action::make('Send')
->label(__('Send'))
->submit('send');
}

protected function hasFullWidthFormActions(): bool
{
return true;
}

public function render(): View
public function render()
{
return view('fixtures.contact-us');
return 'fixtures.contact-us';
}
}
12 changes: 10 additions & 2 deletions tests/TurnstileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,26 @@
/**
* Setting Turnstile keys to always pass the request
*
* @see https://developers.cloudflare.com/turnstile/reference/testing/#dummy-sitekeys-and-secret-keys
* @link https://developers.cloudflare.com/turnstile/reference/testing/#dummy-sitekeys-and-secret-keys
*/
Config::set('turnstile', [
'turnstile_site_key' => '1x00000000000000000000AA',
'turnstile_secret_key' => '1x0000000000000000000000000000000AA',
]);

/**
* In this context, Alpine.js didn't function as expected due to the need to pass the `cf-captcha` field.
* The value of the mentioned key is dynamically determined by the response from Cloudflare (CF) in the UI.
* For more information, refer to the Cloudflare Turnstile documentation:
*
* @link https://developers.cloudflare.com/turnstile/
*/
livewire(ContactUs::class)
->fillForm([
'name' => 'John Doe',
'email' => '[email protected]',
'content' => 'This is a simple message',
'cf-captcha' => 'XXXX.DUMMY.TOKEN.XXXX',
])
->call('send')
->assertHasNoFormErrors();
Expand All @@ -44,7 +52,7 @@
/**
* Setting Turnstile keys to always block the request
*
* @see https://developers.cloudflare.com/turnstile/reference/testing/#dummy-sitekeys-and-secret-keys
* @link https://developers.cloudflare.com/turnstile/reference/testing/#dummy-sitekeys-and-secret-keys
*/
Config::set('turnstile', [
'turnstile_site_key' => '2x00000000000000000000AB',
Expand Down
9 changes: 3 additions & 6 deletions tests/resources/views/fixtures/contact-us.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<x-filament-panels::page.simple>
<div>
<x-filament-panels::form wire:submit="send">
{{ $this->form }}

<x-filament-panels::form.actions
:actions="$this->getCachedFormActions()"
:full-width="$this->hasFullWidthFormActions()"
/>
<button>Send</button>
</x-filament-panels::form>
</x-filament-panels::page.simple>
</div>

0 comments on commit e4fab70

Please sign in to comment.