Skip to content

Commit

Permalink
Merge pull request #1686 from RodriSanchez1/fix/createUserCommunicato…
Browse files Browse the repository at this point in the history
…rOnChangeDefaultBoard

Create new  user communicator on changeDefaultBoard
  • Loading branch information
RodriSanchez1 authored May 27, 2024
2 parents aa67328 + a9cd40c commit 68ff9ca
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
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

0 comments on commit 68ff9ca

Please sign in to comment.