Action on change of input

I'd like to execute a custom action (like some code) when I toggle one of those two values; is this possible?
17 Replies
Dennis Koch
Dennis Koch2y ago
->reactive()->afterStateUpdated()
Robin
RobinOP2y ago
Ohh, and how can I access the current model then?
Dennis Koch
Dennis Koch2y ago
Current model or data?
Robin
RobinOP2y ago
->afterStateUpdated(function ($value, $record) {
->afterStateUpdated(function ($value, $record) {
So like here:
->formatStateUsing(function ($state, $record)
->formatStateUsing(function ($state, $record)
Dennis Koch
Dennis Koch2y ago
Was that a question?
Robin
RobinOP2y ago
Well, the second parameter is that also the record then here?
Dennis Koch
Dennis Koch2y ago
It's the record, but not the current data. You can use $get() for that
Robin
RobinOP2y ago
Like this then?
->afterStateUpdated(function (Closure $value) {
doSomething($value, $get());
}),
->afterStateUpdated(function (Closure $value) {
doSomething($value, $get());
}),
Because I need the whole resource model and the $get() is undefined
Dennis Koch
Dennis Koch2y ago
Do you need the model or the updated data?
Robin
RobinOP2y ago
I need the data from the switch or the selectbox (depending on what the user changes) But I need the model (with old data is OK) too
Dennis Koch
Dennis Koch2y ago
So ->formatStateUsing(function ($state, $record) should work on an EditPage For a EditPage you likely need ->formatStateUsing(function ($state, $livewire) { $livewire->getState()
Robin
RobinOP2y ago
Ah, it's in the resource itself (Class that extends Resource)
Dennis Koch
Dennis Koch2y ago
What do you mean?
Robin
RobinOP2y ago
Dennis Koch
Dennis Koch2y ago
Still no idea what you mean. Also there is no $resource I think
Robin
RobinOP2y ago
So, after that I toggle a switch in the interface on the edit page, I want to execute an action. This action needs the value of the switch (on/off, e.g. true and false) but also the current resource
->afterStateUpdated(function ($state, $livewire) {
$command = new stdClass();
$command->action = 'TOGGLE_P1';
$command->turn_on = (bool) $state;
(new CommunicationService(null, $livewire->getState()))->sendRawCommand($command);
}),
->afterStateUpdated(function ($state, $livewire) {
$command = new stdClass();
$command->action = 'TOGGLE_P1';
$command->turn_on = (bool) $state;
(new CommunicationService(null, $livewire->getState()))->sendRawCommand($command);
}),
Livewire ->getstate() throws an exception; Method App\Filament\Resources\DeviceResource\Pages\EditDevice::getState does not exist. Complete toggle component;
Toggle::make('energy_meter')->label('P1 reader')
->onIcon('heroicon-s-lightning-bolt')
->offIcon('heroicon-s-lightning-bolt')
->onColor('success')
->offColor('disabled')
->reactive()
->afterStateUpdated(function ($state, $livewire) {
$command = new stdClass();
$command->action = 'TOGGLE_P1';
$command->turn_on = (bool) $state;
(new CommunicationService(null, $livewire->getState()))->sendRawCommand($command);
}),
Toggle::make('energy_meter')->label('P1 reader')
->onIcon('heroicon-s-lightning-bolt')
->offIcon('heroicon-s-lightning-bolt')
->onColor('success')
->offColor('disabled')
->reactive()
->afterStateUpdated(function ($state, $livewire) {
$command = new stdClass();
$command->action = 'TOGGLE_P1';
$command->turn_on = (bool) $state;
(new CommunicationService(null, $livewire->getState()))->sendRawCommand($command);
}),
Solution was:
Toggle::make('energy_meter')->label('P1 reader')
->onIcon('heroicon-s-lightning-bolt')
->offIcon('heroicon-s-lightning-bolt')
->onColor('success')
->offColor('disabled')
->reactive()
->afterStateUpdated(function (?Model $record, $state) {
$command = new stdClass();
$command->action = 'TOGGLE_P1';
$command->turn_on = (bool) $state;
(new CommunicationService(null, $record))->sendRawCommand($command);
}),
Toggle::make('energy_meter')->label('P1 reader')
->onIcon('heroicon-s-lightning-bolt')
->offIcon('heroicon-s-lightning-bolt')
->onColor('success')
->offColor('disabled')
->reactive()
->afterStateUpdated(function (?Model $record, $state) {
$command = new stdClass();
$command->action = 'TOGGLE_P1';
$command->turn_on = (bool) $state;
(new CommunicationService(null, $record))->sendRawCommand($command);
}),
Can be closed, thanks for the help!
Want results from more Discord servers?
Add your server