Skip to content

Commit aca145f

Browse files
authored
Merge pull request #75 from stepanzh/74-bug-when-there-are-some-ingredients-and-user-updates-list-the-scaled-list-is-not-updated
Fix bug when there are some ingredients and user updates list the scaled list is not updated #74
2 parents 9ce5373 + 52e80c6 commit aca145f

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

src/stores/proportioCalculator.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export const useProportioCalculatorStore = defineStore('proportio-calculator', (
1212

1313
const numberOfIngredients = computed(() => ingredients.value.length)
1414

15+
const scaleFactor = ref(NaN)
16+
1517
function emptyIngredient() {
1618
const ingr = reactive({
1719
id: crypto.randomUUID(),
@@ -22,7 +24,15 @@ export const useProportioCalculatorStore = defineStore('proportio-calculator', (
2224
})
2325

2426
ingr.stopWatchingScaledAmount = watch(() => ingr.scaledAmount, () => { onScaleAmountChanged(ingr.id) })
25-
watch(() => ingr.originalAmount, () => { ingr.scaledAmount = NaN })
27+
watch(() => ingr.originalAmount, () => {
28+
// Note. When there are some components and scale factor, update ingr.scaledAmount.
29+
// It is needed when user backed from scaled mode and add new ingredient.
30+
if (scaleFactor.value && scaleFactor.value !== NaN) {
31+
updateScaleAmount(ingr, scaleFactor.value)
32+
} else {
33+
ingr.scaledAmount = NaN
34+
}
35+
})
2636

2737
ingr.displayedName = computed(() => ingr.name === '' ? '<Без названия>' : ingr.name )
2838

@@ -46,16 +56,19 @@ export const useProportioCalculatorStore = defineStore('proportio-calculator', (
4656
}
4757

4858
ingredients.value.push(ingr)
49-
50-
// TODO: When there are some components and scale factor, update ingr.scaledAmount
5159
}
5260

5361
function remove(id) {
54-
ingredients.value = ingredients.value.filter((x) => x.id != id)
62+
if (ingredients.value.length == 1) {
63+
clear()
64+
} else {
65+
ingredients.value = ingredients.value.filter((x) => x.id != id)
66+
}
5567
}
5668

5769
function clear() {
5870
ingredients.value = []
71+
scaleFactor.value = NaN
5972
}
6073

6174
// Moves ingredient with `id` up, if possible
@@ -98,22 +111,26 @@ export const useProportioCalculatorStore = defineStore('proportio-calculator', (
98111
const changedIngr = ingredients.value.find((x) => x.id === forId)
99112

100113
// TODO: NaNs
101-
const scaleFactor = changedIngr.scaledAmount / changedIngr.originalAmount
102-
updateScaleAmounts(forId, scaleFactor)
114+
scaleFactor.value = changedIngr.scaledAmount / changedIngr.originalAmount
115+
updateScaleAmounts(forId, scaleFactor.value)
103116
}
104117

105118
function updateScaleAmounts(excludeId, scaleBy) {
106119
ingredients.value
107120
.filter((ingr) => ingr.id !== excludeId)
108121
.forEach((ingr) => {
109-
console.log(`${Date.now()} updateScaleAmounts: updating ${ingr.id}`)
110-
ingr.stopWatchingScaledAmount()
111-
ingr.scaledAmount = ingr.originalAmount * scaleBy
112-
// DRY
113-
ingr.stopWatchingScaledAmount = watch(() => ingr.scaledAmount, () => { onScaleAmountChanged(ingr.id) })
122+
updateScaleAmount(ingr, scaleBy)
114123
})
115124
}
116125

126+
function updateScaleAmount(ingr, scaleBy) {
127+
console.log(`${Date.now()} updateScaleAmounts: updating ${ingr.id}`)
128+
ingr.stopWatchingScaledAmount()
129+
ingr.scaledAmount = ingr.originalAmount * scaleBy
130+
// DRY
131+
ingr.stopWatchingScaledAmount = watch(() => ingr.scaledAmount, () => { onScaleAmountChanged(ingr.id) })
132+
}
133+
117134
function getRecipeAsPlainTextTabular() {
118135
if (numberOfIngredients > 0) {
119136
console.error('Невозможно создать рецепт в виде строки: рецепт пуст')
@@ -148,9 +165,6 @@ export const useProportioCalculatorStore = defineStore('proportio-calculator', (
148165
clear,
149166
moveTowardsFirstOnce,
150167
moveTowardsLastOnce,
151-
emptyIngredient,
152-
onScaleAmountChanged,
153-
updateScaleAmounts,
154168
getRecipeAsPlainTextTabular,
155169
}
156170
})

0 commit comments

Comments
 (0)