22 lines
1.1 KiB
JavaScript
22 lines
1.1 KiB
JavaScript
import { setPlanningDay } from '#tofu/tools/planning/setPlanningDay.js'
|
|
|
|
// Reconciles a planning that just arrived from the store with what the user
|
|
// typed while it was loading. The loaded week is the base — dropping it would
|
|
// erase the days already stored — and only the cells actually touched since
|
|
// the load started are re-applied on top of it.
|
|
//
|
|
// editedKeys holds "<dateKey>:<field>" entries, so editing Monday's dish never
|
|
// shields Monday's untouched ingredient line from the loaded value.
|
|
export function mergeEditedDays(loadedPlanning, localPlanning, editedKeys) {
|
|
if (!editedKeys || editedKeys.size === 0) return loadedPlanning
|
|
if (!loadedPlanning || !Array.isArray(localPlanning?.days)) return loadedPlanning
|
|
|
|
let merged = loadedPlanning
|
|
for (const day of localPlanning.days) {
|
|
const patch = {}
|
|
if (editedKeys.has(`${day.date}:dish`)) patch.dish = day.dish
|
|
if (editedKeys.has(`${day.date}:ingredients`)) patch.ingredients = day.ingredients
|
|
if (Object.keys(patch).length > 0) merged = setPlanningDay(merged, day.date, patch)
|
|
}
|
|
return merged
|
|
}
|