Files
salvato.newfreeplanet/src/mixins/mixin-users.ts
2023-12-20 22:11:52 +01:00

290 lines
7.9 KiB
TypeScript
Executable File

import { IMessage, INotif } from '@src/model'
import { useUserStore } from '@store/UserStore'
import { useNotifStore } from '@store/NotifStore'
import { useGlobalStore } from '@store/globalStore'
import { useProducts } from '@store/Products'
import { serv_constants } from '@store/Modules/serv_constants'
import { tools } from '@store/Modules/tools'
import { shared_consts } from '@src/common/shared_vuejs'
// You can declare a mixin as the same style as components.
export default function () {
function getUserByUsername(username: string) {
const userStore = useUserStore()
return userStore.getNameSurnameByUsername(username)
}
function getImgByUsername(username: string) {
const userStore = useUserStore()
return `${userStore.getImgByUsername(username)}`
}
function getRefLink(username: string) {
const userStore = useUserStore()
return `${userStore.getRefLink(username)}`
}
function isValidUsername(username: string) {
return username && username !== 'nessuno' && username !== 'none'
}
function getMyUsername() {
const userStore = useUserStore()
return userStore.my.username
}
function getUsernameChatByMsg(msg: IMessage) {
if (msg) {
if (msg.dest) {
if (msg.dest !== getMyUsername()) return msg.dest
return msg.origin ? msg.origin : {}
}
} else {
return ''
}
return ''
}
function getnumItemsCart(): number {
const products = useProducts()
const arrcart = products.cart
if (!!arrcart) {
if (!!arrcart.items) {
const total = arrcart.items.reduce((sum, item) => sum + item.order.quantity + item.order.quantitypreordered , 0)
return total
}
}
return 0
}
function getImgByMsg(msg: IMessage) {
const userStore = useUserStore()
// @ts-ignore
return `${userStore.getImgByUsername(this.getUsernameChatByMsg(msg))}`
}
function getMyImg() {
const userStore = useUserStore()
const ris = userStore.getImgByUsername(userStore.my.username)
const out = (ris !== '') ? `${ris}` : 'images/noimg-user.svg'
// console.log('getMyImg = ', out)
return out
}
function getMyImgforIcon() {
const userStore = useUserStore()
const mypath = userStore.getImgByUsername(userStore.my.username)
const filename = tools.getLastItem(mypath)
let img_small = tools.baseurl(mypath) + '/' + serv_constants.PREFIX_IMG_SMALL + filename
console.log('img_small', img_small)
return (filename !== '') ? `img:${img_small}` : 'fas fa-user'
}
function getIconCart() {
const iconcart = 'fas fa-shopping-cart'
return iconcart
}
function MenuCollapse() {
const globalStore = useGlobalStore()
return globalStore.menuCollapse
// return true
}
function Username() {
const userStore = useUserStore()
return userStore.my.username
}
function myName() {
const userStore = useUserStore()
return userStore.my.name
}
function mySurname() {
const userStore = useUserStore()
return userStore.my.surname
}
function myCell() {
const userStore = useUserStore()
return userStore.my.profile.cell
}
function notAsk_ToVerify() {
const userStore = useUserStore()
return tools.isVerified() && userStore.my.notask_verif
}
function paotest() {
return 'Ciaoooooooooooooooo!'
}
function MadeGift() {
const userStore = useUserStore()
return userStore.my.made_gift
}
function Email() {
const userStore = useUserStore()
return userStore.my.email
}
function getNumMsg() {
// ++Todo: conv
/*
return MessageStore.getlasts_messages().length
*/
return 0
}
function getNumMsgUnread() {
// return userStore.getlasts_messages().length
// ++Todo: conv
// return MessageStore.getnumMsgUnread()
return 0
}
function getMsgText(msg: IMessage, inarray: boolean) {
let add = ''
if (msg.origin && msg.origin === getMyUsername()) add = 'Tu: '
const ris = add + msg.message
if (inarray) return [ris]
return ris
}
function getNotifText($t: any, notif: INotif) {
let descr = notif.descr
let status = notif.status
if (notif.typedir === shared_consts.TypeNotifs.TYPEDIR_FRIENDS) {
if (status === shared_consts.StatusNotifs.STATUS_FRIENDS_ACCEPTED) {
descr += '<br><em>' + $t('friends.accepted') + '</em>'
} else if (status === shared_consts.StatusNotifs.STATUS_FRIENDS_REFUSED) {
descr += '<br><em>' + $t('friends.refused') + '</em>'
}
} else if (notif.typedir === shared_consts.TypeNotifs.TYPEDIR_CIRCUITS) {
if (notif.typeid === shared_consts.TypeNotifs.ID_CIRCUIT_SENDCOINSREQ) {
// console.log('notif.extrarec', notif.extrarec)
if (notif.extrarec && notif.extrarec.hasOwnProperty('causal') && notif.extrarec.causal) {
descr += '<br>' + $t('movement.causal') + ': ' + notif.extrarec.causal
}
}
} else if (notif.typedir === shared_consts.TypeNotifs.TYPEDIR_HANDSHAKE) {
if (status === shared_consts.StatusNotifs.STATUS_HANDSHAKE_ACCEPTED) {
descr += '<br><em>' + $t('handshake.accepted') + '</em>'
}
} else if (notif.typedir === shared_consts.TypeNotifs.TYPEDIR_FAVORITE) {
// if (status === shared_consts.StatusNotifs.STATUS_HANDSHAKE_ACCEPTED) {
// descr += '<br><em>' + $t('handshake.accepted') + '</em>'
// }
} else if (notif.typedir === shared_consts.TypeNotifs.TYPEDIR_CIRCUITS) {
if (notif.typeid === shared_consts.TypeNotifs.ID_CIRCUIT_SENDCOINSREQ) {
// console.log('notif.extrarec', notif.extrarec)
if (notif.extrarec && notif.extrarec.hasOwnProperty('causal') && notif.extrarec.causal) {
descr += '<br>' + $t('movement.causal') + ': ' + notif.extrarec.causal
}
}
} else if (notif.typedir === shared_consts.TypeNotifs.TYPEDIR_GROUPS) {
if (status === shared_consts.GroupsNotifs.STATUS_GROUPS_ACCEPTED) {
descr += '<br><em>' + $t('groups.accepted') + '</em>'
} else if (status === shared_consts.GroupsNotifs.STATUS_GROUPS_REFUSED) {
descr += '<br><em>' + $t('groups.refused') + '</em>'
} else if (status === shared_consts.GroupsNotifs.STATUS_GROUPS_BLOCKED) {
descr += '<br><em>' + $t('groups.blocked') + '</em>'
} else if (status === shared_consts.GroupsNotifs.STATUS_GROUPS_DELETED) {
descr += '<br><em>' + $t('groups.deleted') + '</em>'
} else if (status === shared_consts.GroupsNotifs.STATUS_GROUPS_REMOVED) {
descr += '<br><em>' + $t('groups.removed') + '</em>'
}
}
return descr
}
function getNumNotif() {
const notifStore = useNotifStore()
const mynotifs = notifStore.getlasts_notifs
if (!!mynotifs)
return mynotifs.length
return 0
}
function getNumNotifUnread() {
const notifStore = useNotifStore()
return notifStore.getnumNotifUnread
}
function getnumCoinsUnread() {
const notifStore = useNotifStore()
return notifStore.getnumCoinsUnread
}
function getUsernameChatByNotif(msg: INotif) {
if (msg) {
if (msg.dest) {
return msg.dest
}
} else {
return ''
}
return ''
}
function getTypeDirNotif(msg: INotif) {
return (msg && msg.typedir) ? msg.typedir : 0
}
function getTypeIdNotif(msg: INotif) {
return (msg && msg.typeid) ? msg.typeid : 0
}
function getImgByNotif(notif: INotif) {
const userStore = useUserStore()
return `${userStore.getImgByUsername(notif.sender)}`
}
return {
getUsernameChatByMsg,
getMyUsername,
Username,
myName,
mySurname,
myCell,
notAsk_ToVerify,
MadeGift,
Email,
getMyImg,
getMyImgforIcon,
getImgByMsg,
getnumItemsCart,
getUserByUsername,
getImgByUsername,
isValidUsername,
getNumMsg,
getNumMsgUnread,
getMsgText,
paotest,
getRefLink,
getNumNotifUnread,
getNumNotif,
getUsernameChatByNotif,
getTypeDirNotif,
getTypeIdNotif,
getImgByNotif,
getNotifText,
getnumCoinsUnread,
}
}