Files
newfreeplanet_OLD/src/components/CSendCoins/CSendCoins.ts

151 lines
4.1 KiB
TypeScript
Raw Normal View History

2022-09-03 13:06:35 +02:00
import { computed, defineComponent, onMounted, PropType, ref, watch } from 'vue'
2022-09-12 18:36:54 +02:00
import { IAccount, ICircuit, IOperators, ISendCoin, ISpecialField, IUserFields } from '../../model'
2022-09-03 13:06:35 +02:00
import { tools } from '@store/Modules/tools'
2022-09-12 18:36:54 +02:00
import { CCurrencyValue } from '@/components/CCurrencyValue'
2022-09-03 13:06:35 +02:00
import { useUserStore } from '@store/UserStore'
import { useCircuitStore } from '@store/CircuitStore'
import { useQuasar } from 'quasar'
2022-09-12 18:36:54 +02:00
import { useI18n } from '@/boot/i18n'
2022-09-03 13:06:35 +02:00
export default defineComponent({
name: 'CSendCoins',
emits: ['close'],
props: {
showprop: {
type: Boolean,
default: false,
},
to_user: {
type: Object as PropType<IUserFields>,
required: true,
},
},
2022-09-12 18:36:54 +02:00
components: { CCurrencyValue },
2022-09-03 13:06:35 +02:00
setup(props, { emit }) {
const $q = useQuasar()
2022-09-12 18:36:54 +02:00
const { t } = useI18n()
2022-09-03 13:06:35 +02:00
const show = ref(false)
const userStore = useUserStore()
const circuitStore = useCircuitStore()
const from_username = ref(userStore.my.username)
const circuitsel = ref('')
const qty = ref(1)
const causal = ref('')
const bothcircuits = ref(<any>[])
2022-09-12 18:36:54 +02:00
const circuitloaded = ref(<ICircuit | undefined>undefined)
const circuitdest = ref(<ICircuit | undefined>undefined)
const accountloaded = ref(<IAccount | undefined>undefined)
const accountdest = ref(<IAccount | undefined>undefined)
const remainingCoins = ref(0)
2022-09-03 13:06:35 +02:00
2022-09-12 18:36:54 +02:00
const priceLabel = computed(() => circuitloaded.value ? `${qty.value} ` + circuitloaded.value.symbol : '')
const arrayMarkerLabel = ref(<any>[])
const qtyRef = ref(null)
2022-09-03 13:06:35 +02:00
watch(() => circuitsel.value, (newval, oldval) => {
aggiorna()
2022-09-03 13:06:35 +02:00
})
watch(() => props.showprop, (newval, oldval) => {
console.log('props.showprop', props.showprop, newval)
show.value = newval
})
function aggiorna() {
circuitloaded.value = circuitStore.listcircuits.find((rec: ICircuit) => rec.name === circuitsel.value)
2022-09-12 18:36:54 +02:00
if (circuitloaded.value) {
accountloaded.value = userStore.getAccountByCircuitId(circuitloaded.value._id)
// accountdest.value = userStore.getAccountByCircuitId(circuitloaded.value._id)
if (accountloaded.value) {
remainingCoins.value = tools.getRemainingCoinsToSend(accountloaded.value)
const quanti = [ ...Array(100).keys() ].map( i => i+1)
for (const ind of quanti) {
let value = ind * 10
if (value > remainingCoins.value) {
break
} else {
const label = value.toString()
arrayMarkerLabel.value.push({value, label})
}
}
}
}
console.log('circuitStore.listcircuits', circuitStore.listcircuits, 'aggiorna', circuitloaded.value)
2022-09-12 18:36:54 +02:00
console.log('userStore.my.profile.mycircuits', userStore.my.profile.mycircuits)
}
2022-09-03 13:06:35 +02:00
function mounted() {
// ....
bothcircuits.value = userStore.IsMyCircuitByUser(props.to_user)
if (bothcircuits.value.length === 1) {
circuitsel.value = bothcircuits.value[0]
}
aggiorna()
2022-09-03 13:06:35 +02:00
show.value = true
}
function hide() {
emit('close', true)
}
function sendCoin() {
console.log('sendcoin', qty.value, props.to_user.username)
if (props.to_user.username && qty.value && circuitloaded.value) {
const myrecsendcoin: ISendCoin = {
qty: qty.value,
dest: props.to_user.username,
circuitname: circuitsel.value,
causal: causal.value,
symbol: circuitloaded.value.symbol,
}
console.log('myrecsendcoin', myrecsendcoin)
if (circuitloaded.value) {
tools.sendCoinsByCircuit($q, circuitloaded.value, myrecsendcoin).then((ris: any) => {
if (ris) {
show.value = false
}
})
2022-09-03 13:06:35 +02:00
}
}
}
onMounted(mounted)
return {
2022-09-12 18:36:54 +02:00
t,
2022-09-03 13:06:35 +02:00
tools,
show,
bothcircuits,
from_username,
circuitsel,
circuitloaded,
2022-09-12 18:36:54 +02:00
accountloaded,
accountdest,
2022-09-03 13:06:35 +02:00
qty,
hide,
sendCoin,
causal,
2022-09-12 18:36:54 +02:00
priceLabel,
arrayMarkerLabel,
remainingCoins,
qtyRef,
2022-09-03 13:06:35 +02:00
}
},
})