Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: comment sort by date, create anonymous comments in card group #804

Merged
merged 3 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions backend/src/modules/comments/controller/comments.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Body,
Controller,
Delete,
HttpStatus,
Inject,
Param,
Post,
Expand Down Expand Up @@ -274,18 +275,18 @@ export default class CommentsController {
const board = await this.deleteCommentApp.deleteItemComment(
boardId,
commentId,
request.user._id
request.user._id.toString()
);

if (!board) {
if (board.modifiedCount !== 1) {
throw new BadRequestException(DELETE_FAILED);
}

if (socketId) {
this.socketService.sendUpdatedBoard(boardId, socketId);
}

return board;
return HttpStatus.OK;
}

@ApiOperation({ summary: 'Delete a comment in a card' })
Expand Down Expand Up @@ -320,17 +321,17 @@ export default class CommentsController {
const board = await this.deleteCommentApp.deleteCardGroupComment(
boardId,
commentId,
request.user._id
request.user._id.toString()
);

if (!board) {
if (board.modifiedCount !== 1) {
throw new BadRequestException(DELETE_FAILED);
}

if (socketId) {
this.socketService.sendUpdatedBoard(boardId, socketId);
}

return board;
return HttpStatus.OK;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import { LeanDocument } from 'mongoose';
import { BoardDocument } from 'src/modules/boards/schemas/board.schema';
import { UpdateResult } from 'mongodb';

export interface DeleteCommentApplication {
deleteItemComment(
boardId: string,
commentId: string,
userId: string
): Promise<LeanDocument<BoardDocument> | null>;
deleteItemComment(boardId: string, commentId: string, userId: string): Promise<UpdateResult>;

deleteCardGroupComment(
boardId: string,
commentId: string,
userId: string
): Promise<LeanDocument<BoardDocument> | null>;
deleteCardGroupComment(boardId: string, commentId: string, userId: string): Promise<UpdateResult>;
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import { LeanDocument } from 'mongoose';
import { BoardDocument } from 'src/modules/boards/schemas/board.schema';
import { UpdateResult } from 'mongodb';

export interface DeleteCommentService {
deleteItemComment(
boardId: string,
commentId: string,
userId: string
): Promise<LeanDocument<BoardDocument> | null>;
deleteItemComment(boardId: string, commentId: string, userId: string): Promise<UpdateResult>;

deleteCardGroupComment(
boardId: string,
commentId: string,
userId: string
): Promise<LeanDocument<BoardDocument> | null>;
deleteCardGroupComment(boardId: string, commentId: string, userId: string): Promise<UpdateResult>;
}
4 changes: 3 additions & 1 deletion backend/src/modules/comments/schemas/comment.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import User from 'src/modules/users/entities/user.schema';

export type CommentDocument = Comment & mongoose.Document;

@Schema()
@Schema({
timestamps: true
})
export default class Comment {
@Prop({ nullable: false })
text!: string;
Expand Down
15 changes: 12 additions & 3 deletions backend/src/modules/comments/services/create.comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export default class CreateCommentServiceImpl implements CreateCommentService {
'columns.$.cards.$[c].items.$[i].comments': {
text,
createdBy: userId,
anonymous
anonymous,
createdAt: new Date()
}
}
},
Expand All @@ -40,7 +41,13 @@ export default class CreateCommentServiceImpl implements CreateCommentService {
.exec();
}

createCardGroupComment(boardId: string, cardId: string, userId: string, text: string) {
createCardGroupComment(
boardId: string,
cardId: string,
userId: string,
text: string,
anonymous: boolean
) {
return this.boardModel
.findOneAndUpdate(
{
Expand All @@ -51,7 +58,9 @@ export default class CreateCommentServiceImpl implements CreateCommentService {
$push: {
'columns.$.cards.$[c].comments': {
text,
createdBy: userId
createdBy: userId,
anonymous,
createdAt: new Date()
}
}
},
Expand Down
23 changes: 13 additions & 10 deletions backend/src/modules/comments/services/delete.comment.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { UpdateResult } from 'mongodb';
import { Model } from 'mongoose';
import Board, { BoardDocument } from 'src/modules/boards/schemas/board.schema';
import { DeleteCommentService } from '../interfaces/services/delete.comment.service.interface';
Expand All @@ -8,44 +9,46 @@ import { DeleteCommentService } from '../interfaces/services/delete.comment.serv
export default class DeleteCommentServiceImpl implements DeleteCommentService {
constructor(@InjectModel(Board.name) private boardModel: Model<BoardDocument>) {}

deleteItemComment(boardId: string, commentId: string, userId: string) {
deleteItemComment(boardId: string, commentId: string, userId: string): Promise<UpdateResult> {
return this.boardModel
.findOneAndUpdate(
.updateOne(
{
_id: boardId,
'columns.cards.items.comments._id': commentId,
'columns.cards.items.comments.createdBy': userId
},
{
$pull: {
'columns.$.cards.$[].items.$[].comments': {
'columns.$[].cards.$[].items.$[].comments': {
_id: commentId,
createdBy: userId
}
}
},
{ new: true }
}
)
.lean()
.exec();
}

deleteCardGroupComment(boardId: string, commentId: string, userId: string) {
deleteCardGroupComment(
boardId: string,
commentId: string,
userId: string
): Promise<UpdateResult> {
return this.boardModel
.findOneAndUpdate(
.updateOne(
{
_id: boardId,
'columns.cards.comments._id': commentId
},
{
$pull: {
'columns.$.cards.$[].comments': {
'columns.$[].cards.$[].comments': {
_id: commentId,
createdBy: userId
}
}
},
{ new: true }
}
)
.lean()
.exec();
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/components/Board/Card/CardBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ const CardBoard = React.memo<CardBoardProps>(
}) => {
const isCardGroup = card.items.length > 1;
const comments = useMemo(
() => (card.items.length === 1 ? card.items[0].comments : getCommentsFromCardGroup(card)),
() =>
[
...(card.items.length === 1 ? card.items[0].comments : getCommentsFromCardGroup(card)),
].sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()),
[card],
);

Expand Down
25 changes: 6 additions & 19 deletions frontend/src/components/Board/Card/CardFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ const CardFooter = React.memo<FooterProps>(
} = useVotes();

const user = boardUser;
const disableVotes = maxVotes && user && user.votesCount >= maxVotes;

const calculateVotes = useMemo(() => {
const cardTyped = card as CardType;
Expand Down Expand Up @@ -130,10 +129,8 @@ const CardFooter = React.memo<FooterProps>(
votesInThisCard: votesInThisCard.length,
});

if (user && user.votesCount === maxVotes) {
setMaxVotesReached(true);
} else {
setMaxVotesReached(false);
if (maxVotes) {
setMaxVotesReached(user?.votesCount === maxVotes);
}
}, [
votesOfUserInThisCard,
Expand All @@ -160,7 +157,7 @@ const CardFooter = React.memo<FooterProps>(
...prev,
countVotes: 0,
}));
}, 300);
}, 200);

return () => clearTimeout(timer);
}, [boardId, card._id, cardItemId, mutate, socketId, votesData.countVotes]);
Expand All @@ -175,14 +172,10 @@ const CardFooter = React.memo<FooterProps>(
userVotes: prev.userVotes - 1,
votesInThisCard: prev.votesInThisCard - 1,
}));
toastInfoMessage(`You have ${maxVotes! - (votesData.userVotes - 1)} votes left.`);

if (maxVotes) {
if (user && votesData.userVotes - 1 === maxVotes) {
setMaxVotesReached(true);
} else {
setMaxVotesReached(false);
}
toastInfoMessage(`You have ${maxVotes! - (votesData.userVotes - 1)} votes left.`);
setMaxVotesReached(votesData.userVotes - 1 === maxVotes);
}
};

Expand All @@ -198,11 +191,7 @@ const CardFooter = React.memo<FooterProps>(

if (maxVotes) {
toastInfoMessage(`You have ${maxVotes - (votesData.userVotes + 1)} votes left.`);
if (user && votesData.userVotes + 1 === maxVotes) {
setMaxVotesReached(true);
} else {
setMaxVotesReached(false);
}
setMaxVotesReached(votesData.userVotes + 1 === maxVotes);
}
};

Expand Down Expand Up @@ -245,8 +234,6 @@ const CardFooter = React.memo<FooterProps>(
<StyledButtonIcon
disabled={
!isMainboard ||
!!disableVotes ||
!!(user && maxVotes && votesData.userVotes >= maxVotes) ||
(maxVotes && maxVotesReached) ||
(hideCards && createdBy?._id !== userId)
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/helper/board/transformBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export const handleAddComments = (board: BoardType, changes: AddCommentDto, user
anonymous,
_id: placehodlerId,
id: placehodlerId,
createdAt: new Date().toISOString(),
};

cardItem?.comments.push(commentObj);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/hooks/useComments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ const useComments = () => {

return { previousBoard: board, data };
},
onSettled: (data) => {
queryClient.invalidateQueries(['board', { id: data?._id }]);
onSettled: (data, _, variables) => {
queryClient.invalidateQueries(['board', { id: variables.boardId }]);
},
onError: (data, variables, context) => {
setPreviousBoardQuery(variables.boardId, context);
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/pages/boards/[boardId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ const Board: NextPage<Props> = ({ boardId, mainBoardId }) => {
if (data === null) {
route.push('/board-deleted');
}
}, [data, route]);
if (!userIsInBoard) {
route.back();
}
}, [data, route, userIsInBoard]);

const handleOpen = () => {
setIsOpen(true);
Expand Down
1 change: 1 addition & 0 deletions frontend/src/types/comment/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export default interface CommentType {
createdBy: User;
isNested?: boolean;
anonymous: boolean;
createdAt: string;
}