[pricelist] modify ingredients without saving

This commit is contained in:
Tim Gröger 2021-04-10 23:05:03 +02:00
parent 459a9fea08
commit 8c321fb8ca
5 changed files with 51 additions and 26 deletions

View File

@ -72,7 +72,11 @@
</div> </div>
</div> </div>
<div class="q-pa-sm"> <div class="q-pa-sm">
<ingredients v-model="volume.ingredients" :editable="editable" /> <ingredients
v-model="volume.ingredients"
:editable="editable"
@update="updateVolume(volume)"
/>
</div> </div>
</q-carousel-slide> </q-carousel-slide>
</q-carousel> </q-carousel>
@ -80,10 +84,10 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { computed, defineComponent, PropType, ref, onBeforeMount } from 'vue'; import { computed, defineComponent, PropType, ref, onBeforeMount, unref } from 'vue';
import { DrinkPriceVolume } from 'src/plugins/pricelist/store'; import { DrinkPriceVolume } from 'src/plugins/pricelist/store';
import Ingredients from 'src/plugins/pricelist/components/CalculationTable/Ingredients.vue'; import Ingredients from 'src/plugins/pricelist/components/CalculationTable/Ingredients.vue';
import { calc_volume, clone } from '../../utils/utils';
export default defineComponent({ export default defineComponent({
name: 'DrinkPriceVolume', name: 'DrinkPriceVolume',
components: { Ingredients }, components: { Ingredients },
@ -102,7 +106,8 @@ export default defineComponent({
}, },
setup(props, { emit }) { setup(props, { emit }) {
onBeforeMount(() => { onBeforeMount(() => {
volumes.value = <Array<DrinkPriceVolume>>[...props.modelValue]; //volumes.value = <Array<DrinkPriceVolume>>JSON.parse(JSON.stringify(props.modelValue));
volumes.value = clone(props.modelValue);
}); });
const volumes = ref<Array<DrinkPriceVolume>>([]); const volumes = ref<Array<DrinkPriceVolume>>([]);
const _volume = ref<number | undefined>(); const _volume = ref<number | undefined>();
@ -126,8 +131,14 @@ export default defineComponent({
return retVal; return retVal;
}); });
function updateVolume() { function updateVolume(_volume: DrinkPriceVolume) {
emit('update:modelValue', volumes.value); console.log('updateVolume', _volume);
const index = volumes.value.findIndex((a) => a.id === _volume.id);
if (index > -1) {
console.log('updateVolume old', volumes.value[index]);
volumes.value[index].volume = calc_volume(_volume);
}
emit('update:modelValue', unref(volumes));
} }
return { volumes, volume, options, updateVolume }; return { volumes, volume, options, updateVolume };

View File

@ -147,8 +147,9 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { computed, defineComponent, PropType, ref, onBeforeMount } from 'vue'; import { computed, defineComponent, PropType, ref, onBeforeMount, unref } from 'vue';
import { usePricelistStore } from '../../store'; import { usePricelistStore } from '../../store';
import { clone } from '../../utils/utils';
export default defineComponent({ export default defineComponent({
name: 'Ingredients', name: 'Ingredients',
@ -164,14 +165,13 @@ export default defineComponent({
}, },
emits: { emits: {
'update:modelValue': (val: Array<FG.Ingredient>) => val, 'update:modelValue': (val: Array<FG.Ingredient>) => val,
update: () => true,
}, },
setup(props, { emit }) { setup(props, { emit }) {
onBeforeMount(() => { onBeforeMount(() => {
edit_ingredients.value = []; //edit_ingredients.value = <Array<FG.Ingredient>>JSON.parse(JSON.stringify(props.modelValue))
props.modelValue.forEach((a) => { //edit_ingredients.value = props.modelValue
edit_ingredients.value.push(Object.assign({}, a)); edit_ingredients.value = clone(props.modelValue);
});
console.log(edit_ingredients);
}); });
const store = usePricelistStore(); const store = usePricelistStore();
@ -199,13 +199,15 @@ export default defineComponent({
}; };
} }
edit_ingredients.value.push(_ingredient); edit_ingredients.value.push(_ingredient);
emit('update:modelValue', edit_ingredients.value); emit('update:modelValue', unref(edit_ingredients));
update();
cancelAddIngredient(); cancelAddIngredient();
} }
function updateValue(value: number, initValue: number) { function updateValue(value: number, initValue: number) {
console.log('updateValue', value, initValue); console.log('updateValue', value, initValue);
emit('update:modelValue', edit_ingredients.value); emit('update:modelValue', unref(edit_ingredients));
update();
} }
function cancelAddIngredient() { function cancelAddIngredient() {
@ -218,7 +220,8 @@ export default defineComponent({
if (index > -1) { if (index > -1) {
edit_ingredients.value.splice(index, 1); edit_ingredients.value.splice(index, 1);
} }
emit('update:modelValue', edit_ingredients.value); emit('update:modelValue', unref(edit_ingredients));
update();
} }
const drinks = computed(() => const drinks = computed(() =>
store.drinks.filter((drink) => { store.drinks.filter((drink) => {
@ -232,6 +235,12 @@ export default defineComponent({
return store.drinks.find((a) => a.id === id)?.name; return store.drinks.find((a) => a.id === id)?.name;
} }
function update() {
setTimeout(() => {
emit('update');
}, 500);
}
return { return {
addIngredient, addIngredient,
drinks, drinks,

View File

@ -47,7 +47,11 @@
</div> </div>
</q-card-section> </q-card-section>
<q-card-section> <q-card-section>
<drink-price-volumes v-model="edit_drink.volumes" editable /> <drink-price-volumes
v-model="edit_drink.volumes"
editable
@update:modelValue="updateVolume"
/>
</q-card-section> </q-card-section>
<q-card-actions class="justify-around"> <q-card-actions class="justify-around">
<q-btn label="Abbrechen" @click="cancel" /> <q-btn label="Abbrechen" @click="cancel" />
@ -57,9 +61,9 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { defineComponent, PropType, ref, onBeforeMount } from 'vue'; import { defineComponent, PropType, ref, onBeforeMount } from 'vue';
import { Drink, DrinkPriceVolume } from '../store'; import { Drink } from '../store';
import DrinkPriceVolumes from './CalculationTable/DrinkPriceVolumes.vue'; import DrinkPriceVolumes from './CalculationTable/DrinkPriceVolumes.vue';
import { calc_volume } from 'src/plugins/pricelist/utils/utils'; import { clone } from '../utils/utils';
export default defineComponent({ export default defineComponent({
name: 'DrinkModify', name: 'DrinkModify',
components: { DrinkPriceVolumes }, components: { DrinkPriceVolumes },
@ -72,8 +76,8 @@ export default defineComponent({
emits: { save: () => true, cancel: () => true }, emits: { save: () => true, cancel: () => true },
setup(props, { emit }) { setup(props, { emit }) {
onBeforeMount(() => { onBeforeMount(() => {
edit_drink.value = <Drink>JSON.parse(JSON.stringify(props.drink)); //edit_drink.value = <Drink>JSON.parse(JSON.stringify(props.drink));
console.log('edit_drink', edit_drink.value) edit_drink.value = clone(props.drink);
}); });
const edit_drink = ref<Drink>(); const edit_drink = ref<Drink>();
@ -83,11 +87,8 @@ export default defineComponent({
function cancel() { function cancel() {
emit('cancel'); emit('cancel');
} }
function updateVolume(volume: DrinkPriceVolume) { function updateVolume(test: Drink) {
if (volume) { console.log(test);
console.log('edit_volume', volume);
volume.volume = calc_volume(volume);
}
} }
return { edit_drink, save, cancel, updateVolume }; return { edit_drink, save, cancel, updateVolume };
}, },

View File

View File

@ -68,4 +68,8 @@ function calc_min_prices(
return retVal; return retVal;
} }
export { calc_volume, calc_cost_per_volume, calc_all_min_prices, calc_min_prices }; function clone<T>(o: T): T {
return <T>JSON.parse(JSON.stringify(o));
}
export { calc_volume, calc_cost_per_volume, calc_all_min_prices, calc_min_prices, clone };