Skip to content

Commit

Permalink
✨ Add form submission event, add values data object
Browse files Browse the repository at this point in the history
  • Loading branch information
tgeorgel committed Oct 21, 2024
1 parent 7f95c34 commit 6fc53e8
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 7 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"nevadskiy/laravel-tree": "^0.5.0",
"ralphjsmit/laravel-filament-media-library": "^3.8",
"spatie/eloquent-sortable": "^4.2",
"spatie/laravel-data": "^4.10",
"spatie/laravel-package-tools": "^1.15.0",
"spatie/laravel-permission": "^6.3",
"tgeorgel/filament-tree": "^2.1",
Expand Down
20 changes: 18 additions & 2 deletions src/Actions/Form/SubmitFormEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,37 @@

use Hydrat\GroguCMS\Models\Form;
use Hydrat\GroguCMS\Models\FormEntry;
use Hydrat\GroguCMS\Models\FormField;
use Lorisleiva\Actions\Concerns\AsAction;
use Hydrat\GroguCMS\Events\FormEntryCreated;

class SubmitFormEntry
{
use AsAction;

public function handle(Form $form, array $validated): FormEntry
{
$form->loadMissing('fields');

$fields = $form->fields->sortBy('order')->map(
fn (FormField $field) => [
'key' => $field->key,
'type' => $field->type,
'label' => $field->name,
'value' => $validated[$field->key] ?? null,
'required' => $field->required,
]
);

$values = FormEntryValue::collect($fields);

$entry = $form->entries()->create([
'user_id' => auth()->id(),
'values' => $validated,
'submitted_at' => now(),
'values' => $values,
]);

// trigger submission events
dispatch(new FormEntryCreated($entry));

return $entry;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Datas/FormEntryValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Hydrat\GroguCMS\Datas;

use Spatie\LaravelData\Data;
use Hydrat\GroguCMS\Enums\FormFieldType;

class FormEntryValue extends Data
{
public function __construct(
public string $key,
public FormFieldType $type,
public string $label,
public mixed $value = null,
public bool $required = false,
) {
}
}
35 changes: 35 additions & 0 deletions src/Events/FormEntryCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Hydrat\GroguCMS\Events;

use Hydrat\GroguCMS\Models\FormEntry;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;

class FormEntryCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public FormEntry $entry,
) {
//
}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('channel-name'),
];
}
}
6 changes: 3 additions & 3 deletions src/Livewire/ContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public function submit()
);
}

$this->dispatch('form-validated');
$this->dispatch('form-validated', $this->form, $this->data);

Actions\SubmitFormEntry::run($this->form, $this->data);
$formEntry = Actions\SubmitFormEntry::run($this->form, $this->data);

$this->data = [];
$this->onSuccessMessage = $this->form->submit_success_message;

$this->dispatch('form-submitted');
$this->dispatch('form-submitted', $formEntry);
}

public function render()
Expand Down
6 changes: 4 additions & 2 deletions src/Models/FormEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Hydrat\GroguCMS\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Spatie\LaravelData\DataCollection;
use Illuminate\Database\Eloquent\Model;
use Hydrat\GroguCMS\Datas\FormEntryValue;
use Illuminate\Database\Eloquent\Relations;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class FormEntry extends Model
{
Expand All @@ -28,8 +30,8 @@ class FormEntry extends Model
* @var array
*/
protected $casts = [
'values' => 'collection',
'submitted_at' => 'datetime',
'values' => DataCollection::class.':'.FormEntryValue::class,
];

public function user(): Relations\BelongsTo
Expand Down

0 comments on commit 6fc53e8

Please sign in to comment.