Skip to content

Commit

Permalink
Merge remote-tracking branch 'cboard-org/master' into feature/add-onl…
Browse files Browse the repository at this point in the history
…y-necessary-default-boards-on-login
  • Loading branch information
tomivm committed Jul 3, 2024
2 parents 9de64fe + 42c1242 commit 7e22997
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 73 deletions.
5 changes: 4 additions & 1 deletion src/components/Account/Login/Login.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,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 Down
57 changes: 31 additions & 26 deletions src/components/Board/Board.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -1202,9 +1202,23 @@ export class BoardContainer extends Component {
}

handleCopyRemoteBoard = async () => {
const { intl, showNotification } = this.props;
const { intl, showNotification, history, switchBoard } = this.props;
try {
await this.createBoardsRecursively(this.state.copyPublicBoard);
const copiedBoard = await this.createBoardsRecursively(
this.state.copyPublicBoard
);
if (!copiedBoard?.id) {
throw new Error('Board not copied correctly');
}
switchBoard(copiedBoard.id);
history.replace(`/board/${copiedBoard.id}`, []);
const translatedBoard = this.translateBoard(copiedBoard);
this.setState({
translatedBoard,
isSaving: false,
copyPublicBoard: false,
blockedPrivateBoard: false
});
showNotification(intl.formatMessage(messages.boardCopiedSuccessfully));
} catch (err) {
console.log(err.message);
Expand All @@ -1215,9 +1229,7 @@ export class BoardContainer extends Component {
async createBoardsRecursively(board, records) {
const {
createBoard,
switchBoard,
addBoardCommunicator,
history,
communicator,
userData,
updateApiObjectsNoChild,
Expand All @@ -1228,13 +1240,13 @@ export class BoardContainer extends Component {

//prevent shit
if (!board) {
return;
return null;
}
if (records) {
//get the list of next boards in records
let nextBoardsRecords = records.map(entry => entry.next);
if (nextBoardsRecords.includes(board.id)) {
return;
return null;
}
}

Expand Down Expand Up @@ -1264,6 +1276,13 @@ export class BoardContainer extends Component {
addBoardCommunicator(newBoard.id);
}

if (!records) {
records = [{ prev: board.id, next: newBoard.id }];
} else {
records.push({ prev: board.id, next: newBoard.id });
}
this.updateBoardReferences(board, newBoard, records);

// Loggedin user?
if ('name' in userData && 'email' in userData) {
this.setState({
Expand All @@ -1280,43 +1299,29 @@ export class BoardContainer extends Component {
console.log(err.message);
}
}
if (!records) {
records = [{ prev: board.id, next: newBoard.id }];
switchBoard(newBoard.id);
history.replace(`/board/${newBoard.id}`, []);
const translatedBoard = this.translateBoard(newBoard);
this.setState({
translatedBoard,
isSaving: false,
copyPublicBoard: false,
blockedPrivateBoard: false
});
} else {
records.push({ prev: board.id, next: newBoard.id });
}
this.updateBoardReferences(board, newBoard, records);

if (board.tiles.length < 1) {
return;
return newBoard;
}

//return condition
board.tiles.forEach(async tile => {
for (const tile of board.tiles) {
if (tile.loadBoard && !tile.linkedBoard) {
try {
const nextBoard = await API.getBoard(tile.loadBoard);
this.createBoardsRecursively(nextBoard, records);
await this.createBoardsRecursively(nextBoard, records);
} catch (err) {
if (err.response.status === 404) {
//look for this board in available boards
const localBoard = boards.find(b => b.id === tile.loadBoard);
if (localBoard) {
this.createBoardsRecursively(localBoard, records);
await this.createBoardsRecursively(localBoard, records);
}
}
}
}
});
}
return newBoard;
}

updateBoardReferences(board, newBoard, records) {
Expand Down
116 changes: 104 additions & 12 deletions src/components/Communicator/Communicator.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ import {
UPDATE_API_COMMUNICATOR_STARTED,
GET_API_MY_COMMUNICATORS_SUCCESS,
GET_API_MY_COMMUNICATORS_FAILURE,
GET_API_MY_COMMUNICATORS_STARTED
GET_API_MY_COMMUNICATORS_STARTED,
SYNC_COMMUNICATORS
} from './Communicator.constants';
import { defaultCommunicatorID } from './Communicator.reducer';
import API from '../../api';
import shortid from 'shortid';
import { removeBoardsFromList } from '../Board/Board.actions';
import { removeBoardsFromList, switchBoard } from '../Board/Board.actions';
import { ALL_DEFAULT_BOARDS } from '../../helpers';
import moment from 'moment';
import history from './../../history';

export function importCommunicator(communicator) {
return {
Expand Down Expand Up @@ -232,10 +235,17 @@ export function verifyAndUpsertCommunicator(
*/

export function getApiMyCommunicators() {
return (dispatch, getState) => {
return async (dispatch, getState) => {
dispatch(getApiMyCommunicatorsStarted());
return API.getCommunicators()
.then(res => {
try {
const res = await API.getCommunicators();
dispatch(getApiMyCommunicatorsSuccess(res));
if (res?.data && res.data.length) {
try {
await dispatch(syncCommunicators(res.data));
} catch (e) {
console.error(e);
}
const activeCommunicator =
res.data.find(
communicator =>
Expand All @@ -248,13 +258,13 @@ export function getApiMyCommunicators() {
activeCommunicator.rootBoard
)
);
dispatch(getApiMyCommunicatorsSuccess(res));
return res;
})
.catch(err => {
dispatch(getApiMyCommunicatorsFailure(err.message));
throw new Error(err.message);
});
}

return res;
} catch (err) {
dispatch(getApiMyCommunicatorsFailure(err.message));
throw new Error(err.message);
}
};
}

Expand Down Expand Up @@ -306,6 +316,88 @@ export function updateDefaultBoardsIncluded(boardAlreadyIncludedData) {
};
}

export function syncCommunicators(remoteCommunicators) {
const reconcileCommunicators = (local, remote) => {
if (local.lastEdited && remote.lastEdited) {
if (moment(local.lastEdited).isAfter(remote.lastEdited)) {
return local;
}
if (moment(local.lastEdited).isBefore(remote.lastEdited)) {
return remote;
}
if (moment(local.lastEdited).isSame(remote.lastEdited)) {
return remote;
}
}
return remote;
};
const getActiveCommunicator = getState => {
return getState().communicator.communicators.find(
c => c.id === getState().communicator.activeCommunicatorId
);
};

return async (dispatch, getState) => {
const localCommunicators = getState().communicator.communicators;
const updatedCommunicators = [...localCommunicators];

for (const remote of remoteCommunicators) {
const localIndex = localCommunicators.findIndex(
local => local.id === remote.id
);

if (localIndex !== -1) {
// If the communicator exists locally, reconcile the two
const reconciled = reconcileCommunicators(
localCommunicators[localIndex],
remote
);
if (reconciled === localCommunicators[localIndex]) {
// Local is more recent, update the server
try {
const res = await dispatch(
updateApiCommunicator(localCommunicators[localIndex])
);
updatedCommunicators[localIndex] = res;
} catch (e) {
console.error(e);
}
} else {
updatedCommunicators[localIndex] = reconciled;
}
} else {
// If the communicator does not exist locally, add it
updatedCommunicators.push(remote);
}
}

const activeCommunicatorId = getActiveCommunicator(getState).id ?? null;
const lastRemoteSavedCommunicatorId = remoteCommunicators[0].id ?? null; //The last communicator saved on the server
const needToChangeActiveCommunicator =
activeCommunicatorId !== lastRemoteSavedCommunicatorId &&
updatedCommunicators.length &&
lastRemoteSavedCommunicatorId &&
updatedCommunicators.findIndex(
communicator => communicator.id === lastRemoteSavedCommunicatorId
) !== -1;

dispatch({
type: SYNC_COMMUNICATORS,
communicators: updatedCommunicators,
activeCommunicatorId: needToChangeActiveCommunicator
? lastRemoteSavedCommunicatorId
: activeCommunicatorId
});

if (needToChangeActiveCommunicator) {
const newActiveCommunicator = getActiveCommunicator(getState);
const rootBoard = newActiveCommunicator.rootBoard;
dispatch(switchBoard(rootBoard));
history.replace(rootBoard);
}
};
}

export function concatDefaultBoardIdToBlacklist(boardId) {
const getActiveCommunicator = getState => {
return getState().communicator.communicators.find(
Expand Down
1 change: 1 addition & 0 deletions src/components/Communicator/Communicator.constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ export const GET_API_MY_COMMUNICATORS_FAILURE =
'cboard/Communicator/GET_API_MY_COMMUNICATORS_FAILURE';
export const GET_API_MY_COMMUNICATORS_STARTED =
'cboard/Communicator/GET_API_MY_COMMUNICATORS_STARTED';
export const SYNC_COMMUNICATORS = 'Communicator/SYNC_COMMUNICATORS';
Loading

0 comments on commit 7e22997

Please sign in to comment.