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

141 lines
4.2 KiB
JavaScript
Raw Normal View History

2021-02-18 15:40:54 +00:00
import {makeObservable, observable, action, computed} from 'mobx';
2021-02-18 15:40:54 +00:00
export class storeComments {
constructor() {
makeObservable(this, {
collectionComments: observable,
groupCollectionComments: observable,
filter: observable,
showComments: observable,
changeShowComment: action,
addComment: action,
removeComment: action,
changeComment: action,
changeFilter: action,
2021-03-26 13:23:17 +00:00
groupCollectionFilter: computed,
isOpenEditComment: observable,
openEditComment: action,
isOpenAddReply: observable,
openAddReply: action,
isOpenEditReply: observable,
openEditReply: action
})
}
collectionComments = [];
groupCollectionComments = [];
filter = undefined;
showComments = [];
changeShowComment (uid) {
this.showComments.length = 0;
uid.forEach((item) => {
this.showComments.push(this.findComment(item));
});
}
2021-06-08 13:59:11 +00:00
removeShowComment(id) {
const index = this.showComments.findIndex((comment) => {
return comment.uid === id;
});
if (index !== -1) {
this.showComments.splice(index, 1);
}
}
addComment (comment) {
2021-03-26 13:23:17 +00:00
comment.groupName ? this.groupCollectionComments.push(comment) : this.collectionComments.push(comment);
}
removeComment (id) {
2021-03-26 13:23:17 +00:00
const collection = this.collectionComments.length > 0 ? this.collectionComments : this.groupCollectionComments;
const index = collection.findIndex((comment) => {
return comment.uid === id;
});
if (index !== -1) {
2021-03-26 13:23:17 +00:00
collection.splice(index, 1);
}
2021-06-09 14:17:42 +00:00
this.removeShowComment(id);
}
changeComment (id, changeComment) {
const comment = this.findComment(id);
if (comment) {
comment.comment = changeComment.comment;
comment.userId = changeComment.userId;
comment.userName = changeComment.userName;
2021-07-02 12:48:50 +00:00
comment.parsedName = changeComment.parsedName;
2021-07-02 14:49:53 +00:00
comment.userInitials = changeComment.userInitials;
comment.userColor = changeComment.userColor;
comment.resolved = changeComment.resolved;
comment.quote = changeComment.quote;
comment.time = changeComment.time;
comment.date = changeComment.date;
comment.editable = changeComment.editable;
comment.removable = changeComment.removable;
comment.replies = changeComment.replies;
2021-07-06 14:13:55 +00:00
comment.hide =changeComment.hide;
}
}
changeFilter (filter) {
this.filter = filter;
}
findComment (id) {
2021-03-26 13:23:17 +00:00
const collection = this.collectionComments.length > 0 ? this.collectionComments : this.groupCollectionComments;
let comment = collection.find((item) => {
return item.uid === id;
});
return comment;
}
2021-03-26 13:23:17 +00:00
get groupCollectionFilter () {
if (this.filter && this.groupCollectionComments.length > 0) {
const arr = [];
this.filter.forEach((groupName) => {
this.groupCollectionComments.forEach((comment) => {
if (comment.groupName === groupName) {
arr.push(comment);
}
});
});
2021-03-26 13:23:17 +00:00
return arr;
}
return false;
}
// Edit comment
currentComment = null;
isOpenEditComment = false;
openEditComment (open, comment) {
if (open !== this.isOpenEditComment) {
this.currentComment = open ? comment : null;
this.isOpenEditComment = open;
}
}
2021-03-03 18:21:03 +00:00
currentReply = null;
isOpenAddReply = false;
openAddReply (open, comment) {
if (open !== this.isOpenAddReply) {
this.currentComment = open ? comment : null;
this.isOpenAddReply = open;
}
}
2021-03-03 18:21:03 +00:00
isOpenEditReply = false;
openEditReply (open, comment, reply) {
2021-03-03 18:21:03 +00:00
if (open !== this.isOpenEditReply) {
this.currentComment = open ? comment : null;
this.currentReply = open ? reply : null;
this.isOpenEditReply = open;
}
}
2021-02-18 15:40:54 +00:00
}