diff --git a/app/Http/Livewire/RequiredClientInfo.php b/app/Http/Livewire/RequiredClientInfo.php index d01a3e54fae4..3c174b6b0433 100644 --- a/app/Http/Livewire/RequiredClientInfo.php +++ b/app/Http/Livewire/RequiredClientInfo.php @@ -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', ], diff --git a/resources/views/portal/ninja2020/layout/payments.blade.php b/resources/views/portal/ninja2020/layout/payments.blade.php index 8460f80612bd..34c0e9df2527 100644 --- a/resources/views/portal/ninja2020/layout/payments.blade.php +++ b/resources/views/portal/ninja2020/layout/payments.blade.php @@ -11,7 +11,7 @@ @endpush @section('body') - @livewire('required-client-info', ['gateway' => $gateway]) + @livewire('required-client-info', ['gateway' => $gateway, 'contact' => auth('contact')->user()])