This commit is contained in:
Benjamin Beganović 2021-01-12 16:23:39 +01:00
parent ef3f77b82f
commit cdd62f7e2a
2 changed files with 54 additions and 9 deletions

View File

@ -14,23 +14,33 @@
namespace App\Http\Livewire;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Livewire\Component;
class RequiredClientInfo extends Component
{
public $fields = [];
public $contact;
private $gateway;
private $mappings = [
'client_name' => 'name',
'contact_phone' => 'phone',
];
public function handleSubmit(array $data): bool
{
$rules = [];
collect($this->fields)->map(function ($field) use (&$rules) {
$rules[$field['name']] = $field['validation_rules'];
$rules[$field['name']] = array_key_exists('validation_rules', $field)
? $field['validation_rules']
: 'required';
});
$validator = Validator::make($data, $rules);
if ($validator->fails()) {
@ -39,9 +49,44 @@ class RequiredClientInfo extends Component
return false;
}
$this->emit('passed-required-fields-check');
if ($this->updateClientDetails($data)) {
$this->emit('passed-required-fields-check');
return true;
return true;
}
// TODO: Throw an exception about not being able to update the profile.
return false;
}
private function updateClientDetails(array $data): bool
{
$client = [];
$contact = [];
foreach ($data as $field => $value) {
if (Str::startsWith($field, 'client_', )) {
$client[$this->mappings[$field]] = $value;
}
if (Str::startsWith($field, 'contact_')) {
$contact[$this->mappings[$field]] = $value;
}
}
$contact_update = $this->contact
->fill($contact)
->push();
$client_update = $this->contact->client
->fill($client)
->push();
if ($contact_update && $client_update) {
return true;
}
return false;
}
public function render()
@ -50,14 +95,14 @@ class RequiredClientInfo extends Component
$this->fields = [
[
'name' => 'client_first_name',
'label' => ctrans('texts.first_name'),
'name' => 'client_name',
'label' => ctrans('texts.name'),
'type' => 'text',
'validation_rules' => 'required|min:3'
],
[
'name' => 'client_billing_address_zip',
'label' => ctrans('texts.postal_code'),
'name' => 'contact_phone',
'label' => ctrans('texts.phone'),
'type' => 'number',
'validation_rules' => 'required|min:2',
],

View File

@ -11,7 +11,7 @@
@endpush
@section('body')
@livewire('required-client-info', ['gateway' => $gateway])
@livewire('required-client-info', ['gateway' => $gateway, 'contact' => auth('contact')->user()])
<div class="container mx-auto grid grid-cols-12 hidden" data-ref="gateway-container">
<div class="col-span-12 lg:col-span-6 lg:col-start-4 overflow-hidden bg-white shadow rounded-lg">