Preserve existing origin entries when switching recipe version
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:
Malek TellissiandClaude Sonnet 4.6 committed 2026-09-12 20:20:07 +02:00
1 parent 769d91465f
commit e9734c2590
2 files changed
+12 -10

No files matched your search

@@ -83,17 +83,18 @@ class BatchResource extends Resource
)
->required()
->live()
->afterStateUpdated(function ($state, $set) {
->afterStateUpdated(function ($state, $set, $get) {
if (! $state) {
$set('origins', []);
return;
}
$recipe = Recipe::with('spices')->find($state);
if ($recipe) {
$set('origins', $recipe->spices->map(fn ($s) => [
'spice_id' => $s->id,
'entries' => [['supplier_id' => null, 'origin_country' => null, 'farmer_id' => null, 'notes' => null]],
])->toArray());
$existing = collect($get('origins') ?? [])->keyBy('spice_id');
$set('origins', $recipe->spices->map(fn ($s) => $existing->has($s->id)
? $existing[$s->id]
: ['spice_id' => $s->id, 'entries' => [['supplier_id' => null, 'origin_country' => null, 'farmer_id' => null, 'notes' => null]]]
)->toArray());
}
}),
@@ -50,7 +50,7 @@ class BatchesRelationManager extends RelationManager
->default(fn () => $this->getOwnerRecord()->recipes_id)
->required()
->live()
->afterStateUpdated(function ($state, $set) {
->afterStateUpdated(function ($state, $set, $get) {
if (! $state) {
$set('origins', []);
return;
@@ -59,10 +59,11 @@ class BatchesRelationManager extends RelationManager
if (! $recipe) {
return;
}
$set('origins', $recipe->spices->map(fn ($s) => [
'spice_id' => $s->id,
'entries' => [['supplier_id' => null, 'origin_country' => null, 'farmer_id' => null, 'notes' => null]],
])->toArray());
$existing = collect($get('origins') ?? [])->keyBy('spice_id');
$set('origins', $recipe->spices->map(fn ($s) => $existing->has($s->id)
? $existing[$s->id]
: ['spice_id' => $s->id, 'entries' => [['supplier_id' => null, 'origin_country' => null, 'farmer_id' => null, 'notes' => null]]]
)->toArray());
}),
Forms\Components\TextInput::make('units_produced')