Skip to content

Commit

Permalink
Avoid creating duplicated default boards on the db
Browse files Browse the repository at this point in the history
  • Loading branch information
RodriSanchez1 committed Jul 23, 2024
1 parent 43da260 commit 489c510
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/components/Board/Board.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ function boardReducer(state = initialState, action) {
};
case CREATE_API_BOARD_SUCCESS:
const creadBoards = [...state.boards];
const tilesToUpdateIds = [];
const boardsToMarkForCreation = [];
for (let i = 0; i < creadBoards.length; i++) {
let tiles = creadBoards[i].tiles;
if (tiles) {
Expand All @@ -330,17 +332,27 @@ function boardReducer(state = initialState, action) {
creadBoards[i].hasOwnProperty('email')
) {
creadBoards[i].markToUpdate = true;
const tileUpdatedId = creadBoards[i].tiles[j].id;
tilesToUpdateIds.push(tileUpdatedId);
}

const shouldCreateBoard =
creadBoards[i].id.length < SHORT_ID_MAX_LENGTH;
if (shouldCreateBoard) {
creadBoards[i].shouldCreateBoard = true;
boardsToMarkForCreation.push(creadBoards[i]);
}
}
}
}
}
boardsToMarkForCreation.forEach(board => {
//if the tile id is already in a api board, we don't need to create it
const boardTileIds = board.tiles.map(tile => tile.id);
const boardIsAlreadyCreatedOnDb = boardTileIds.some(tileId =>
tilesToUpdateIds.includes(tileId)
);
if (!boardIsAlreadyCreatedOnDb) board.shouldCreateBoard = true;
});
return {
...state,
isFetching: false,
Expand Down

0 comments on commit 489c510

Please sign in to comment.