Skip to content

Instantly share code, notes, and snippets.

@ManojKiranA
Forked from awcodes/DropInAction.php
Created November 16, 2022 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ManojKiranA/36dbb1172ef1377246ea91c65da99828 to your computer and use it in GitHub Desktop.
Save ManojKiranA/36dbb1172ef1377246ea91c65da99828 to your computer and use it in GitHub Desktop.
<x-forms::field-wrapper
:id="$getId()"
:label="$getLabel()"
:label-sr-only="$isLabelHidden()"
:helper-text="$getHelperText()"
:hint="$getHint()"
:hint-icon="$getHintIcon()"
:required="$isRequired()"
:state-path="$getStatePath()"
class="{{ $isLabelHidden() && ! $hasInlineLabel() ? 'h-full flex items-end' : null }}"
>
<div class="{{ $isLabelHidden() && ! $hasInlineLabel() ? 'relative bottom-0' : null }}">
<x-forms::actions.action
:action="$getExecutableAction()"
component="forms::button"
>
@if (! $getExecutableAction()->isLabelHidden())
{{ $getExecutableAction()->getLabel() }}
@endif
</x-forms::actions.action>
</div>
</x-forms::field-wrapper>
<?php
namespace App\Forms\Components;
use Closure;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\Field;
class DropInAction extends Field
{
protected Action | Closure | null $action = null;
protected string $view = 'forms.components.drop-in-action';
protected function setUp(): void
{
$this->dehydrated(false);
}
public function execute(Action | Closure | null $action): static
{
$this->action = $action;
return $this;
}
public function getExecutableAction()
{
return $this->evaluate($this->action)?->component($this);
}
public function getActions(): array
{
$executableAction = $this->getExecutableAction();
return array_merge(
parent::getActions(),
$executableAction ? [$executableAction->getName() => $executableAction->component($this)] : [],
);
}
}
DropInAction::make('test')
->disableLabel()
->execute(function (Closure $get, Closure $set) {
return Action::make('geolocate')
->icon('heroicon-o-search')
->label('Geocode')
->action(function () use ($get, $set) {
if (blank($get('street')) || blank($get('city')) || blank($get('state')) || blank($get('zip'))) {
Filament::notify('danger', 'Please provide the location\'s complete address before geocoding.');
return;
}
$address = "{$get('street')} {$get('unit')} {$get('city')} {$get('state')} {$get('zip')}";
try {
$mapsData = Http::get(config('services.google_maps.url') . '?key=' . config('services.google_maps.key') . '&address=' . $address . '&sensor=true')
->throw()
->json();
} catch (RequestException $e) {
Filament::notify('danger', 'Unable to geocode for this address.');
return;
}
$set('latitude', $mapsData['results'][0]['geometry']['location']['lat'] ?? $get('latitude'));
$set('longitude', $mapsData['results'][0]['geometry']['location']['lng'] ?? $get('longitude'));
});
}),
DropInAction::make('birdeye_data')
->label('Remote')
->execute(
Action::make('birdeye_exists')
->label('View Data')
->color('secondary')
->action('showBirdeyeData')
->modalHeading(function ($livewire) {
return 'Birdeye data for Location ' . $livewire->record->number;
})
->modalActions(fn ($action): array => [$action->getModalCancelAction()->label('OK')])
->modalWidth('screen')
->modalContent(function ($livewire) {
return View::make('forms.components.birdeye-data', ['data' => (new BirdeyeService($livewire->record))->show(), 'id' => $livewire->id]);
})
->extraAttributes(['class' => 'w-full']),
),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment