web-apps/apps/common/mobile/lib/store/users.js

130 lines
3.7 KiB
JavaScript
Raw Normal View History

2020-08-31 17:31:05 +00:00
2021-03-30 20:11:49 +00:00
import {makeObservable, observable, action, computed} from 'mobx';
2020-08-31 17:31:05 +00:00
2020-09-29 18:59:57 +00:00
export class storeUsers {
constructor() {
makeObservable(this, {
users: observable,
reset: action,
currentUser: observable,
setCurrentUser: action,
connection: action,
isDisconnected: observable,
2021-03-30 20:11:49 +00:00
resetDisconnected: action,
2021-05-14 11:03:06 +00:00
hasEditUsers: computed,
editUsers: computed
})
}
users = [];
currentUser;
isDisconnected = false;
2020-08-31 17:31:05 +00:00
reset (users) {
2020-09-29 18:59:57 +00:00
this.users = Object.values(users)
}
2021-02-18 15:40:54 +00:00
setCurrentUser (id) {
2021-02-18 15:40:54 +00:00
this.users.forEach((item) => {
if (item.asc_getIdOriginal() === id) {
this.currentUser = item;
}
});
return this.currentUser;
2021-02-18 15:40:54 +00:00
}
connection (change) {
let changed = false;
for (let uid in this.users) {
if (undefined !== uid) {
const user = this.users[uid];
if (user && user.asc_getId() === change.asc_getId()) {
this.users[uid] = change;
changed = true;
}
}
}
!changed && change && (this.users[change.asc_getId()] = change);
}
resetDisconnected (isDisconnected) {
this.isDisconnected = isDisconnected;
}
2021-02-18 15:40:54 +00:00
getInitials (name) {
2021-07-02 14:49:53 +00:00
const fio = name.split(' ');
2021-02-18 15:40:54 +00:00
let initials = fio[0].substring(0, 1).toUpperCase();
for (let i = fio.length-1; i>0; i--) {
if (fio[i][0]!=='(' && fio[i][0]!==')') {
initials += fio[i].substring(0, 1).toUpperCase();
break;
}
}
return initials;
}
searchUserById (id) {
2021-03-04 17:24:35 +00:00
let user = null;
this.users.forEach((item) => {
if (item.asc_getIdOriginal() === id) {
2021-03-04 17:24:35 +00:00
user = item;
}
});
2021-03-04 17:24:35 +00:00
return user;
}
searchUserByCurrentId (id) {
let user = null;
this.users.forEach((item) => {
if (item.asc_getId() === id) {
user = item;
}
});
return user;
}
2021-03-30 20:11:49 +00:00
get hasEditUsers () {
let length = 0;
this.users.forEach((item) => {
if ((item.asc_getState()!==false) && !item.asc_getView()) {
length++;
}
});
return (length >= 1);
}
2021-05-14 11:03:06 +00:00
get editUsers () {
const idArray = [];
const usersArray = [];
const curUserId = this.currentUser.asc_getIdOriginal();
this.users.forEach((item) => {
const name = AscCommon.UserInfoParser.getParsedName(item.asc_getUserName());
if((item.asc_getState() !== false) && !item.asc_getView()) {
const idOriginal = item.asc_getIdOriginal();
const ind = idArray.indexOf(idOriginal);
if (ind !== -1) {
usersArray[ind].count = usersArray[ind].count + 1;
} else {
const userAttr = {
color: item.asc_getColor(),
id: item.asc_getId(),
idOriginal: item.asc_getIdOriginal(),
name: name,
view: item.asc_getView(),
initials: this.getInitials(name),
count: 1
};
if(idOriginal === curUserId) {
usersArray.unshift(userAttr);
idArray.unshift(idOriginal);
} else {
usersArray.push(userAttr);
idArray.push(idOriginal);
}
}
}
});
return usersArray;
}
2020-09-29 18:59:57 +00:00
}