Skip to content

Commit

Permalink
fix: handeSplitBoards bug and refactor generate subBoards loop
Browse files Browse the repository at this point in the history
  • Loading branch information
nunocaseiro committed May 2, 2022
1 parent 6d7609f commit b2f1662
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const MainBoardCard = React.memo(({ team }: { team: Team }) => {
setCreateBoardData((prev) => ({
...prev,
users,
board: { ...prev.board, dividedBoards: handleSplitBoards(2, maxUsersCount) },
board: { ...prev.board, dividedBoards: handleSplitBoards(2) },
count: {
...prev.count,
teamsCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const QuickEditSubTeams = ({ team }: QuickEditSubTeamsProps) => {
},
board: {
...prev.board,
dividedBoards: handleSplitBoards(Number(values.teamsCount), Number(values.maxUsersCount)),
dividedBoards: handleSplitBoards(Number(values.teamsCount)),
},
}));
};
Expand Down
24 changes: 10 additions & 14 deletions frontend/hooks/useCreateBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,18 @@ const useCreateBoard = (team: Team) => {
);

const handleSplitBoards = useCallback(
(maxTeams: number, maxUsers: number) => {
(maxTeams: number) => {
const subBoards: BoardToAdd[] = [];
const splitedUsers: BoardUserToAdd[][] = [];
const splitedUsers: BoardUserToAdd[][] = new Array(maxTeams).fill([]);

let availableUsers = [...teamMembers];

for (let i = 0; i < teamMembers.length; i++) {
for (let i = 0, j = 0; i < teamMembers.length; i++, j++) {
const teamUser = getRandomUser(availableUsers);
if (splitedUsers[i]?.length === maxUsers) break;

if (i >= maxTeams) i = 0;
splitedUsers[i] = [
...(splitedUsers[i] ?? []),
if (j >= maxTeams) j = 0;
splitedUsers[j] = [
...splitedUsers[j],
{
user: teamUser.user,
role: BoardUserRoles.MEMBER,
Expand All @@ -81,13 +80,13 @@ const useCreateBoard = (team: Team) => {
availableUsers = availableUsers.filter((user) => user.user._id !== teamUser.user._id);
}

for (let i = 0; i < maxTeams; i++) {
new Array(maxTeams).fill(0).forEach((_, i) => {
const newBoard = generateSubBoard(i + 1);
splitedUsers[i][Math.floor(Math.random() * splitedUsers[i].length)].role =
BoardUserRoles.RESPONSIBLE;
newBoard.users = splitedUsers[i];
subBoards.push(newBoard);
}
});
return subBoards;
},
[generateSubBoard, getRandomUser, teamMembers]
Expand Down Expand Up @@ -116,10 +115,7 @@ const useCreateBoard = (team: Team) => {
count: { ...prev.count, maxUsersCount: countUsers, teamsCount: dividedBoardsCount + 1 },
board: {
...prev.board,
dividedBoards: handleSplitBoards(
dividedBoardsCount + 1,
Math.ceil(teamMembers.length / (dividedBoardsCount + 1))
),
dividedBoards: handleSplitBoards(dividedBoardsCount + 1),
},
}));
};
Expand All @@ -132,7 +128,7 @@ const useCreateBoard = (team: Team) => {
count: { ...prev.count, maxUsersCount: countUsers, teamsCount: dividedBoardsCount - 1 },
board: {
...prev.board,
dividedBoards: handleSplitBoards(dividedBoardsCount - 1, countUsers),
dividedBoards: handleSplitBoards(dividedBoardsCount - 1),
},
}));
};
Expand Down

0 comments on commit b2f1662

Please sign in to comment.