Skip to content

Commit 6aa6643

Browse files
authored
RI-6932: create btn open oauth modal (#262)
* when add database called from the database list - open the oauth social dialog - add new vscode actions * close modal when successfully create db and X button on the toast notification is clicked
1 parent 61a837a commit 6aa6643

File tree

8 files changed

+95
-11
lines changed

8 files changed

+95
-11
lines changed

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ export async function activate(context: vscode.ExtensionContext) {
8888
viewId: ViewId.AddDatabase,
8989
handleMessage: (message) => handleMessage(message),
9090
message: args,
91+
}).postMessage({
92+
action: 'OpenOAuthSsoDialog',
93+
data: { source: args?.source, ssoFlow: args?.ssoFlow },
9194
})
9295
}),
9396

src/utils/handleMessage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const handleMessage = async (message: any = {}) => {
4343
await setUIStorageField('ssoFlow', message.data?.ssoFlow)
4444
}
4545

46-
vscode.commands.executeCommand('RedisForVSCode.addDatabase')
46+
vscode.commands.executeCommand('RedisForVSCode.addDatabase', message.data)
4747
break
4848
case 'CloseAddDatabase':
4949
console.debug('RedisForVSCode.addDatabaseClose, ', message.data)

src/webviews/src/constants/vscode/vscode.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export enum VscodeMessageAction {
3232
OAuthCallback = 'OAuthCallback',
3333
RefreshDatabases = 'RefreshDatabases',
3434
OpenExternalUrl = 'OpenExternalUrl',
35+
OpenOAuthSsoDialog = 'OpenOAuthSsoDialog',
3536
}
3637

