Skip to content

Commit

Permalink
reworked handleComment and fixed duplicate add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Severino committed Dec 13, 2024
1 parent 8c08599 commit 311bc05
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 48 deletions.
78 changes: 34 additions & 44 deletions resources/js/bootstrap/stores/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
only,
} from '@/helpers/helpers.js';

import { getEditedComment } from '@/helpers/comment.js';

import {
addEntity,
addEntityType,
Expand Down Expand Up @@ -60,7 +62,7 @@ function updateSelectionTypeIdList(selection) {
const handleAddEntityType = (context, typeData, attributes = []) => {
context.entityTypeAttributes[typeData.id] = attributes.slice();
context.entityTypes[typeData.id] = typeData;
}
};

const handlePostDelete = (context, entityId) => {
const currentRoute = router.currentRoute.value;
Expand All @@ -87,7 +89,7 @@ const handlePostDelete = (context, entityId) => {
}
}
}
}
};

export const useEntityStore = defineStore('entity', {
state: _ => ({
Expand Down Expand Up @@ -180,7 +182,7 @@ export const useEntityStore = defineStore('entity', {
colors = state.entityTypeColors[id];
}
return colors;
}
};
},
getEntityTypeName(state) {
return id => {
Expand Down Expand Up @@ -584,60 +586,48 @@ export const useEntityStore = defineStore('entity', {
},
async removeReference(id, entityId, attributeUrl) {
return deleteReferenceFromEntity(id).then(_ => {
this.handleReference(entityId, attributeUrl, 'delete', {id: id});
this.handleReference(entityId, attributeUrl, 'delete', { id: id });
});
},
handleComment(entityId, comment, action, data) {
const replyTo = data.replyTo;
async addComment(entityId, comment, { replyTo = null } = {}) {
const entity = this.getEntity(entityId);
if(action == 'add') {
if(replyTo) {
const op = entity.comments.find(c => c.id == replyTo);
if(op.replies) {
op.replies.push(comment);
}
op.replies_count++;
} else {
if(!entity.comments) {
entity.comments = [];
}
entity.comments.push(comment);
entity.comments_count++;
if(replyTo) {
const op = entity.comments.find(c => c.id == replyTo);
if(op.replies) {
op.replies.push(comment);
}
} else if(action == 'update' || action == 'delete') {
let editedComment = null;
if(replyTo) {
const op = entity.comments.find(c => c.id == replyTo);
if(op.replies) {
editedComment = op.replies.find(reply => reply.id == comment.id);
}
} else {
editedComment = entity.comments.find(c => c.id == comment.id);
}
if(editedComment) {
if(action == 'update') {
editedComment.content = comment.content;
editedComment.updated_at = comment.updated_at;
} else if(action == 'delete') {
editedComment.content = null;
editedComment.updated_at = Date();
editedComment.deleted_at = Date();
}
op.replies_count++;
} else {
if(!entity.comments) {
entity.comments = [];
}
// We dont need to add the comment manually
// as it is broadcasted via websocket
entity.comments.push(comment);
entity.comments_count++;
}

if(this.selectedEntity) {
this.selectedEntity.comments = entity.comments;
}
},
async addComment() {
async updateComment(entityId, comment, { replyTo = null } = {}) {
const entity = this.getEntity(entityId);
let editedComment = getEditedComment(entity, comment, replyTo);
if(editedComment) {
editedComment.content = comment.content;
editedComment.updated_at = comment.updated_at;

}
},
async editComment() {
// TODO
},
async deleteComment() {
// TODO
async deleteComment(entityId, comment, { replyTo = null } = {}) {
const entity = this.getEntity(entityId);
let editedComment = getEditedComment(entity, comment, replyTo);
if(editedComment) {
editedComment.content = null;
editedComment.updated_at = Date();
editedComment.deleted_at = Date();
}
},
async setById(entityId) {
let entity = this.entities[entityId];
Expand Down
2 changes: 2 additions & 0 deletions resources/js/bootstrap/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Echo from 'laravel-echo';

import Pusher from 'pusher-js';

// Why do we handle this as window objects and not with
// import/export syntax and pollute the window object? [SO]
window.Pusher = Pusher;

window.Echo = new Echo({
Expand Down
2 changes: 1 addition & 1 deletion resources/js/components/EntityDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@
};
const addComment = event => {
entityStore.handleComment(state.entity.id, event.comment, 'add', {
entityStore.addComment(state.entity.id, event.comment, {
replyTo: event.replyTo,
});
};
Expand Down
12 changes: 9 additions & 3 deletions resources/js/handlers/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ export const handleEntityReferenceDeleted = {

export const handleEntityCommentAdded = {
'CommentAdded': e => {
if(e.user.id == useUserStore().getCurrentUserId) return;

const entityStore = useEntityStore();
entityStore.handleComment(e.comment.commentable_id, e.comment, 'add', {
entityStore.addComment(e.comment.commentable_id, e.comment, {
replyTo: e.replyTo,
});

Expand All @@ -138,8 +140,10 @@ export const handleEntityCommentAdded = {

export const handleEntityCommentUpdated = {
'CommentUpdated': e => {
if(e.user.id == useUserStore().getCurrentUserId) return;

const entityStore = useEntityStore();
entityStore.handleComment(e.comment.commentable_id, e.comment, 'update', {
entityStore.updateComment(e.comment.commentable_id, e.comment, {
replyTo: e.replyTo,
});

Expand All @@ -157,8 +161,10 @@ export const handleEntityCommentUpdated = {

export const handleEntityCommentDeleted = {
'CommentDeleted': e => {
if(e.user.id == useUserStore().getCurrentUserId) return;

const entityStore = useEntityStore();
entityStore.handleComment(e.comment.commentable_id, e.comment, 'delete', {
entityStore.deleteComment(e.comment.commentable_id, e.comment, {
replyTo: e.replyTo,
});

Expand Down
12 changes: 12 additions & 0 deletions resources/js/helpers/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function getEditedComment(entity, comment, replyTo) {
let editedComment = null;
if(replyTo) {
const op = entity.comments.find(c => c.id == replyTo);
if(op.replies) {
editedComment = op.replies.find(reply => reply.id == comment.id);
}
} else {
editedComment = entity.comments.find(c => c.id == comment.id);
}
return editedComment;
}

0 comments on commit 311bc05

Please sign in to comment.