Skip to content

Commit

Permalink
Merge branch 'master' into feature/edit-folder-load-board
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivm committed Jul 4, 2024
2 parents 958ba42 + 92aac1a commit d16d574
Show file tree
Hide file tree
Showing 15 changed files with 606 additions and 310 deletions.
3 changes: 2 additions & 1 deletion src/api/communicators.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"author": "Cboard Team",
"email": "[email protected]",
"rootBoard": "root",
"boards": ["root"]
"boards": ["root"],
"defaultBoardBlackList": []
}
]
9 changes: 6 additions & 3 deletions src/components/Account/Login/Login.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '../../App/App.actions';
import { getVoiceURI } from '../../../i18n';
import { isCordova, isElectron } from '../../../cordova-util';
import { isRemoteIdChecker } from '../../../helpers';

export function loginSuccess(payload) {
return dispatch => {
Expand Down Expand Up @@ -147,7 +148,10 @@ export function login({ email, password, activatedData }, type = 'local') {
);

if (loginData.communicators && loginData.communicators.length) {
currentCommunicator = loginData.communicators[0];
const lastRemoteSavedCommunicatorIndex =
loginData.communicators.length - 1;
currentCommunicator =
loginData.communicators[lastRemoteSavedCommunicatorIndex]; //use the latest communicator
}

const localBoardsIds = [];
Expand All @@ -166,13 +170,12 @@ export function login({ email, password, activatedData }, type = 'local') {
.map(async id => {
let board = null;
try {
board = await API.getBoard(id);
if (isRemoteIdChecker(id)) board = await API.getBoard(id);
} catch (e) {}
return board;
})
.filter(b => b !== null)
);

dispatch(addBoards(apiBoards));
if (type === 'local') {
dispatch(
Expand Down
170 changes: 113 additions & 57 deletions src/components/Board/Board.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,31 @@ import {
DOWNLOAD_IMAGES_FAILURE,
DOWNLOAD_IMAGES_STARTED,
DOWNLOAD_IMAGE_SUCCESS,
DOWNLOAD_IMAGE_FAILURE
DOWNLOAD_IMAGE_FAILURE,
REMOVE_BOARDS_FROM_LIST,
UNMARK_SHOULD_CREATE_API_BOARD,
SHORT_ID_MAX_LENGTH
} from './Board.constants';

import API from '../../api';

import {
updateApiCommunicator,
createApiCommunicator,
replaceBoardCommunicator,
upsertCommunicator,
getApiMyCommunicators,
editCommunicator,
upsertApiCommunicator,
updateDefaultBoardsIncluded,
addDefaultBoardIncluded,
createCommunicator,
changeCommunicator
verifyAndUpsertCommunicator,
concatDefaultBoardIdToBlacklist
} from '../Communicator/Communicator.actions';
import { isAndroid, writeCvaFile } from '../../cordova-util';
import { DEFAULT_BOARDS } from '../../helpers';
import history from './../../history';
import { improvePhraseAbortController } from '../../api/api';
import shortid from 'shortid';

const BOARDS_PAGE_LIMIT = 100;
const BOARDS_PAGE_LIMIT = 1000;

export function importBoards(boards) {
return {
Expand Down Expand Up @@ -102,18 +102,9 @@ export function changeDefaultBoard(selectedBoardNameOnJson) {

if (userData.email && activeCommunicator.email !== userData?.email) {
// Create a new communicator for the user if it doesn't exist
const newCommunicator = {
...activeCommunicator,
boards: [...activeCommunicator.boards],
author: userData.name,
email: userData.email,
id: shortid.generate()
};
dispatch(createCommunicator(newCommunicator));
dispatch(changeCommunicator(newCommunicator.id));
return newCommunicator;
dispatch(verifyAndUpsertCommunicator(activeCommunicator));
}
return activeCommunicator;
return getActiveCommunicator(getState);
};

const activeCommunicator = checkUserCommunicator();
Expand Down Expand Up @@ -164,10 +155,14 @@ export function changeDefaultBoard(selectedBoardNameOnJson) {

const switchActiveBoard = homeBoardId => {
if (homeBoardId) {
const storeBoards = getState().board.boards;
const board = storeBoards.find(board => board.id === homeBoardId);
if (!board) return null;
const goTo = `/board/${homeBoardId}`;

dispatch(switchBoard(homeBoardId));
history.replace(goTo);
return true;
}
};

Expand Down Expand Up @@ -196,7 +191,9 @@ export function changeDefaultBoard(selectedBoardNameOnJson) {

dispatch(editCommunicator(communicatorWithRootBoardReplaced));
if (userData?.role) {
dispatch(upsertApiCommunicator(communicatorWithRootBoardReplaced));
dispatch(
upsertApiCommunicator(communicatorWithRootBoardReplaced)
).catch(console.error);
}
};

Expand All @@ -209,9 +206,7 @@ export function changeDefaultBoard(selectedBoardNameOnJson) {
homeBoardId
});

switchActiveBoard(homeBoardId);

replaceHomeBoard(homeBoardId);
if (switchActiveBoard(homeBoardId)) replaceHomeBoard(homeBoardId);
};
}

Expand Down Expand Up @@ -278,8 +273,19 @@ export function previousBoard() {
}

export function toRootBoard() {
return {
type: TO_ROOT_BOARD
return (dispatch, getState) => {
const navHistory = getState().board.navHistory;
const firstBoardOnHistory = navHistory[0];
const allBoardsIds = getState().board.boards.map(board => board.id);

if (!firstBoardOnHistory || !allBoardsIds.includes(firstBoardOnHistory)) {
return null;
}
history.replace(firstBoardOnHistory);
dispatch({
type: TO_ROOT_BOARD
});
return firstBoardOnHistory;
};
}

Expand Down Expand Up @@ -519,14 +525,15 @@ export function getApiMyBoards() {
}

export function createApiBoard(boardData, boardId) {
return dispatch => {
return async dispatch => {
dispatch(createApiBoardStarted());
boardData = {
...boardData,
isPublic: false
};
return API.createBoard(boardData)
.then(res => {
.then(async res => {
await dispatch(concatDefaultBoardIdToBlacklist(boardId));
dispatch(createApiBoardSuccess(res, boardId));
return res;
})
Expand All @@ -538,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 @@ -689,13 +696,12 @@ function getFileNameFromUrl(url) {

export function updateApiObjectsNoChild(
parentBoard,
createCommunicator = false,
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 All @@ -722,12 +728,9 @@ export function updateApiObjectsNoChild(
comm.activeBoardId = updatedParentBoardId;
dispatch(upsertCommunicator(comm));
}
const caction = createCommunicator
? createApiCommunicator
: updateApiCommunicator;
return dispatch(caction(comm, comm.id))
.then(() => {
dispatch(updateApiMarkedBoards());
return dispatch(upsertApiCommunicator(comm))
.then(async () => {
await dispatch(updateApiMarkedBoards());
return updatedParentBoardId;
})
.catch(e => {
Expand All @@ -740,34 +743,74 @@ 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
};
}

export function updateApiObjects(
childBoard,
parentBoard,
createCommunicator = false,
createParentBoard = false
) {
return (dispatch, getState) => {
Expand Down Expand Up @@ -819,10 +862,7 @@ export function updateApiObjects(
comm.activeBoardId = updatedParentBoardId;
dispatch(upsertCommunicator(comm));
}
const caction = createCommunicator
? createApiCommunicator
: updateApiCommunicator;
return dispatch(caction(comm, comm.id))
return dispatch(upsertApiCommunicator(comm))
.then(() => {
dispatch(updateApiMarkedBoards());
return updatedParentBoardId;
Expand All @@ -840,3 +880,19 @@ export function updateApiObjects(
});
};
}

export function removeBoardsFromList(blacklist = [], rootBoard) {
return (dispatch, getState) => {
const actualBoardId = getState().board.activeBoardId;
if (blacklist.includes(actualBoardId)) {
history.replace(rootBoard);
dispatch(switchBoard(rootBoard));
const rootBoardFinded = dispatch(toRootBoard());
if (!rootBoardFinded || blacklist.includes(rootBoard)) return;
}
dispatch({
type: REMOVE_BOARDS_FROM_LIST,
blacklist
});
};
}
4 changes: 4 additions & 0 deletions src/components/Board/Board.constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const ADD_BOARDS = 'cboard/Board/ADD_BOARDS';
export const CREATE_BOARD = 'cboard/Board/CREATE_BOARD';
export const UPDATE_BOARD = 'cboard/Board/UPDATE_BOARD';
export const DELETE_BOARD = 'cboard/Board/DELETE_BOARD';
export const REMOVE_BOARDS_FROM_LIST = 'cboard/Board/REMOVE_BOARDS_FROM_LIST';
export const CHANGE_BOARD = 'cboard/Board/CHANGE_BOARD';
export const REPLACE_BOARD = 'cboard/Board/REPLACE_BOARD';
export const SWITCH_BOARD = 'cboard/Board/SWITCH_BOARD';
Expand All @@ -19,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 @@ -41,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;
Loading

0 comments on commit d16d574

Please sign in to comment.