From 27d286346809472d3db2e3181f2ccd0e127c4130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1tia=20Antunes?= Date: Thu, 5 Jan 2023 19:01:54 +0000 Subject: [PATCH] fix: fix newJoiner status, responsible of subBoard (#807) --- .../boards/services/create.board.service.ts | 8 ++--- .../repositories/team-user.repository.ts | 4 +-- .../teams/repositories/team.repository.ts | 8 ++--- .../src/components/Boards/MyBoards/index.tsx | 2 ++ frontend/src/hooks/useBoard.tsx | 31 +++++++++++++++++-- frontend/src/hooks/useCreateBoard.tsx | 9 +++--- frontend/src/hooks/useSocketIO.ts | 1 - frontend/src/pages/teams/new.tsx | 3 +- 8 files changed, 46 insertions(+), 20 deletions(-) diff --git a/backend/src/modules/boards/services/create.board.service.ts b/backend/src/modules/boards/services/create.board.service.ts index 958a7df24..78a345396 100644 --- a/backend/src/modules/boards/services/create.board.service.ts +++ b/backend/src/modules/boards/services/create.board.service.ts @@ -281,10 +281,10 @@ export default class CreateBoardServiceImpl implements CreateBoardService { sortUsersListByOldestCreatedDate = (users: TeamUser[]) => users .map((user) => { - user.userCreated = - (user.user as User).providerAccountCreatedAt || (user.user as User).joinedAt; - - return user; + return { + ...user, + userCreated: (user.user as User).providerAccountCreatedAt || (user.user as User).joinedAt + }; }) .sort((a, b) => Number(b.userCreated) - Number(a.userCreated)); diff --git a/backend/src/modules/teams/repositories/team-user.repository.ts b/backend/src/modules/teams/repositories/team-user.repository.ts index d82bcbc77..0648487e1 100644 --- a/backend/src/modules/teams/repositories/team-user.repository.ts +++ b/backend/src/modules/teams/repositories/team-user.repository.ts @@ -34,9 +34,9 @@ export class TeamUserRepository } getUsersOfTeam(teamId: string) { - return this.findAllWithQuery({ team: teamId }, undefined, { + return this.findAllWithQuery({ team: teamId }, 'user role isNewJoiner _id', { path: 'user', - select: '_id firstName lastName email isSAdmin' + select: '_id firstName lastName email isSAdmin providerAccountCreatedAt' }); } diff --git a/backend/src/modules/teams/repositories/team.repository.ts b/backend/src/modules/teams/repositories/team.repository.ts index f8f8d8cba..d917d9a97 100644 --- a/backend/src/modules/teams/repositories/team.repository.ts +++ b/backend/src/modules/teams/repositories/team.repository.ts @@ -23,7 +23,7 @@ export class TeamRepository select: 'user role isNewJoiner', populate: { path: 'user', - select: '_id firstName lastName email joinedAt' + select: '_id firstName lastName email joinedAt providerAccountCreatedAt' } } ); @@ -33,10 +33,10 @@ export class TeamRepository return this.findAllWithQuery({ _id: { $in: teamIds } }, { _id: 1, name: 1 }, [ { path: 'users', - select: 'user role', + select: 'user role isNewJoiner', populate: { path: 'user', - select: '_id firstName lastName email joinedAt' + select: '_id firstName lastName email joinedAt providerAccountCreatedAt' } }, { @@ -52,7 +52,7 @@ export class TeamRepository select: 'user role email', populate: { path: 'user', - select: '_id firstName lastName email joinedAt' + select: '_id firstName lastName email joinedAt providerAccountCreatedAt' } }); } diff --git a/frontend/src/components/Boards/MyBoards/index.tsx b/frontend/src/components/Boards/MyBoards/index.tsx index dd9f8e2b2..1359f8492 100644 --- a/frontend/src/components/Boards/MyBoards/index.tsx +++ b/frontend/src/components/Boards/MyBoards/index.tsx @@ -58,6 +58,8 @@ const MyBoards = React.memo(({ userId, isSuperAdmin }) => { const { data, isLoading } = fetchBoards; + console.log('data', data); + const teamSocketId = data?.pages[0].boards[0].team ? data?.pages[0].boards[0].team._id : undefined; diff --git a/frontend/src/hooks/useBoard.tsx b/frontend/src/hooks/useBoard.tsx index 11f0eee6e..a2daa94c6 100644 --- a/frontend/src/hooks/useBoard.tsx +++ b/frontend/src/hooks/useBoard.tsx @@ -1,4 +1,4 @@ -import { useMutation, useQuery } from 'react-query'; +import { InfiniteData, useMutation, useQuery } from 'react-query'; import { useSetRecoilState } from 'recoil'; import { AxiosError } from 'axios'; @@ -11,6 +11,7 @@ import { import { newBoardState } from '@/store/board/atoms/board.atom'; import UseBoardType from '@/types/board/useBoard'; import { ToastStateEnum } from '@/utils/enums/toast-types'; +import BoardType from '@/types/board/board'; import useBoardUtils from './useBoardUtils'; interface AutoFetchProps { @@ -48,8 +49,32 @@ const useBoard = ({ autoFetchBoard = false }: AutoFetchProps): UseBoardType => { }); const deleteBoard = useMutation(deleteBoardRequest, { - onSuccess: () => { - queryClient.invalidateQueries('boards'); + onSuccess: (data, variables) => { + queryClient.setQueryData( + ['boards'], + ( + oldData: + | InfiniteData<{ + boards: BoardType[]; + hasNextPage: boolean; + page: number; + }> + | undefined, + ) => { + if (!oldData) return { pages: [], pageParams: [] }; + + return { + ...oldData, + pages: oldData.pages.map((page) => ({ + ...page, + boards: page.boards.filter((board) => board._id !== variables.id), + })), + }; + }, + ); + + queryClient.invalidateQueries(['boards']); + setToastState({ open: true, content: 'The board was successfully deleted.', diff --git a/frontend/src/hooks/useCreateBoard.tsx b/frontend/src/hooks/useCreateBoard.tsx index 23aeddd41..4ec86843f 100644 --- a/frontend/src/hooks/useCreateBoard.tsx +++ b/frontend/src/hooks/useCreateBoard.tsx @@ -75,10 +75,10 @@ const useCreateBoard = (team?: Team) => { const sortUsersListByOldestCreatedDate = (users: TeamUser[]) => users - .map((user) => { - user.userCreated = user.user.providerAccountCreatedAt || user.user.joinedAt; - return user; - }) + .map((user) => ({ + ...user, + userCreated: user.user.providerAccountCreatedAt || user.user.joinedAt, + })) .sort((a, b) => Number(b.userCreated) - Number(a.userCreated)); const getAvailableUsersToBeResponsible = useCallback((availableUsers: TeamUser[]) => { @@ -103,6 +103,7 @@ const useCreateBoard = (team?: Team) => { // this object ensures that each group has one element that can be responsible const candidateToBeTeamResponsible = getRandomUser(availableUsersToBeResponsible); + randomGroupOfUsers.push({ user: candidateToBeTeamResponsible.user, role: BoardUserRoles.MEMBER, diff --git a/frontend/src/hooks/useSocketIO.ts b/frontend/src/hooks/useSocketIO.ts index 580697360..1037d98f0 100644 --- a/frontend/src/hooks/useSocketIO.ts +++ b/frontend/src/hooks/useSocketIO.ts @@ -19,7 +19,6 @@ export const useSocketIO = (boardId: string): string | undefined => { }); newSocket.on('updateAllBoard', () => { - console.log('UPDATE ALL BOARD'); queryClient.invalidateQueries(['board', { id: boardId }]); }); diff --git a/frontend/src/pages/teams/new.tsx b/frontend/src/pages/teams/new.tsx index b58ff5da5..9045e0f9c 100644 --- a/frontend/src/pages/teams/new.tsx +++ b/frontend/src/pages/teams/new.tsx @@ -13,7 +13,6 @@ import { ToastStateEnum } from '@/utils/enums/toast-types'; import QueryError from '@/components/Errors/QueryError'; import LoadingPage from '@/components/loadings/LoadingPage'; import { Suspense, useEffect } from 'react'; -import { verifyIfIsNewJoiner } from '@/utils/verifyIfIsNewJoiner'; const NewTeam: NextPage = () => { const { data: session } = useSession({ required: true }); @@ -45,7 +44,7 @@ const NewTeam: NextPage = () => { listMembers.push({ user, role: TeamUserRoles.ADMIN, - isNewJoiner: verifyIfIsNewJoiner(user.joinedAt, user.providerAccountCreatedAt), + isNewJoiner: false, }); } });