Will πŸ‡¬πŸ‡Ή
Will πŸ‡¬πŸ‡Ή
FFilament
Created by Will πŸ‡¬πŸ‡Ή on 11/16/2024 in #β“β”Šhelp
Best practice for multiple dynamic properties
Looking for advice for the following scenario:
Select::make('product_id')
->options(Product::all()->pluck('name', 'id')),
TextInput::make('quantity')
->numeric()
->step(1)
->maxValue(function(Get $get) {
$productId = $get('product_id');
if (!$productId) {
return;
}

return Product::find($productId)->quanity;
})
->helperText(function(Get $get {
// Basically the same logic as maxValue
return "Available: $stock";
}),
Select::make('product_id')
->options(Product::all()->pluck('name', 'id')),
TextInput::make('quantity')
->numeric()
->step(1)
->maxValue(function(Get $get) {
$productId = $get('product_id');
if (!$productId) {
return;
}

return Product::find($productId)->quanity;
})
->helperText(function(Get $get {
// Basically the same logic as maxValue
return "Available: $stock";
}),
My form also has several places where I will be using the Product data. Do I really need to execute the Product::find($productId) on every dynamic property? Example: After the TextInput::make('quantity') is ->live(onBlur: true) I have another field that will show the Total which is quantity * $product->price . Calling Product::find... for the 3rd time seems a bit much. is there a more efficient approach that I might be not considering, cause calling the Product model every time doesn't seem efficient. Thanks in advance for the advice
4 replies
FFilament
Created by Will πŸ‡¬πŸ‡Ή on 6/27/2024 in #β“β”Šhelp
Call async JavaScript function from a Action
I have a TextInput that has a hintAction to copy a code to an NFC tag using Chrome's NDEFReader
TextInput::make('nfc_code')
->hintAction(
Action::make('writeNfc')
->icon(getTeamIcon('microchip'))
->requiresConfirmation()
->action(function ($state, $livewire) {
$livewire->js("await nfcHandler('$state')");
})
),
TextInput::make('nfc_code')
->hintAction(
Action::make('writeNfc')
->icon(getTeamIcon('microchip'))
->requiresConfirmation()
->action(function ($state, $livewire) {
$livewire->js("await nfcHandler('$state')");
})
),
JS function
window.nfcHandler = function(code) {
if ('NDEFReader' in window) {
const url = `${window.location.origin}/animals/${code}/tasks`;

const ndef = new NDEFReader();
ndef.write({
records: [{ recordType: "url", data: url }]
}).then(() => {
new FilamentNotification()
.title('Saved successfully')
.success()
.send()
}).catch(error => {
new FilamentNotification()
.title(error.toString())
.danger()
.send()
});
} else {
// for testing
setTimeout(() => {
new FilamentNotification()
.title('NDEFReader not available')
.danger()
.send()

}, 5000);
}
}
window.nfcHandler = function(code) {
if ('NDEFReader' in window) {
const url = `${window.location.origin}/animals/${code}/tasks`;

const ndef = new NDEFReader();
ndef.write({
records: [{ recordType: "url", data: url }]
}).then(() => {
new FilamentNotification()
.title('Saved successfully')
.success()
.send()
}).catch(error => {
new FilamentNotification()
.title(error.toString())
.danger()
.send()
});
} else {
// for testing
setTimeout(() => {
new FilamentNotification()
.title('NDEFReader not available')
.danger()
.send()

}, 5000);
}
}
The JS function works, calling the JS functions from Action works. However, the modal doesn't wait for the JS function, it closes as soon as I confirm on the modal. Is there a way to "wait" for the JS function to finish before closing the modal? Any guidance will be greatly appreciate it
3 replies
FFilament
Created by Will πŸ‡¬πŸ‡Ή on 4/8/2024 in #β“β”Šhelp
How to get team_id when creating new record from Relation Manager
I have a Relation Manager ConversionsRelationManager I'm trying to create new record from there but I keep getting team_id doesn't have a default value. I have the normal resource UnitConversionResource and I'm able to create new records there with the team_id being passed correctly
5 replies