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

Create new user communicator on changeDefaultBoard #1686

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 28 additions & 10 deletions src/components/Board/Board.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ import {
upsertCommunicator,
getApiMyCommunicators,
editCommunicator,
upsertApiCommunicator,
updateDefaultBoardsIncluded,
addDefaultBoardIncluded
addDefaultBoardIncluded,
createCommunicator,
changeCommunicator
} 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;

Expand Down Expand Up @@ -92,7 +96,27 @@ export function changeDefaultBoard(selectedBoardNameOnJson) {
const board = DEFAULT_BOARDS[selectedBoardNameOnJson];
const BOARD_ALREADY_INCLUDED_NAME = 'advanced';

const activeCommunicator = getActiveCommunicator(getState);
const checkUserCommunicator = () => {
const userData = getState().app?.userData;
const activeCommunicator = getActiveCommunicator(getState);

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;
}
return activeCommunicator;
};

const activeCommunicator = checkUserCommunicator();

const fallbackInitialDefaultBoardsIncluded = activeCommunicator => {
const oldUserHomeBoard = activeCommunicator.rootBoard;
Expand Down Expand Up @@ -171,14 +195,8 @@ export function changeDefaultBoard(selectedBoardNameOnJson) {
};

dispatch(editCommunicator(communicatorWithRootBoardReplaced));

try {
if (userData?.role)
await dispatch(
updateApiCommunicator(communicatorWithRootBoardReplaced)
);
} catch (error) {
console.error('error', error);
if (userData?.role) {
dispatch(upsertApiCommunicator(communicatorWithRootBoardReplaced));
}
};

Expand Down
25 changes: 24 additions & 1 deletion src/components/Communicator/Communicator.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
GET_API_MY_COMMUNICATORS_FAILURE,
GET_API_MY_COMMUNICATORS_STARTED
} from './Communicator.constants';

import { defaultCommunicatorID } from './Communicator.reducer';
import API from '../../api';

export function importCommunicator(communicator) {
Expand Down Expand Up @@ -50,6 +50,29 @@ export function upsertCommunicator(communicator) {
};
}

export function upsertApiCommunicator(communicator) {
return (dispatch, getState) => {
const {
communicator: { communicators }
} = getState();
const SHORT_ID_MAX_LENGTH = 14;

// If the communicator is not on the local state return
if (!communicators.find(c => c.id === communicator.id)) return;

communicator.id.length < SHORT_ID_MAX_LENGTH ||
communicator.id === defaultCommunicatorID
? dispatch(createApiCommunicator(communicator, communicator.id)).catch(
error => {
console.error(error);
}
)
: dispatch(updateApiCommunicator(communicator)).catch(error => {
console.error(error);
});
};
}

export function editCommunicator(communicator) {
return {
type: EDIT_COMMUNICATOR,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Communicator/Communicator.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from './Communicator.constants';
import { LOGIN_SUCCESS, LOGOUT } from '../Account/Login/Login.constants';

const defaultCommunicatorID = 'cboard_default';
export const defaultCommunicatorID = 'cboard_default';
const initialState = {
communicators: defaultCommunicators,
activeCommunicatorId: defaultCommunicatorID
Expand Down
Loading