Preserve existing origin entries when switching recipe version
Deploy / deploy (push) Successful in 14s
Deploy / deploy (push) Successful in 14s
When the recipe is changed in a batch form, keep origin data for spices that exist in both the old and new recipe. Only add empty rows for spices that are new to the selected recipe version. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
769d91465f
commit
e9734c2590
2 files changed
+12
-10
No files matched your search
@@ -83,17 +83,18 @@ class BatchResource extends Resource
|
|||||||
)
|
)
|
||||||
->required()
|
->required()
|
||||||
->live()
|
->live()
|
||||||
->afterStateUpdated(function ($state, $set) {
|
->afterStateUpdated(function ($state, $set, $get) {
|
||||||
if (! $state) {
|
if (! $state) {
|
||||||
$set('origins', []);
|
$set('origins', []);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$recipe = Recipe::with('spices')->find($state);
|
$recipe = Recipe::with('spices')->find($state);
|
||||||
if ($recipe) {
|
if ($recipe) {
|
||||||
$set('origins', $recipe->spices->map(fn ($s) => [
|
$existing = collect($get('origins') ?? [])->keyBy('spice_id');
|
||||||
'spice_id' => $s->id,
|
$set('origins', $recipe->spices->map(fn ($s) => $existing->has($s->id)
|
||||||
'entries' => [['supplier_id' => null, 'origin_country' => null, 'farmer_id' => null, 'notes' => null]],
|
? $existing[$s->id]
|
||||||
])->toArray());
|
: ['spice_id' => $s->id, 'entries' => [['supplier_id' => null, 'origin_country' => null, 'farmer_id' => null, 'notes' => null]]]
|
||||||
|
)->toArray());
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
|||||||
+6
-5
@@ -50,7 +50,7 @@ class BatchesRelationManager extends RelationManager
|
|||||||
->default(fn () => $this->getOwnerRecord()->recipes_id)
|
->default(fn () => $this->getOwnerRecord()->recipes_id)
|
||||||
->required()
|
->required()
|
||||||
->live()
|
->live()
|
||||||
->afterStateUpdated(function ($state, $set) {
|
->afterStateUpdated(function ($state, $set, $get) {
|
||||||
if (! $state) {
|
if (! $state) {
|
||||||
$set('origins', []);
|
$set('origins', []);
|
||||||
return;
|
return;
|
||||||
@@ -59,10 +59,11 @@ class BatchesRelationManager extends RelationManager
|
|||||||
if (! $recipe) {
|
if (! $recipe) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$set('origins', $recipe->spices->map(fn ($s) => [
|
$existing = collect($get('origins') ?? [])->keyBy('spice_id');
|
||||||
'spice_id' => $s->id,
|
$set('origins', $recipe->spices->map(fn ($s) => $existing->has($s->id)
|
||||||
'entries' => [['supplier_id' => null, 'origin_country' => null, 'farmer_id' => null, 'notes' => null]],
|
? $existing[$s->id]
|
||||||
])->toArray());
|
: ['spice_id' => $s->id, 'entries' => [['supplier_id' => null, 'origin_country' => null, 'farmer_id' => null, 'notes' => null]]]
|
||||||
|
)->toArray());
|
||||||
}),
|
}),
|
||||||
|
|
||||||
Forms\Components\TextInput::make('units_produced')
|
Forms\Components\TextInput::make('units_produced')
|
||||||
|
|||||||
Reference in new issue
Block a user