Skip to content

Instantly share code, notes, and snippets.

@roni-estein
Created April 23, 2020 02:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save roni-estein/e1399318729b660b13df33b7eb57f5ac to your computer and use it in GitHub Desktop.
Livewire form, glitching on selects
<?php
namespace App\Http\Livewire;
use App\Events\RefillRequested;
use Livewire\Component;
class RefillComponent extends Component
{
public $name = '';
public $phone = '';
public $rxs = [
0 => '',
];
public $callback = 'asap';
public $callbackTiming = [
'asap' => 'Call me as soon as possible',
'soon' => 'Call me between 15 - 30 min',
'after30' => 'Call me between 30 min - 1 hour',
'afternoon' => 'Call me between this afternoon',
'evening' => 'Call me this evening',
];
public $timingMessage = '';
public $pickup = 0;
public $pickupOptions = [
'store' => [
'text'=> 'Pick up in the Pharmacy',
'timing' => 'This can be done, same day or the following day depending on our stock and what time you place this refill',
],
'parking' => [
'text'=> 'Have someone bring it out to the parking lot',
'timing' => 'This can be done, same day or the following day depending on our stock and what time you place this refill',
'explanation' => 'If you are at risk, or even if you just want to social distance,
this will let you wait in your car, instead of in the pharmacy.
We will walk it out to your car when it&quot;s ready.',
],
'home' => [
'text'=> 'Deliver it to my home',
'timing' => 'Unless this is done before 11, this most often occurs the next day'
],
];
public $showConfirmation = false;
public function render()
{
return view('livewire.refill-component');
}
public function updatedRxs()
{
//Rekey the rxs, remove empty rows, add a new field to the end
$this->rxs = collect($this->rxs)
->reject(function($value,$key){
return empty(trim($value));
})
->push('')
->values()
->toArray();
$this->checkValidation();
}
public function updatedPickup()
{
$this->timingMessage = $this->pickupOptions[$this->pickup]['timing'];
}
public function openConfirmation()
{
$this->checkValidation();
$this->showConfirmation = true;
}
public function closeConfirmation()
{
$this->showConfirmation = false;
}
public function createRefillRequest()
{
$this->closeConfirmation();
event(new RefillRequested([
'name' => $this->name,
'phone' => $this->phone,
'callback' => $this->callback,
'pickup' => $this->pickup,
'rxs' => $this->rxs,
]));
}
public function checkValidation()
{
$this->validate([
'name' => 'required',
'phone' => 'required',
'rxs.0' => 'required',
'pickup' => 'alpha'
],[
'rxs.0.required' => 'You must have at least one prescription to refill',
'pickup.alpha' => 'Please choose how you would like to get your medication',
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment