Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add only necessary default boards on login #1711

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
05568ae
Add actions to clean all boards and add the necessary default boards
tomivm Jun 11, 2024
c0a07cb
Clean All boards from the store before loginSuccess
tomivm Jun 11, 2024
ae9e5da
Add root board if is not on the communicator boards before login success
tomivm Jun 11, 2024
69f5bee
Fix addRootBoard import
tomivm Jun 11, 2024
bec7283
refactor: Update addRootBoard import and function name to addRootBoar…
tomivm Jun 11, 2024
408fba8
refactor: Rename addRootBoard function to findAndAddRootBoardOnDefaul…
tomivm Jun 12, 2024
360aa9e
Refactor login to add necessary default boards for homeBoards
tomivm Jun 13, 2024
15ba033
Add necessary default boards for remote boards
tomivm Jun 13, 2024
2aeeb4a
Prevent unnecessary requests to get boards for local Ids
tomivm Jun 13, 2024
1dcf25e
Add necessary Default boards on changeDefaultBoard
tomivm Jun 14, 2024
2cca7af
Enforces the possibilities to find the home board if it is requested
tomivm Jun 14, 2024
f1e3fcf
Fix incorrect map to search ids on ALL_DEFAULT_BOARDS
tomivm Jun 14, 2024
ee8c86c
Create helper function to check if id is Remote
tomivm Jun 14, 2024
3a7b973
Add insurance to find Default Boards on a folder click
tomivm Jun 14, 2024
0f7c473
use remoteIdChecker helper function
tomivm Jun 14, 2024
952d81a
mark unnecesaryDefaultBoardsRemoved as true on cleanAllBoards
tomivm Jun 19, 2024
74ee5db
Fix board reducer test
tomivm Jun 25, 2024
82debb1
Merge branch 'refactor/implementUpsertApiCommunicator' into feature/d…
tomivm Jun 26, 2024
4bcd548
fix getActiveComunnicator function
tomivm Jun 26, 2024
150d7b6
Concat default boards Ids in a blacklist on create API board
tomivm Jun 26, 2024
aa38a14
remove unnecessary import
tomivm Jun 26, 2024
54aa14d
await for concatDefaultBoardIdToBlacklist
tomivm Jun 26, 2024
b18c8a2
Remove Boards from the blacklist on update API objects
tomivm Jun 26, 2024
3d7fff1
Merge branch 'master' into pr/tomivm/1711
RodriSanchez1 Jun 26, 2024
8f369d9
Merge branch 'feature/add-only-necessary-default-boards-on-login' of …
RodriSanchez1 Jun 26, 2024
154ab2d
Add 'defaultBoards-blacklist' feature
tomivm Jun 27, 2024
26d9689
Remove staff to clean all boards on login
tomivm Jun 27, 2024
9d4659c
remove unnecessary stuff to addNecessaryDefaultBoardsFor
tomivm Jun 27, 2024
2bf7bd1
edite the 'check createApiBoard' test to work with new actions
tomivm Jun 27, 2024
01a1668
Add prevention to only include default board ids to the blacklist
tomivm Jun 27, 2024
a1f9f4e
improve CREATE_API_BOARD_SUCCESS test to find action
tomivm Jun 27, 2024
1d8256c
use Last communicator if active communicator is not finded
tomivm Jun 28, 2024
27ca47e
fix action to include only default board ids in the blacklist
tomivm Jun 28, 2024
63a01e8
Avoid unnecessary call to API
tomivm Jul 1, 2024
37d2356
Add a default state for defaultBoardBlackList
tomivm Jul 1, 2024
3332394
move to root board if user is on the crated board in another device
tomivm Jul 1, 2024
93b8e1b
Prevent navigation to a deletedBoard
tomivm Jul 1, 2024
9de64fe
Refactor toRootBoard test
tomivm Jul 3, 2024
7e22997
Merge remote-tracking branch 'cboard-org/master' into feature/add-onl…
tomivm Jul 3, 2024
0674579
reduce the possibilities of delete active board
tomivm Jul 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "support@cboard.io",
"rootBoard": "root",
"boards": ["root"]
"boards": ["root"],
"defaultBoardBlackList": []
}
]
4 changes: 2 additions & 2 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 @@ -169,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
48 changes: 40 additions & 8 deletions src/components/Board/Board.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import {
DOWNLOAD_IMAGES_FAILURE,
DOWNLOAD_IMAGES_STARTED,
DOWNLOAD_IMAGE_SUCCESS,
DOWNLOAD_IMAGE_FAILURE
DOWNLOAD_IMAGE_FAILURE,
REMOVE_BOARDS_FROM_LIST
} from './Board.constants';

import API from '../../api';
Expand All @@ -51,7 +52,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 @@ -151,10 +153,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 @@ -198,9 +204,7 @@ export function changeDefaultBoard(selectedBoardNameOnJson) {
homeBoardId
});

switchActiveBoard(homeBoardId);

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

Expand Down Expand Up @@ -267,8 +271,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 @@ -515,7 +530,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 @@ -821,3 +837,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 @@ -868,8 +874,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 @@ -1511,6 +1530,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
11 changes: 9 additions & 2 deletions src/components/Board/Board.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import {
GET_API_MY_BOARDS_STARTED,
DOWNLOAD_IMAGES_STARTED,
DOWNLOAD_IMAGE_SUCCESS,
DOWNLOAD_IMAGE_FAILURE
DOWNLOAD_IMAGE_FAILURE,
REMOVE_BOARDS_FROM_LIST
} from './Board.constants';
import { LOGOUT, LOGIN_SUCCESS } from '../Account/Login/Login.constants';

Expand Down Expand Up @@ -249,7 +250,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
41 changes: 39 additions & 2 deletions src/components/Communicator/Communicator.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import {
import { defaultCommunicatorID } from './Communicator.reducer';
import API from '../../api';
import shortid from 'shortid';
import { removeBoardsFromList, switchBoard } from '../Board/Board.actions';
import { ALL_DEFAULT_BOARDS } from '../../helpers';
import moment from 'moment';
import { switchBoard } from '../Board/Board.actions';
import history from './../../history';

export function importCommunicator(communicator) {
Expand Down Expand Up @@ -234,7 +235,7 @@ export function verifyAndUpsertCommunicator(
*/

export function getApiMyCommunicators() {
return async dispatch => {
return async (dispatch, getState) => {
dispatch(getApiMyCommunicatorsStarted());
try {
const res = await API.getCommunicators();
Expand All @@ -245,6 +246,18 @@ export function getApiMyCommunicators() {
} catch (e) {
console.error(e);
}
const activeCommunicator =
res.data.find(
communicator =>
communicator.id === getState().communicator.activeCommunicator
) ?? res.data[res.data.length - 1];
const defaultBoardBlackList = activeCommunicator?.defaultBoardBlackList;
dispatch(
removeBoardsFromList(
defaultBoardBlackList,
activeCommunicator.rootBoard
)
);
}

return res;
Expand Down Expand Up @@ -384,3 +397,27 @@ export function syncCommunicators(remoteCommunicators) {
}
};
}

export function concatDefaultBoardIdToBlacklist(boardId) {
const getActiveCommunicator = getState => {
return getState().communicator.communicators.find(
c => c.id === getState().communicator.activeCommunicatorId
);
};
return (dispatch, getState) => {
if (!ALL_DEFAULT_BOARDS.map(({ id }) => id).includes(boardId)) return;
const updatedCommunicatorData = { ...getActiveCommunicator(getState) };

const concatBoardIdIfNecessary = () => {
if (!updatedCommunicatorData?.defaultBoardBlackList.includes(boardId))
return updatedCommunicatorData?.defaultBoardBlackList.concat(boardId);
return updatedCommunicatorData?.defaultBoardBlackList;
};

updatedCommunicatorData.defaultBoardBlackList = updatedCommunicatorData?.defaultBoardBlackList
? concatBoardIdIfNecessary()
: [boardId];

return dispatch(verifyAndUpsertCommunicator(updatedCommunicatorData));
};
}
10 changes: 10 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ export const DEFAULT_BOARDS = {
advanced: boards.advanced,
picSeePal: picSeePal
};
let advancedCopy = JSON.parse(JSON.stringify(DEFAULT_BOARDS.advanced));
let picSeePalCopy = JSON.parse(JSON.stringify(DEFAULT_BOARDS.picSeePal));

export const ALL_DEFAULT_BOARDS = [...advancedCopy, ...picSeePalCopy];

export const SHORT_ID_MAX_LENGTH = 14;

export const isRemoteIdChecker = id => {
return !(id.length < SHORT_ID_MAX_LENGTH);
};

export const dataURLtoFile = (dataurl, filename, checkExtension = false) => {
// https://stackoverflow.com/a/38936042
Expand Down
Loading