Skip to content

Commit

Permalink
Merge pull request #1703 from tomivm/fix/create-remote-Board-on-updat…
Browse files Browse the repository at this point in the history
…e-LoadBoards

Fix/create-remote-Board-on-update-LoadBoards
  • Loading branch information
tomivm authored Jul 3, 2024
2 parents 8592f43 + 2e86193 commit 92aac1a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 34 deletions.
89 changes: 66 additions & 23 deletions src/components/Board/Board.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ import {
DOWNLOAD_IMAGES_STARTED,
DOWNLOAD_IMAGE_SUCCESS,
DOWNLOAD_IMAGE_FAILURE,
REMOVE_BOARDS_FROM_LIST
REMOVE_BOARDS_FROM_LIST,
UNMARK_SHOULD_CREATE_API_BOARD,
SHORT_ID_MAX_LENGTH
} from './Board.constants';

import API from '../../api';
Expand Down Expand Up @@ -523,7 +525,7 @@ export function getApiMyBoards() {
}

export function createApiBoard(boardData, boardId) {
return dispatch => {
return async dispatch => {
dispatch(createApiBoardStarted());
boardData = {
...boardData,
Expand All @@ -543,7 +545,7 @@ export function createApiBoard(boardData, boardId) {
}

export function updateApiBoard(boardData) {
return dispatch => {
return async dispatch => {
dispatch(updateApiBoardStarted());
return API.updateBoard(boardData)
.then(res => {
Expand Down Expand Up @@ -696,10 +698,10 @@ export function updateApiObjectsNoChild(
parentBoard,
createParentBoard = false
) {
return (dispatch, getState) => {
return async (dispatch, getState) => {
//create - update parent board
const action = createParentBoard ? createApiBoard : updateApiBoard;
return dispatch(action(parentBoard, parentBoard.id))
return await dispatch(action(parentBoard, parentBoard.id))
.then(res => {
const updatedParentBoardId = res.id;
//add new boards to the active communicator
Expand Down Expand Up @@ -727,8 +729,8 @@ export function updateApiObjectsNoChild(
dispatch(upsertCommunicator(comm));
}
return dispatch(upsertApiCommunicator(comm))
.then(() => {
dispatch(updateApiMarkedBoards());
.then(async () => {
await dispatch(updateApiMarkedBoards());
return updatedParentBoardId;
})
.catch(e => {
Expand All @@ -741,27 +743,68 @@ export function updateApiObjectsNoChild(
};
}
export function updateApiMarkedBoards() {
return (dispatch, getState) => {
return async (dispatch, getState) => {
const allBoards = [...getState().board.boards];
for (let i = 0; i < allBoards.length; i++) {
for await (const board of allBoards) {
const boardsIds = getState().board.boards?.map(board => board.id);
if (!boardsIds.includes(board.id)) return;

if (
allBoards[i].id.length > 14 &&
allBoards[i].hasOwnProperty('email') &&
allBoards[i].email === getState().app.userData.email &&
allBoards[i].hasOwnProperty('markToUpdate') &&
allBoards[i].markToUpdate
board.id.length > 14 &&
board.hasOwnProperty('email') &&
board.email === getState().app.userData.email &&
board.hasOwnProperty('markToUpdate') &&
board.markToUpdate
) {
dispatch(updateApiBoard(allBoards[i]))
.then(() => {
dispatch(unmarkBoard(allBoards[i].id));
return;
})
.catch(e => {
throw new Error(e.message);
});
try {
await dispatch(updateApiBoard(board));
dispatch(unmarkBoard(board.id));
} catch (e) {
throw new Error(e.message);
}
}
if (board.id.length < SHORT_ID_MAX_LENGTH && board.shouldCreateBoard) {
const state = getState();

// TODO - translate name using intl in a redux action
//name: intl.formatMessage({ id: allBoards[i].nameKey })
const extractName = () => {
const splitedNameKey = board.nameKey.split('.');
const NAMEKEY_LAST_INDEX = splitedNameKey.length - 1;
return splitedNameKey[NAMEKEY_LAST_INDEX];
};
const name = board.name ?? extractName();
let boardData = {
...board,
author: state.app.userData.name,
email: state.app.userData.email,
hidden: false,
locale: state.lang,
name
};
delete boardData.shouldCreateBoard;
dispatch(unmarkShouldCreateBoard(boardData.id));

dispatch(updateBoard(boardData));
try {
const boardId = await dispatch(
updateApiObjectsNoChild(boardData, true)
);
dispatch(
replaceBoard({ ...boardData }, { ...boardData, id: boardId })
);
} catch (err) {
console.log(err.message);
}
}
}
return;
};
}

function unmarkShouldCreateBoard(boardId) {
return {
type: UNMARK_SHOULD_CREATE_API_BOARD,
boardId
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/components/Board/Board.constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const CHANGE_IMPROVED_PHRASE = 'cboard/Board/CHANGE_IMPROVED_PHRASE';
export const CHANGE_LIVE_MODE = 'cboard/Board/CHANGE_LIVE_MODE';
export const HISTORY_REMOVE_BOARD = 'cboard/Board/HISTORY_REMOVE_BOARD';
export const UNMARK_BOARD = 'cboard/Board/UNMARK_BOARD';
export const UNMARK_SHOULD_CREATE_API_BOARD =
'cboard/Board/UNMARK_SHOULD_CREATE_API_BOARD';
export const CREATE_API_BOARD_SUCCESS = 'cboard/Board/CREATE_API_BOARD_SUCCESS';
export const CREATE_API_BOARD_FAILURE = 'cboard/Board/CREATE_API_BOARD_FAILURE';
export const CREATE_API_BOARD_STARTED = 'cboard/Board/CREATE_API_BOARD_STARTED';
Expand All @@ -42,3 +44,4 @@ export const DOWNLOAD_IMAGE_SUCCESS = 'cboard/Board/DOWNLOAD_IMAGE_SUCCESS';
export const DOWNLOAD_IMAGE_FAILURE = 'cboard/Board/DOWNLOAD_IMAGE_FAILURE';
export const DEFAULT_ROWS_NUMBER = 5;
export const DEFAULT_COLUMNS_NUMBER = 5;
export const SHORT_ID_MAX_LENGTH = 14;
19 changes: 9 additions & 10 deletions src/components/Board/Board.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,16 +531,15 @@ export class BoardContainer extends Component {
updateBoard(boardData);
}
//api updates
updateApiObjectsNoChild(boardData, createBoard)
.then(boardId => {
if (createBoard) {
replaceBoard({ ...boardData }, { ...boardData, id: boardId });
}
this.props.history.replace(`/board/${boardId}`);
})
.catch(err => {
console.log(err.message);
});
const boardId = await updateApiObjectsNoChild(
boardData,

createBoard
);
if (createBoard) {
replaceBoard({ ...boardData }, { ...boardData, id: boardId });
}
this.props.history.replace(`/board/${boardId}`);
} catch (err) {
console.log(err.message);
} finally {
Expand Down
19 changes: 18 additions & 1 deletion src/components/Board/Board.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ import {
DOWNLOAD_IMAGES_STARTED,
DOWNLOAD_IMAGE_SUCCESS,
DOWNLOAD_IMAGE_FAILURE,
REMOVE_BOARDS_FROM_LIST
REMOVE_BOARDS_FROM_LIST,
UNMARK_SHOULD_CREATE_API_BOARD,
SHORT_ID_MAX_LENGTH
} from './Board.constants';
import { LOGOUT, LOGIN_SUCCESS } from '../Account/Login/Login.constants';

Expand Down Expand Up @@ -296,6 +298,15 @@ function boardReducer(state = initialState, action) {
: { ...board, markToUpdate: false }
)
};
case UNMARK_SHOULD_CREATE_API_BOARD:
return {
...state,
boards: state.boards.map(board =>
board.id !== action.boardId
? board
: { ...board, shouldCreateBoard: false }
)
};
case CHANGE_OUTPUT:
return {
...state,
Expand All @@ -320,6 +331,12 @@ function boardReducer(state = initialState, action) {
) {
creadBoards[i].markToUpdate = true;
}

const shouldCreateBoard =
creadBoards[i].id.length < SHORT_ID_MAX_LENGTH;
if (shouldCreateBoard) {
creadBoards[i].shouldCreateBoard = true;
}
}
}
}
Expand Down

0 comments on commit 92aac1a

Please sign in to comment.