Skip to content

Commit

Permalink
update to match new connection
Browse files Browse the repository at this point in the history
  • Loading branch information
paula-stacho committed Jul 10, 2024
1 parent db7c9aa commit 3d415c3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
15 changes: 10 additions & 5 deletions packages/compass-connections/src/stores/connections-store.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,8 @@ describe('use-connections hook', function () {
});
});

describe('getConnectionDuplicate', function () {
it('should return a connection duplicate', async function () {
describe('createDuplicateConnection', function () {
it('should create a connection duplicate and set it as the new active connection', async function () {
const { result } = renderHookWithContext(() =>
useConnections({
onConnected: noop,
Expand All @@ -588,10 +588,13 @@ describe('use-connections hook', function () {
});

const original = result.current.favoriteConnections[0];
const duplicate = result.current.getConnectionDuplicate(original);
result.current.createDuplicateConnection(original);

const duplicate = result.current.state.activeConnectionInfo;

expect(duplicate).to.haveOwnProperty('id');
expect(duplicate.id).not.to.equal(original.id);
expect(result.current.state.activeConnectionId).to.equal(duplicate.id);
delete original.id;
delete duplicate.id;
expect(duplicate).to.deep.equal({
Expand Down Expand Up @@ -626,11 +629,13 @@ describe('use-connections hook', function () {
});

const original = result.current.favoriteConnections[0]; // copying the original
const duplicate = result.current.getConnectionDuplicate(original);
result.current.createDuplicateConnection(original);
const duplicate = result.current.state.activeConnectionInfo;
expect(duplicate.favorite.name).to.equal(`${original.favorite.name} (2)`);

const copy = result.current.favoriteConnections[1]; // copying the copy
const duplicate2 = result.current.getConnectionDuplicate(copy);
result.current.createDuplicateConnection(copy);
const duplicate2 = result.current.state.activeConnectionInfo;
expect(duplicate2.favorite.name).to.equal(
`${original.favorite.name} (2)`
);
Expand Down
9 changes: 6 additions & 3 deletions packages/compass-connections/src/stores/connections-store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ export function useConnections({
connect: (connectionInfo: ConnectionInfo) => Promise<void>;
closeConnection: (connectionId: ConnectionInfo['id']) => Promise<void>;
createNewConnection: () => void;
createDuplicateConnection: (connectionInfo: ConnectionInfo) => void;
saveConnection: (connectionInfo: ConnectionInfo) => Promise<void>;
setActiveConnectionById: (newConnectionId: string) => void;
removeAllRecentsConnections: () => Promise<void>;
legacyDuplicateConnection: (connectionInfo: ConnectionInfo) => void;
getConnectionDuplicate: (connectionInfo: ConnectionInfo) => ConnectionInfo;
removeConnection: (connectionInfo: ConnectionInfo) => void;
} {
// TODO(COMPASS-7397): services should not be used directly in render method,
Expand Down Expand Up @@ -542,7 +542,7 @@ export function useConnections({
}
);
},
getConnectionDuplicate(connectionInfo: ConnectionInfo) {
createDuplicateConnection(connectionInfo: ConnectionInfo) {
const findConnectionByFavoriteName = (name: string) =>
[...favoriteConnections, ...recentConnections].find(
(connection: ConnectionInfo) => connection.favorite?.name === name
Expand All @@ -563,7 +563,10 @@ export function useConnections({
duplicate.favorite.name = `${name} (${copyNumber})`;
}

return duplicate;
dispatch({
type: 'new-connection',
connectionInfo: duplicate,
});
},
async removeAllRecentsConnections() {
await Promise.all(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,6 @@ export function MultipleConnectionSidebar({
const [activeConnectionsFilterRegex, setActiveConnectionsFilterRegex] =
useState<RegExp | null>(null);
const [isConnectionFormOpen, setIsConnectionFormOpen] = useState(false);
const [duplicateInProgress, setDuplicateInProgress] = useState<
ConnectionInfo | undefined
>(undefined);
useEffect(() => {
if (!isConnectionFormOpen) setDuplicateInProgress(undefined);
}, [isConnectionFormOpen]);

const [connectionInfoModalConnectionId, setConnectionInfoModalConnectionId] =
useState<string | undefined>();

Expand All @@ -215,7 +208,7 @@ export function MultipleConnectionSidebar({
removeConnection,
saveConnection,
createNewConnection,
getConnectionDuplicate,
createDuplicateConnection,
state: { activeConnectionId, activeConnectionInfo, connectionErrorMessage },
} = useConnections();

Expand Down Expand Up @@ -401,10 +394,10 @@ export function MultipleConnectionSidebar({

const onDuplicateConnection = useCallback(
(info: ConnectionInfo) => {
setDuplicateInProgress(getConnectionDuplicate(info));
createDuplicateConnection(info);
setIsConnectionFormOpen(true);
},
[setIsConnectionFormOpen, getConnectionDuplicate]
[setIsConnectionFormOpen, createDuplicateConnection]
);

const onToggleFavoriteConnectionInfo = useCallback(
Expand Down Expand Up @@ -504,7 +497,7 @@ export function MultipleConnectionSidebar({
onConnectClicked={onNewConnectionConnect}
key={activeConnectionId}
onSaveConnectionClicked={onSaveNewConnection}
initialConnectionInfo={duplicateInProgress || activeConnectionInfo}
initialConnectionInfo={activeConnectionInfo}
connectionErrorMessage={connectionErrorMessage}
preferences={formPreferences}
/>
Expand Down

0 comments on commit 3d415c3

Please sign in to comment.