makes selector work

This commit is contained in:
2019-10-03 21:27:42 +02:00
parent d6fe5b71f5
commit c99a38a738
2 changed files with 40 additions and 53 deletions

View File

@@ -12,7 +12,7 @@
<p v-if="!is_selling">Vendre</p> <p v-if="!is_selling">Vendre</p>
<p v-else>{{ selected_items.length > 0 ? `${totalSelectedValue} po` : 'Annuler' }}</p> <p v-else>{{ selected_items.length > 0 ? `${totalSelectedValue} po` : 'Annuler' }}</p>
</button> </button>
<PercentInput v-show="is_selling" v-model="modifiers['global']"></PercentInput> <PercentInput v-show="is_selling" v-model="global_mod"></PercentInput>
</div> </div>
<div v-else-if="perms.canBuy"> <div v-else-if="perms.canBuy">
<button class="button is-danger is-fullwidth" <button class="button is-danger is-fullwidth"
@@ -36,7 +36,7 @@
@claim="(data) => $emit('claim', data)" @claim="(data) => $emit('claim', data)"
@unclaim="(data) => $emit('unclaim', data)"> @unclaim="(data) => $emit('unclaim', data)">
</Request> </Request>
<Selector :id="`select-${idx}`" v-else-if="showSelectors" v-model="selected_items"></Selector> <Selector :id="item.id" v-else-if="showSelectors" v-model="selected_items"></Selector>
</td> </td>
</tr> </tr>
</template> </template>
@@ -76,19 +76,12 @@
return { return {
is_selling: false, is_selling: false,
selected_items: [], selected_items: [],
modifiers: {}, global_mod: 0,
}; };
}, },
methods: { methods: {
buySelectedItems () { buySelectedItems () {
const items = this.items.filter(i => this.selected_items.includes(i.id)); this.$emit("buy", this.selected_items);
var payload = [];
for (var idx in items) {
var item = items[idx];
var mod = this._modifierForItem(item.id);
payload.push([item.id, mod]);
}
this.$emit("buy", payload);
this.selected_items = []; this.selected_items = [];
}, },
sellSelectedItems () { sellSelectedItems () {
@@ -97,41 +90,27 @@
} else { } else {
this.is_selling = false; this.is_selling = false;
if (this.selected_items.length > 0) { if (this.selected_items.length > 0) {
const items = this.items.filter(i => this.selected_items.includes(i.id)); this.$emit("sell", this.selected_items);
var payload = [];
items.forEach(item => {
const mod = this._modifierForItem(item.id);
payload.push([item.id, mod]);
});
this.$emit("sell", payload);
this.selected_items = []; this.selected_items = [];
} }
} }
}, },
// TODO: fix this. It breaks reactivity and it's lazy
_modifierForItem (itemId) {
if (!(itemId in this.modifiers)) {
this.modifiers[itemId] = 0;
}
return 1 + this.modifiers[itemId] / 100;
}
}, },
computed: { computed: {
showSelectors () { return !this.perms.canGrab && (this.is_selling || this.perms.canBuy); }, showSelectors () { return !this.perms.canGrab && (this.is_selling || this.perms.canBuy); },
totalSelectedValue () { totalSelectedValue () {
const global_mod = this._modifierForItem("global"); var total = this.selected_items
var total = this.items .map(([id, mod]) => {
.filter(item => this.selected_items.includes(item.id)) const item = this.items.find(item => item.id == id);
.map(item => {
const mod = this._modifierForItem(item.id);
var price = item.base_price * mod; var price = item.base_price * mod;
if (this.is_selling) { if (this.is_selling) {
price = price / 2; price = price / 2;
} }
console.log(item, price, mod);
return price; return price;
}) })
.reduce((total,value) => total + value, 0); .reduce((total,value) => total + value, 0);
return global_mod * total; return (1 + this.global_mod / 100) * total;
}, },
}, },
} }

View File

@@ -3,47 +3,55 @@
<label class="button is-fullwidth"> <label class="button is-fullwidth">
<input type="checkbox" class="checkbox" v-model="selected"> <input type="checkbox" class="checkbox" v-model="selected">
</label> </label>
<PercentInput v-model="modifier"></PercentInput> <PercentInput v-model.number="mod_value"></PercentInput>
</div> </div>
</template> </template>
<script> <script>
import PercentInput from './PercentInput.vue' import PercentInput from './PercentInput.vue'
/* /* Selector for a specific item, with an associated price modifier.
Selector for a specific item, with an associated price modifier.
Acts as checkbox on a v-model, except it populates an array with [value, modifier] instead of value alone Acts as checkbox on a v-model, except it populates an array with [value, modifier] instead of value alone
*/ */
export default { export default {
props: ["value"], props: ["id", "value"],
components: { PercentInput }, components: { PercentInput },
data () { data () {
return { return {
selected: false, selected: false,
modifier: 0, mod_value: 0,
}; };
}, },
methods: { computed: {
update () { modifier () {
return 1 + this.mod_value / 100;
}
},
watch: {
selected (newState) {
let idx = this._findData();
var updated = this.value; var updated = this.value;
let idx = this.value.findIndex(([val, mod]) => this.value == val); if (newState == true && idx == -1) {
if (idx == -1) { updated.push([this.id, this.modifier]);
if this.selected { } else if (newState == false && idx != -1 ) {
updated.push([this.value, this.modifier]) updated.splice(idx, 1);
}
} else {
if this.selected {
updated.splice(idx, 1, [this.value, this.modifier])
// Replace this.value[idx] by [val, this.modifier]
} else {
// Remove this.value[idx]
}
} }
this.$emit('input', updated); this.$emit('input', updated);
}, },
mod_value (newState) {
let idx = this._findData();
var updated = this.value;
if (idx != -1) {
updated.splice(idx, 1, [this.id, this.modifier]);
this.$emit('input', updated);
}
}
},
methods: {
_findData () {
return this.value.findIndex(([val,mod]) => this.id == val);
}
} }
} }
</script> </script>