diff --git a/src/components/Board/Board.actions.js b/src/components/Board/Board.actions.js index 6ed39922d..9c11e2578 100644 --- a/src/components/Board/Board.actions.js +++ b/src/components/Board/Board.actions.js @@ -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'; @@ -523,7 +525,7 @@ export function getApiMyBoards() { } export function createApiBoard(boardData, boardId) { - return dispatch => { + return async dispatch => { dispatch(createApiBoardStarted()); boardData = { ...boardData, @@ -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 => { @@ -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 @@ -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 => { @@ -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 }; } diff --git a/src/components/Board/Board.constants.js b/src/components/Board/Board.constants.js index 4365c968e..9b3937e79 100644 --- a/src/components/Board/Board.constants.js +++ b/src/components/Board/Board.constants.js @@ -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'; @@ -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; diff --git a/src/components/Board/Board.container.js b/src/components/Board/Board.container.js index 25175a079..62a6aad54 100644 --- a/src/components/Board/Board.container.js +++ b/src/components/Board/Board.container.js @@ -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 { diff --git a/src/components/Board/Board.reducer.js b/src/components/Board/Board.reducer.js index d1f669d3d..713f39ce4 100644 --- a/src/components/Board/Board.reducer.js +++ b/src/components/Board/Board.reducer.js @@ -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'; @@ -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, @@ -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; + } } } }