3738
export enum VscodeStateItem {

src/webviews/src/index.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
fetchEditedDatabase,
88
fetchCerts,
99
useDatabasesStore,
10+
useOAuthStore,
1011
} from 'uiSrc/store'
1112
import { Config } from 'uiSrc/modules'
1213
import { AppRoutes } from 'uiSrc/Routes'
@@ -88,6 +89,14 @@ document.addEventListener('DOMContentLoaded', () => {
8889
case VscodeMessageAction.OAuthCallback:
8990
processOauthCallback(message.data as CloudAuthResponse)
9091
break
92+
case VscodeMessageAction.OpenOAuthSsoDialog:
93+
const { source, ssoFlow } = message.data ?? {}
94+
if (ssoFlow && source) {
95+
const state = useOAuthStore.getState()
96+
state.setSSOFlow(ssoFlow)
97+
state.setSocialDialogState(source)
98+
}
99+
break
91100
default:
92101
break
93102
}

src/webviews/src/interfaces/vscode/api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AllKeyTypes, KeyTypes, OAuthSocialAction, OAuthStrategy, SelectedKeyActionType, VscodeMessageAction } from 'uiSrc/constants'
1+
import { AllKeyTypes, KeyTypes, OAuthSocialAction, OAuthSocialSource, OAuthStrategy, SelectedKeyActionType, VscodeMessageAction } from 'uiSrc/constants'
22
import { Database } from 'uiSrc/store'
33
import { AppInfoStore, GetAppSettingsResponse } from 'uiSrc/store/hooks/use-app-info-store/interface'
44
import { CloudAuthResponse } from 'uiSrc/modules/oauth/interfaces'
@@ -63,9 +63,11 @@ export interface DatabaseAction {
6363
action: VscodeMessageAction.RefreshDatabases
6464
| VscodeMessageAction.CloseAddDatabase
6565
| VscodeMessageAction.OpenAddDatabase
66+
| VscodeMessageAction.OpenOAuthSsoDialog
6667
data?: {
6768
database?: Database
6869
ssoFlow?: OAuthSocialAction
70+
source?: OAuthSocialSource
6971
}
7072
}
7173
export interface CliAction {
Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,93 @@
11
import React from 'react'
22
import { sendEventTelemetry, TelemetryEvent } from 'uiSrc/utils'
33
import * as utils from 'uiSrc/utils'
4-
import { OAuthSocialSource } from 'uiSrc/constants'
4+
import { OAuthSocialAction, OAuthSocialSource, VscodeMessageAction } from 'uiSrc/constants'
55
import { initialOAuthState, useOAuthStore } from 'uiSrc/store'
6-
import { cleanup, fireEvent, render } from 'testSrc/helpers'
6+
import { vscodeApi } from 'uiSrc/services'
7+
import { cleanup, fireEvent, render, screen } from 'testSrc/helpers'
78
import OAuthCreateFreeDb from './OAuthCreateFreeDb'
9+
import OAuthSsoDialog from '../oauth-sso-dialog'
810

911
vi.spyOn(utils, 'sendEventTelemetry')
12+
vi.spyOn(vscodeApi, 'postMessage')
1013

1114
beforeEach(() => {
1215
useOAuthStore.setState({
1316
...initialOAuthState,
14-
source: 'source',
17+
source: null,
18+
ssoFlow: undefined,
1519
})
1620
cleanup()
1721
vi.resetAllMocks()
1822
})
1923

20-
describe('OAuthConnectFreeDb', () => {
24+
describe('OAuthCreateFreeDb', () => {
2125
it('should render if there is a free cloud db', () => {
22-
const { queryByTestId } = render(<OAuthCreateFreeDb source={OAuthSocialSource.ListOfDatabases}/>)
26+
const { queryByTestId } = render(<OAuthCreateFreeDb source={OAuthSocialSource.AddDbForm}/>)
2327
expect(queryByTestId('create-free-db-btn')).toBeInTheDocument()
2428
})
2529

2630
it('should send telemetry after click on connect btn', async () => {
27-
const { queryByTestId } = render(<OAuthCreateFreeDb source={OAuthSocialSource.ListOfDatabases} />)
31+
const { queryByTestId } = render(<OAuthCreateFreeDb source={OAuthSocialSource.DatabasesList} />)
2832

2933
fireEvent.click(queryByTestId('create-free-db-btn') as HTMLButtonElement)
3034

3135
expect(sendEventTelemetry).toBeCalledWith({
3236
event: TelemetryEvent.CLOUD_FREE_DATABASE_CLICKED,
3337
eventData: {
34-
source: OAuthSocialSource.ListOfDatabases,
38+
source: OAuthSocialSource.DatabasesList,
3539
},
3640
})
3741
})
42+
43+
it('should set the propper state variables when action is triggered with with valid ssoFlow and source', () => {
44+
const { queryByTestId } = render(<OAuthCreateFreeDb source={OAuthSocialSource.DatabasesList} compressed={true}/>)
45+
render(<OAuthSsoDialog />)
46+
47+
const compressedCreateBtn = queryByTestId('create-free-db-btn')
48+
expect(compressedCreateBtn).toBeInTheDocument()
49+
50+
fireEvent.click(compressedCreateBtn as HTMLButtonElement)
51+
52+
expect(vscodeApi.postMessage).toBeCalledWith({
53+
action: VscodeMessageAction.OpenAddDatabase,
54+
data: {
55+
ssoFlow: OAuthSocialAction.Create,
56+
source: OAuthSocialSource.DatabasesList,
57+
},
58+
})
59+
})
60+
61+
it('should set the propper state variables when action is triggered with source null', () => {
62+
const { queryByTestId } = render(<OAuthCreateFreeDb source={null} compressed={true}/>)
63+
render(<OAuthSsoDialog />)
64+
65+
const compressedCreateBtn = queryByTestId('create-free-db-btn')
66+
expect(compressedCreateBtn).toBeInTheDocument()
67+
68+
fireEvent.click(compressedCreateBtn as HTMLButtonElement)
69+
70+
expect(vscodeApi.postMessage).toBeCalledWith({
71+
action: VscodeMessageAction.OpenAddDatabase,
72+
data: {
73+
ssoFlow: OAuthSocialAction.Create,
74+
source: null,
75+
},
76+
})
77+
})
78+
79+
it('should open auth sso dialog when non compressed button is clicked', () => {
80+
const { queryByTestId } = render(<OAuthCreateFreeDb source={OAuthSocialSource.DatabasesList} compressed={false}/>)
81+
82+
// component is initialized in the document, but the state is not updated yet to show the dialog
83+
render(<OAuthSsoDialog />)
84+
expect(screen.queryByTestId('social-oauth-dialog')).not.toBeInTheDocument()
85+
86+
const regularCreateBtn = queryByTestId('create-free-db-btn')
87+
expect(regularCreateBtn).toBeInTheDocument()
88+
89+
fireEvent.click(regularCreateBtn as HTMLButtonElement)
90+
91+
expect(screen.queryByTestId('social-oauth-dialog')).toBeInTheDocument()
92+
})
3893
})

src/webviews/src/modules/oauth/oauth-create-free-db/OAuthCreateFreeDb.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const OAuthCreateFreeDb = ({ source, compressed }: Props) => {
3232
action: VscodeMessageAction.OpenAddDatabase,
3333
data: {
3434
ssoFlow: OAuthSocialAction.Create,
35+
source,
3536
},
3637
})
3738

src/webviews/src/modules/oauth/oauth-jobs/OAuthJobs.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useEffect } from 'react'
22
import { get } from 'lodash'
33
import { useShallow } from 'zustand/react/shallow'
44

5-
import { CloudJobStatus, CloudJobName, ApiStatusCode, StorageItem, CustomErrorCodes, CloudJobStep, VscodeMessageAction } from 'uiSrc/constants'
5+
import { CloudJobStatus, CloudJobName, ApiStatusCode, StorageItem, CustomErrorCodes, CloudJobStep, VscodeMessageAction, OAuthSocialSource } from 'uiSrc/constants'
66
import { parseCustomError, TelemetryEvent, sendEventTelemetry, getApiErrorMessage } from 'uiSrc/utils'
77
import { showInfinityToast, removeInfinityToast, showErrorInfinityToast } from 'uiSrc/utils/notifications/toasts'
88
import { localStorageService, vscodeApi } from 'uiSrc/services'
@@ -21,6 +21,7 @@ const OAuthJobs = () => {
2121
setSSOFlow,
2222
setJob,
2323
setSocialDialogState,
24+
source,
2425
} = useOAuthStore(useShallow((state) => ({
2526
status: state.job?.status,
2627
jobName: state.job?.name,
@@ -31,6 +32,7 @@ const OAuthJobs = () => {
3132
setSSOFlow: state.setSSOFlow,
3233
setJob: state.setJob,
3334
setSocialDialogState: state.setSocialDialogState,
35+
source: state.source,
3436
})))
3537

3638
const onConnect = () => {
@@ -39,6 +41,14 @@ const OAuthJobs = () => {
3941
})
4042
}
4143

44+
const closeAddDatabasePanel = () => {
45+
if (source === OAuthSocialSource.DatabasesList) {
46+
vscodeApi.postMessage({
47+
action: VscodeMessageAction.CloseAddDatabase,
48+
})
49+
}
50+
}
51+
4252
useEffect(() => {
4353
switch (status) {
4454
case CloudJobStatus.Running:
@@ -49,7 +59,10 @@ const OAuthJobs = () => {
4959

5060
case CloudJobStatus.Finished:
5161

52-
showInfinityToast(INFINITE_MESSAGES.SUCCESS_CREATE_DB(jobName, onConnect)?.Inner)
62+
showInfinityToast(INFINITE_MESSAGES.SUCCESS_CREATE_DB(
63+
jobName, onConnect)?.Inner,
64+
{ onClose: closeAddDatabasePanel },
65+
)
5366

5467
setJob({
5568
id: '',

0 commit comments

Comments
 (0)