Skip to content

Commit

Permalink
Merge branch 'master' into fix/create-remote-Board-on-update-LoadBoards
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivm committed Jul 3, 2024
2 parents ef123c4 + 8592f43 commit 2e86193
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 53 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
46 changes: 39 additions & 7 deletions src/components/Board/Board.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
DOWNLOAD_IMAGES_STARTED,
DOWNLOAD_IMAGE_SUCCESS,
DOWNLOAD_IMAGE_FAILURE,
REMOVE_BOARDS_FROM_LIST,
UNMARK_SHOULD_CREATE_API_BOARD,
SHORT_ID_MAX_LENGTH
} from './Board.constants';
Expand All @@ -53,7 +54,8 @@ import {
upsertApiCommunicator,
updateDefaultBoardsIncluded,
addDefaultBoardIncluded,
verifyAndUpsertCommunicator
verifyAndUpsertCommunicator,
concatDefaultBoardIdToBlacklist
} from '../Communicator/Communicator.actions';
import { isAndroid, writeCvaFile } from '../../cordova-util';
import { DEFAULT_BOARDS } from '../../helpers';
Expand Down Expand Up @@ -153,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 @@ -200,9 +206,7 @@ export function changeDefaultBoard(selectedBoardNameOnJson) {
homeBoardId
});

switchActiveBoard(homeBoardId);

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

Expand Down Expand Up @@ -269,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 @@ -517,7 +532,8 @@ export function createApiBoard(boardData, boardId) {
isPublic: false
};
return API.createBoard(boardData)
.then(res => {
.then(async res => {
await dispatch(concatDefaultBoardIdToBlacklist(boardId));
dispatch(createApiBoardSuccess(res, boardId));
return res;
})
Expand Down Expand Up @@ -864,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
});
};
}
1 change: 1 addition & 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 Down
37 changes: 34 additions & 3 deletions src/components/Board/Board.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import {
IS_BROWSING_FROM_APPLE_TOUCH,
IS_BROWSING_FROM_SAFARI
} from '../../constants';
import { ALL_DEFAULT_BOARDS, isRemoteIdChecker } from '../../helpers';
//import { isAndroid } from '../../cordova-util';

const ogv = require('ogv');
Expand Down Expand Up @@ -273,9 +274,14 @@ export class BoardContainer extends Component {

if (!boardExists) {
// try the root board
boardExists = boards.find(b => b.id === communicator.rootBoard);
const homeBoard = communicator.rootBoard;
boardExists = boards.find(b => b.id === homeBoard);
if (!boardExists) {
boardExists = boards.find(b => b.id !== '');
if (isRemoteIdChecker(homeBoard))
boardExists = this.tryRemoteBoard(homeBoard);
if (!boardExists)
boardExists = this.addDefaultBoardIfnecessary(homeBoard);
if (!boardExists) boardExists = boards.find(b => b.id !== '');
}
}
const boardId = boardExists.id;
Expand Down Expand Up @@ -867,8 +873,21 @@ export class BoardContainer extends Component {
};

if (tile.loadBoard) {
const loadBoardFinder = loadBoardSearched => {
const findBoardOnStore = boardId =>
this.props.boards.find(b => b.id === boardId);

const nextBoard = findBoardOnStore(loadBoardSearched);
if (nextBoard) return nextBoard;
if (
ALL_DEFAULT_BOARDS.map(({ id }) => id).includes(loadBoardSearched)
) {
const nextBoard = this.addDefaultBoardIfnecessary(loadBoardSearched);
if (nextBoard) return nextBoard;
}
};
const nextBoard =
boards.find(b => b.id === tile.loadBoard) ||
loadBoardFinder(tile.loadBoard) ||
// If the board id is invalid, try falling back to a board
// with the right name.
boards.find(b => b.name === tile.label);
Expand Down Expand Up @@ -1510,6 +1529,18 @@ export class BoardContainer extends Component {
: [];
};

addDefaultBoardIfnecessary = boardId => {
const { boards, addBoards } = this.props;
if (!boards.find(b => b.id === boardId)) {
const board = ALL_DEFAULT_BOARDS.find(({ id }) => id === boardId);
if (board) {
addBoards([board]);
return board;
}
return;
}
};

render() {
const {
navHistory,
Expand Down
9 changes: 8 additions & 1 deletion src/components/Board/Board.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
DOWNLOAD_IMAGES_STARTED,
DOWNLOAD_IMAGE_SUCCESS,
DOWNLOAD_IMAGE_FAILURE,
REMOVE_BOARDS_FROM_LIST,
UNMARK_SHOULD_CREATE_API_BOARD,
SHORT_ID_MAX_LENGTH
} from './Board.constants';
Expand Down Expand Up @@ -251,7 +252,13 @@ function boardReducer(state = initialState, action) {
board => action.boardId.indexOf(board.id) === -1
)
};

case REMOVE_BOARDS_FROM_LIST:
return {
...state,
boards: state.boards.filter(
board => !action.blacklist?.includes(board.id)
)
};
case CREATE_TILE:
return {
...state,
Expand Down
19 changes: 16 additions & 3 deletions src/components/Board/__tests__/Board.actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,21 @@ describe('actions', () => {
expect(actions.previousBoard()).toEqual(expectedAction);
});

it('should create an action to REPLACE_ME', () => {
it('should create an action to REPLACE_ME', async () => {
const expectedAction = {
type: types.TO_ROOT_BOARD
};
expect(actions.toRootBoard()).toEqual(expectedAction);
const store = mockStore({
...initialState,
board: {
...initialState.board,
navHistory: ['12345678901234567'],
boards: [{ ...mockBoard, id: '12345678901234567' }]
}
});
await store.dispatch(actions.toRootBoard());
const toRootBoardAction = store.getActions()[0];
expect(toRootBoardAction).toEqual(expectedAction);
});

it('should create an action to REPLACE_ME', () => {
Expand Down Expand Up @@ -318,7 +328,10 @@ describe('actions', () => {
boardId: '12345678901234567',
type: 'cboard/Board/CREATE_API_BOARD_SUCCESS'
};
expect(actions[1]).toEqual(dataResp);
const successAction = actions.find(
action => action.type === types.CREATE_API_BOARD_SUCCESS
);
expect(successAction).toEqual(dataResp);
expect(data).toEqual(mockBoard);
})
.catch(e => {
Expand Down
Loading

0 comments on commit 2e86193

Please sign in to comment.