composition: forcebuyFor

This commit is contained in:
Matthias 2022-04-16 20:02:30 +02:00
parent 50ee60042a
commit e91144fdaf

View File

@ -79,99 +79,98 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import { namespace } from 'vuex-class';
import { BotState, ForceEnterPayload, OrderSides } from '@/types';
import { BotStoreGetters } from '@/store/modules/ftbot'; import { BotStoreGetters } from '@/store/modules/ftbot';
import StoreModules from '@/store/storeSubModules'; import StoreModules from '@/store/storeSubModules';
import { ForceEnterPayload, OrderSides } from '@/types';
const ftbot = namespace(StoreModules.ftbot); import { defineComponent, ref, nextTick } from '@vue/composition-api';
import { useNamespacedActions, useNamespacedGetters } from 'vuex-composition-helpers';
@Component({}) export default defineComponent({
export default class ForceBuyForm extends Vue { name: 'ForceBuyForm',
pair = ''; setup(_, { root }) {
const { botState, shortAllowed, botApiVersion, stakeCurrency } = useNamespacedGetters(
StoreModules.ftbot,
[
BotStoreGetters.botState,
BotStoreGetters.shortAllowed,
BotStoreGetters.botApiVersion,
BotStoreGetters.stakeCurrency,
],
);
const { forcebuy } = useNamespacedActions(StoreModules.ftbot, ['forcebuy']);
const form = ref<HTMLFormElement>();
const pair = ref('');
const price = ref<number | null>(null);
const stakeAmount = ref<number | null>(null);
const ordertype = ref('');
const orderSide = ref<OrderSides>(OrderSides.long);
price = null; const checkFormValidity = () => {
const valid = form.value?.checkValidity();
stakeAmount = null;
ordertype?: string = '';
orderSide: OrderSides = OrderSides.long;
$refs!: {
form: HTMLFormElement;
};
@ftbot.Getter [BotStoreGetters.botState]?: BotState;
@ftbot.Getter [BotStoreGetters.shortAllowed]?: boolean;
@ftbot.Getter [BotStoreGetters.botApiVersion]: number;
@ftbot.Getter [BotStoreGetters.stakeCurrency]!: string;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@ftbot.Action forcebuy!: (payload: ForceEnterPayload) => Promise<string>;
created() {
this.$bvModal.show('forcebuy-modal');
}
close() {
this.$emit('close');
}
checkFormValidity() {
const valid = this.$refs.form.checkValidity();
return valid; return valid;
} };
handleBuy(bvModalEvt) { const handleSubmit = () => {
// Prevent modal from closing
bvModalEvt.preventDefault();
// Trigger submit handler
this.handleSubmit();
}
resetForm() {
console.log('resetForm');
this.pair = '';
this.price = null;
this.stakeAmount = null;
if (this.botApiVersion > 1.1) {
this.ordertype =
this.botState?.order_types?.forcebuy ||
this.botState?.order_types?.forceentry ||
this.botState?.order_types?.buy ||
this.botState?.order_types?.entry;
}
}
handleSubmit() {
// Exit when the form isn't valid // Exit when the form isn't valid
if (!this.checkFormValidity()) { if (!checkFormValidity()) {
return; return;
} }
// call forcebuy // call forcebuy
const payload: ForceEnterPayload = { pair: this.pair }; const payload: ForceEnterPayload = { pair: pair.value };
if (this.price) { if (price.value) {
payload.price = Number(this.price); payload.price = Number(price.value);
} }
if (this.ordertype) { if (ordertype.value) {
payload.ordertype = this.ordertype; payload.ordertype = ordertype.value;
} }
if (this.stakeAmount) { if (stakeAmount.value) {
payload.stakeamount = this.stakeAmount; payload.stakeamount = stakeAmount.value;
} }
if (this.botApiVersion >= 2.13) { if (botApiVersion.value >= 2.13) {
payload.side = this.orderSide; payload.side = orderSide.value;
} }
this.forcebuy(payload); forcebuy(payload);
this.$nextTick(() => { nextTick(() => {
this.$bvModal.hide('forcebuy-modal'); root.$bvModal.hide('forcebuy-modal');
}); });
};
const resetForm = () => {
console.log('resetForm');
pair.value = '';
price.value = null;
stakeAmount.value = null;
if (botApiVersion.value > 1.1) {
ordertype.value =
botState.value?.order_types?.forcebuy ||
botState.value?.order_types?.force_entry ||
botState.value?.order_types?.buy ||
botState.value?.order_types?.entry;
} }
} };
const handleBuy = (bvModalEvt) => {
// Prevent modal from closing
bvModalEvt.preventDefault();
// Trigger submit handler
handleSubmit();
};
return {
handleSubmit,
botState,
shortAllowed,
botApiVersion,
stakeCurrency,
form,
handleBuy,
resetForm,
pair,
price,
stakeAmount,
ordertype,
orderSide,
};
},
});
</script> </script>