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

273 lines
7.7 KiB
TypeScript
Raw Normal View History

2022-09-03 13:06:35 +02:00
import { computed, defineComponent, onMounted, PropType, ref, watch } from 'vue'
2023-01-03 16:51:45 +01:00
import { IAccount, ICircuit, IMyGroup, IOperators, ISendCoin, ISpecialField, IUserFields } from '../../model'
2022-09-03 13:06:35 +02:00
import { tools } from '@store/Modules/tools'
2022-09-14 11:31:48 +02:00
import { CSaldo } from '@/components/CSaldo'
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'
import { CMyUserOnlyView } from '@/components/CMyUserOnlyView'
2023-01-12 01:03:19 +01:00
import { CMyGroupOnlyView } from '@/components/CMyGroupOnlyView'
2022-09-13 12:28:33 +02:00
import { costanti } from '@costanti'
2022-09-03 13:06:35 +02:00
export default defineComponent({
name: 'CSendCoins',
emits: ['close'],
props: {
showprop: {
type: Boolean,
default: false,
},
circuitname: {
type: String,
default: ''
},
2023-01-12 01:03:19 +01:00
qtydefault: {
type: String,
required: false,
default: '1'
},
2022-09-03 13:06:35 +02:00
to_user: {
type: Object as PropType<IUserFields>,
2023-01-03 16:51:45 +01:00
required: false,
default: null
},
to_group: {
type: Object as PropType<IMyGroup>,
required: false,
default: null,
},
from_group: {
type: Object as PropType<IMyGroup>,
required: false,
default: null,
2022-09-03 13:06:35 +02:00
},
},
2023-01-12 01:03:19 +01:00
components: { CSaldo, CMyUserOnlyView, CMyGroupOnlyView },
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)
2023-01-12 01:03:19 +01:00
const from_groupname = ref('')
2022-09-03 13:06:35 +02:00
const circuitsel = ref('')
2023-01-03 16:51:45 +01:00
const qty = ref(<string | number>'')
2022-09-03 13:06:35 +02:00
const causal = ref('')
const bothcircuits = ref(<any>[])
2023-01-12 01:03:19 +01:00
const groupSel = ref(<IMyGroup | null | undefined>null)
2022-09-12 18:36:54 +02:00
const circuitloaded = ref(<ICircuit | undefined>undefined)
const circuitdest = ref(<ICircuit | undefined>undefined)
2023-01-12 01:03:19 +01:00
const accountloaded = ref(<IAccount | undefined | null>undefined)
2022-09-12 18:36:54 +02:00
const accountdest = ref(<IAccount | undefined>undefined)
const remainingCoins = ref(0)
2022-09-13 12:28:33 +02:00
const maxsendable = ref(0)
const numstep = ref(0)
const tipoConto = ref(costanti.AccountType.USER)
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>[])
2022-09-13 12:28:33 +02:00
const qtyRef = ref(<any>null)
const causalRef = ref(<any>null)
2022-09-03 13:06:35 +02:00
2023-01-12 01:03:19 +01:00
const groupsListAdmin = ref(<IMyGroup[]>[])
const arrGroupsList = ref(<any[]>[])
2022-09-03 13:06:35 +02:00
watch(() => circuitsel.value, (newval, oldval) => {
2022-09-14 11:31:48 +02:00
tools.setCookie(tools.CIRCUIT_USE, newval)
aggiorna()
2022-09-03 13:06:35 +02:00
})
2023-01-12 01:03:19 +01:00
watch(() => tipoConto.value, (newval, oldval) => {
aggiorna()
})
watch(() => from_groupname.value, (newval, oldval) => {
aggiorna()
})
watch(() => from_username.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() {
2023-01-12 01:03:19 +01:00
groupSel.value = null
accountloaded.value = null
circuitloaded.value = circuitStore.listcircuits.find((rec: ICircuit) => rec.name === circuitsel.value)
2022-09-12 18:36:54 +02:00
if (circuitloaded.value) {
2023-01-12 01:03:19 +01:00
if (tipoConto.value === costanti.AccountType.USER) {
accountloaded.value = userStore.getAccountByCircuitId(circuitloaded.value._id)
} else if (tipoConto.value === costanti.AccountType.COMMUNITY_ACCOUNT) {
groupSel.value = userStore.my.profile.manage_mygroups.find((group: IMyGroup) => from_groupname.value === group.groupname)
accountloaded.value = groupSel.value ? groupSel.value.account : null
}
groupsListAdmin.value = userStore.GroupsListWhereIAmAdmin()
arrGroupsList.value = []
for (const group of groupsListAdmin.value) {
arrGroupsList.value.push({ label: group.groupname, value: group.groupname });
}
2022-09-12 18:36:54 +02:00
// accountdest.value = userStore.getAccountByCircuitId(circuitloaded.value._id)
if (accountloaded.value) {
2022-09-13 12:28:33 +02:00
remainingCoins.value = circuitStore.getRemainingCoinsToSend(accountloaded.value)
if (accountloaded.value.saldo > 0) {
maxsendable.value = accountloaded.value.saldo + accountloaded.value.fidoConcesso
} else {
maxsendable.value = accountloaded.value.fidoConcesso
}
if (remainingCoins.value < 10) {
remainingCoins.value = 10
}
numstep.value = Math.round(maxsendable.value / 10)
if (numstep.value < 1) {
numstep.value = 1
}
const quanti = [...Array(20).keys()].map(i => i + 1)
2022-09-12 18:36:54 +02:00
for (const ind of quanti) {
let valuenorm = ind * numstep.value
2022-09-13 12:28:33 +02:00
let value = ind * numstep.value
2022-09-12 18:36:54 +02:00
if (value > remainingCoins.value) {
break
} else {
const label = valuenorm.toString()
2022-09-13 12:28:33 +02:00
arrayMarkerLabel.value.push({ value, label })
2022-09-12 18:36:54 +02:00
}
}
}
}
}
2022-09-03 13:06:35 +02:00
function mounted() {
// ....
if (props.to_user) {
console.log('user', props.to_user)
2023-01-03 16:51:45 +01:00
bothcircuits.value = userStore.getMyCircuitsInCommonByUser(props.to_user)
if (props.circuitname) {
circuitsel.value = props.circuitname
} else {
circuitsel.value = tools.getCookie(tools.CIRCUIT_USE, bothcircuits.value[0])
}
if (!userStore.IsMyCircuitByName(circuitsel.value)) {
circuitsel.value = bothcircuits.value[0]
}
2023-01-12 01:03:19 +01:00
qty.value = props.qtydefault
2023-01-03 16:51:45 +01:00
aggiorna()
show.value = true
}
if (props.to_group) {
console.log('group', props.to_group)
bothcircuits.value = userStore.getMyCircuitsInCommonByGroup(props.to_group)
2022-09-03 13:06:35 +02:00
if (props.circuitname) {
circuitsel.value = props.circuitname
} else {
circuitsel.value = tools.getCookie(tools.CIRCUIT_USE, bothcircuits.value[0])
}
if (!userStore.IsMyCircuitByName(circuitsel.value)) {
circuitsel.value = bothcircuits.value[0]
}
2022-09-03 13:06:35 +02:00
aggiorna()
2022-09-03 13:06:35 +02:00
show.value = true
}
2022-09-03 13:06:35 +02:00
}
function hide() {
emit('close', true)
}
function sendCoin() {
2023-01-03 16:51:45 +01:00
console.log('sendcoin', qty.value, props.to_group ? props.to_group.groupname : props.to_user.username)
let ok = (props.to_user && props.to_user.username) || (props.to_group && props.to_group.groupname)
2022-09-03 13:06:35 +02:00
2023-01-03 16:51:45 +01:00
if (ok && qty.value && circuitloaded.value) {
let myrecsendcoin: ISendCoin = {
2022-09-13 12:28:33 +02:00
qty: tools.convstrToNum(qty.value),
2023-01-03 16:51:45 +01:00
dest: '',
groupdest: '',
grouporig: '',
2022-09-03 13:06:35 +02:00
circuitname: circuitsel.value,
causal: causal.value,
symbol: circuitloaded.value.symbol,
}
2023-01-03 16:51:45 +01:00
myrecsendcoin.groupdest = props.to_group ? props.to_group.groupname : ''
myrecsendcoin.dest = props.to_user ? props.to_user.username : ''
myrecsendcoin.grouporig = tipoConto.value === costanti.AccountType.COMMUNITY_ACCOUNT ? from_groupname.value : ''
2023-01-03 16:51:45 +01:00
2022-09-13 12:28:33 +02:00
if (myrecsendcoin) {
tools.sendCoinsByCircuit($q, circuitloaded.value, myrecsendcoin)
.then((ris: any) => {
if (ris) {
show.value = false
}
})
2022-09-03 13:06:35 +02:00
}
}
}
2023-01-12 01:03:19 +01:00
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,
causalRef,
2022-09-13 12:28:33 +02:00
maxsendable,
circuitStore,
numstep,
costanti,
userStore,
2023-01-12 01:03:19 +01:00
tipoConto,
arrGroupsList,
from_groupname,
2022-09-03 13:06:35 +02:00
}
},
})