Skip to content

Commit c374ecf

Browse files
committed
add forceSave option to connect action creator
1 parent f2a74f0 commit c374ecf

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
lines changed

packages/compass-connections/src/stores/connections-store-redux.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ type ConnectionAutoconnectCheckAction = {
313313
type ConnectionAttemptStartAction = {
314314
type: ActionTypes.ConnectionAttemptStart;
315315
connectionInfo: ConnectionInfo;
316+
options: {
317+
forceSave: boolean;
318+
};
316319
};
317320

318321
type ConnectionAttemptSuccessAction = {
@@ -823,10 +826,13 @@ const reducer: Reducer<State, Action> = (state = INITIAL_STATE, action) => {
823826
state.connections,
824827
action.connectionInfo.id,
825828
{
826-
...(isNewConnection(state, action.connectionInfo.id)
829+
...(isNewConnection(state, action.connectionInfo.id) ||
830+
action.options.forceSave
827831
? {
828-
// For new connections, update the state with new info right
829-
// away (we will also save it to the storage at the end)
832+
// For new connections or when we're forcing a
833+
// save (the Save & Connect button), update the state with new
834+
// info right away (we will also save it to the storage at the
835+
// end)
830836
info: action.connectionInfo,
831837
}
832838
: {
@@ -1464,6 +1470,35 @@ export const connect = (
14641470
| ConnectionAttemptSuccessAction
14651471
| ConnectionAttemptCancelledAction
14661472
| OidcNotifyDeviceAuthAction
1473+
> => {
1474+
return connectWithOptions(connectionInfo, { forceSave: false });
1475+
};
1476+
1477+
export const saveAndConnect = (
1478+
connectionInfo: ConnectionInfo
1479+
): ConnectionsThunkAction<
1480+
Promise<void>,
1481+
| ConnectionAttemptStartAction
1482+
| ConnectionAttemptErrorAction
1483+
| ConnectionAttemptSuccessAction
1484+
| ConnectionAttemptCancelledAction
1485+
| OidcNotifyDeviceAuthAction
1486+
> => {
1487+
return connectWithOptions(connectionInfo, { forceSave: true });
1488+
};
1489+
1490+
const connectWithOptions = (
1491+
connectionInfo: ConnectionInfo,
1492+
options: {
1493+
forceSave: boolean;
1494+
}
1495+
): ConnectionsThunkAction<
1496+
Promise<void>,
1497+
| ConnectionAttemptStartAction
1498+
| ConnectionAttemptErrorAction
1499+
| ConnectionAttemptSuccessAction
1500+
| ConnectionAttemptCancelledAction
1501+
| OidcNotifyDeviceAuthAction
14671502
> => {
14681503
return async (
14691504
dispatch,
@@ -1516,6 +1551,7 @@ export const connect = (
15161551
dispatch({
15171552
type: ActionTypes.ConnectionAttemptStart,
15181553
connectionInfo,
1554+
options: { forceSave: options.forceSave },
15191555
});
15201556

15211557
track(

packages/compass-connections/src/stores/connections-store.spec.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,37 @@ describe('useConnections', function () {
323323
}
324324
});
325325

326+
describe('#saveAndConnect', function () {
327+
it('saves the connection options before connecting', async function () {
328+
const { result, track, connectionStorage } = renderHookWithConnections(
329+
useConnections,
330+
{
331+
connections: mockConnections,
332+
preferences: defaultPreferences,
333+
}
334+
);
335+
336+
const updatedConnection = {
337+
...mockConnections[0],
338+
savedConnectionType: 'recent' as const,
339+
};
340+
341+
await result.current.saveAndConnect(updatedConnection);
342+
343+
await waitFor(() => {
344+
expect(track.getCall(1).firstArg).to.eq('New Connection');
345+
});
346+
347+
expect(
348+
await connectionStorage.load({ id: updatedConnection.id })
349+
).to.have.property('savedConnectionType', 'recent');
350+
351+
expect(
352+
screen.getByText(`Connected to ${mockConnections[0].favorite.name}`)
353+
).to.exist;
354+
});
355+
});
356+
326357
describe('#disconnect', function () {
327358
it('disconnect even if connection is in progress cleaning up progress toasts', async function () {
328359
const { result, track } = renderHookWithConnections(useConnections, {

packages/compass-connections/src/stores/connections-store.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function useConnections(): {
1616
state: State;
1717

1818
connect: (connectionInfo: ConnectionInfo) => Promise<void>;
19+
saveAndConnect: (connectionInfo: ConnectionInfo) => Promise<void>;
1920
disconnect: (connectionId: string) => void;
2021

2122
createNewConnection: () => void;
@@ -53,6 +54,7 @@ export function useConnections(): {
5354
};
5455
const {
5556
connect,
57+
saveAndConnect,
5658
disconnect,
5759
createNewConnection,
5860
editConnection,
@@ -67,6 +69,7 @@ export function useConnections(): {
6769
return {
6870
state,
6971
connect,
72+
saveAndConnect,
7073
disconnect,
7174
createNewConnection,
7275
editConnection,

packages/compass-connections/src/stores/store-context.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
import {
1919
cancelEditConnection,
2020
connect as connectionsConnect,
21+
saveAndConnect,
2122
connectionsEventEmitter,
2223
createNewConnection,
2324
disconnect,
@@ -87,6 +88,9 @@ function getConnectionsActions(dispatch: ConnectionsStore['dispatch']) {
8788
connect: (connectionInfo: ConnectionInfo) => {
8889
return dispatch(connectionsConnect(connectionInfo));
8990
},
91+
saveAndConnect: (connectionInfo: ConnectionInfo) => {
92+
return dispatch(saveAndConnect(connectionInfo));
93+
},
9094
disconnect: (connectionId: ConnectionId) => {
9195
return dispatch(disconnect(connectionId));
9296
},

packages/compass-sidebar/src/components/multiple-connections/sidebar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export function MultipleConnectionSidebar({
109109
const connectionsWithStatus = useConnectionsWithStatus();
110110
const {
111111
connect,
112+
saveAndConnect,
112113
disconnect,
113114
createNewConnection,
114115
editConnection,
@@ -267,8 +268,7 @@ export function MultipleConnectionSidebar({
267268
disableEditingConnectedConnection
268269
? undefined
269270
: (connectionInfo) => {
270-
void saveEditedConnection(connectionInfo);
271-
void connect(connectionInfo);
271+
void saveAndConnect(connectionInfo);
272272
}
273273
}
274274
/>

0 commit comments

Comments
 (0)