Skip to content

Commit

Permalink
Merge pull request #505 from cboard-org/fix-save-single
Browse files Browse the repository at this point in the history
fix for edition of remote boards
  • Loading branch information
martinbedouret authored Oct 29, 2019
2 parents 45664c0 + fb5e231 commit 061de85
Showing 1 changed file with 98 additions and 87 deletions.
185 changes: 98 additions & 87 deletions src/components/Board/Board.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
previousBoard,
createBoard,
updateBoard,
switchBoard,
createTile,
deleteTiles,
editTiles,
Expand Down Expand Up @@ -155,9 +156,7 @@ export class BoardContainer extends Component {
isSelecting: false,
isLocked: true,
tileEditorOpen: false,
translatedBoard: null,
newOwnBoard: null,
isApiRequired: false
translatedBoard: null
};

async componentWillMount() {
Expand Down Expand Up @@ -367,12 +366,13 @@ export class BoardContainer extends Component {
};

handleEditTileEditorSubmit = tiles => {
const { board, editTiles } = this.props;
const { board, editTiles, userData } = this.props;
editTiles(tiles, board.id);
// Loggedin user?
if ('name' in userData && 'email' in userData) {
this.handleApiUpdates(null, null, tiles);
}
this.toggleSelectMode();
this.setState({
isApiRequired: true
});
};

handleAddTileEditorSubmit = tile => {
Expand All @@ -381,6 +381,7 @@ export class BoardContainer extends Component {
createTile,
board,
createBoard,
switchBoard,
addBoardCommunicator,
history
} = this.props;
Expand All @@ -399,26 +400,13 @@ export class BoardContainer extends Component {
if (tile.type !== 'board') {
createTile(tile, board.id);
} else {
switchBoard(boardData.id);
history.replace(`/board/${boardData.id}`, []);
}

// Loggedin user?
if ('name' in userData && 'email' in userData) {
this.setState({
isApiRequired: true
});
//update new board to be an own board
if (tile.loadBoard) {
this.setState({
newOwnBoard: {
...boardData,
author: userData.name,
email: userData.email,
locale: userData.locale,
caption: tile.image
}
});
}
this.handleApiUpdates(tile);
}
};

Expand Down Expand Up @@ -517,12 +505,13 @@ export class BoardContainer extends Component {
showNotification,
board,
userData,
communicator
communicator,
deleteBoardCommunicator,
deleteApiBoard
} = this.props;
deleteTiles(this.state.selectedTileIds, board.id);
this.setState({
selectedTileIds: [],
isApiRequired: true
selectedTileIds: []
});
showNotification(intl.formatMessage(messages.tilesDeleted));

Expand All @@ -537,8 +526,8 @@ export class BoardContainer extends Component {
board.tiles[j].loadBoard.length > 14
) {
if (board.tiles[j].loadBoard !== communicator.rootBoard) {
this.props.deleteBoardCommunicator(board.tiles[j].loadBoard);
this.props.deleteApiBoard(board.tiles[j].loadBoard);
deleteBoardCommunicator(board.tiles[j].loadBoard);
deleteApiBoard(board.tiles[j].loadBoard);
} else {
showNotification(
intl.formatMessage(messages.rootBoardNotDeleted)
Expand All @@ -547,7 +536,9 @@ export class BoardContainer extends Component {
}
}
}
this.handleApiUpdates(null, this.state.selectedTileIds, null);
}
this.toggleSelectMode();
};

handleLockNotify = countdown => {
Expand Down Expand Up @@ -594,88 +585,108 @@ export class BoardContainer extends Component {

handleUpdateBoard = board => {
this.props.replaceBoard(this.props.board, board);
const { userData, communicator } = this.props;
};

handleApiUpdates = (
tile = null,
deletedTilesiIds = null,
editedTiles = null
) => {
const {
userData,
communicator,
board,
upsertCommunicator,
changeCommunicator,
updateApiObjectsNoChild,
updateApiObjects,
historyRemovePreviousBoard,
updateBoard
} = this.props;

// Loggedin user?
if (this.state.isApiRequired && 'name' in userData && 'email' in userData) {
if ('name' in userData && 'email' in userData) {
this.setState({
isSaving: true,
isApiRequired: false
isSaving: true
});

var createCommunicator = false;
var createParentBoard = false;
var createChildBoard = false;
var childBoardData = null;

const communicatorData = {
...communicator,
author: userData.name,
email: userData.email,
id: shortid.generate(),
boards: []
};
let uTiles = [];
if (deletedTilesiIds) {
uTiles = board.tiles.filter(
cTile => !deletedTilesiIds.includes(cTile.id)
);
}
if (editedTiles) {
uTiles = board.tiles.map(
cTile => editedTiles.find(s => s.id === cTile.id) || cTile
);
}
if (tile) {
uTiles = [...board.tiles, tile];
}

const parentBoardData = {
...board,
tiles: uTiles,
author: userData.name,
email: userData.email,
locale: userData.locale,
isPublic: false
};

//check if user has an own communicator
let communicatorData = { ...communicator };
if (communicator.email !== userData.email) {
this.props.upsertCommunicator(communicatorData);
this.props.changeCommunicator(communicatorData.id);
//need to create a new communicator
communicatorData = {
...communicator,
author: userData.name,
email: userData.email,
id: shortid.generate()
};
upsertCommunicator(communicatorData);
changeCommunicator(communicatorData.id);
createCommunicator = true;
}
//check for a new own board
if (this.state.newOwnBoard) {
childBoardData = { ...this.state.newOwnBoard };
this.setState({
newOwnBoard: null
});
if (tile && tile.loadBoard) {
const boardData = {
id: tile.loadBoard,
name: tile.label,
nameKey: tile.labelKey,
hidden: false,
tiles: [],
isPublic: false,
author: userData.name,
email: userData.email,
locale: userData.locale,
caption: tile.image
};
childBoardData = { ...boardData };
createChildBoard = true;
this.props.updateBoard(childBoardData);
this.props.addBoardCommunicator(childBoardData.id);
updateBoard(childBoardData);
}
//check if we have to create a copy of the parent
if (board.isPublic && board.email !== userData.email) {
if (communicatorData.rootBoard === board.id) {
parentBoardData.id = shortid.generate();
this.props.createBoard(parentBoardData);
this.props.addBoardCommunicator(parentBoardData.id);
createParentBoard = true;
//parent board needs to be the root board
communicatorData.rootBoard = parentBoardData.id;
communicatorData.activeBoardId = parentBoardData.id;
this.props.upsertCommunicator(communicatorData);
} else {
//update the parent
this.props.updateBoard(parentBoardData);
this.props.addBoardCommunicator(parentBoardData.id);
if (parentBoardData.id.length < 15) {
createParentBoard = true;
}
//if it is a new communicator , need to set as root
if (createCommunicator === true) {
communicatorData.rootBoard = parentBoardData.id;
communicatorData.activeBoardId = parentBoardData.id;
this.props.upsertCommunicator(communicatorData);
}
}
if (parentBoardData.id.length < 14) {
createParentBoard = true;
} else {
//update the parent
updateBoard(parentBoardData);
}
//api updates
if (!createChildBoard) {
this.props
.updateApiObjectsNoChild(
parentBoardData,
createCommunicator,
createParentBoard
)
updateApiObjectsNoChild(
parentBoardData,
createCommunicator,
createParentBoard
)
.then(parentBoardId => {
if (createParentBoard) {
this.props.historyRemovePreviousBoard(parentBoardId);
historyRemovePreviousBoard(parentBoardId);
}
this.props.history.replace(`/board/${parentBoardId}`);
this.setState({ isSaving: false });
Expand All @@ -684,16 +695,15 @@ export class BoardContainer extends Component {
this.setState({ isSaving: false });
});
} else {
this.props
.updateApiObjects(
childBoardData,
parentBoardData,
createCommunicator,
createParentBoard
)
updateApiObjects(
childBoardData,
parentBoardData,
createCommunicator,
createParentBoard
)
.then(parentBoardId => {
if (createParentBoard) {
this.props.historyRemovePreviousBoard(parentBoardId);
historyRemovePreviousBoard(parentBoardId);
}
this.props.history.replace(`/board/${parentBoardId}`);
this.setState({ isSaving: false });
Expand Down Expand Up @@ -838,6 +848,7 @@ const mapDispatchToProps = {
historyRemovePreviousBoard,
createBoard,
updateBoard,
switchBoard,
createTile,
deleteTiles,
editTiles,
Expand Down

0 comments on commit 061de85

Please sign in to comment.