Skip to content

Commit 3b2abc3

Browse files
committed
#RI-6351 - fix system theme changing
#RI-6354 - fix autodiscovery logout #RI-6350 - fix endless spinner
1 parent 6b000c1 commit 3b2abc3

File tree

9 files changed

+66
-24
lines changed

9 files changed

+66
-24
lines changed

redisinsight/ui/src/components/oauth/oauth-user-profile/OAuthUserProfile.spec.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { act, cleanup, mockedStore, render, screen,
77
mockedStoreFn,
88
} from 'uiSrc/utils/test-utils'
99

10-
import { getUserInfo, logoutUser, oauthCloudUserSelector } from 'uiSrc/slices/oauth/cloud'
10+
import { getUserInfo, logoutUser, oauthCloudUserSelector, setInitialLoadingState } from 'uiSrc/slices/oauth/cloud'
1111
import { sendEventTelemetry, TelemetryEvent } from 'uiSrc/telemetry'
1212
import { loadSubscriptionsRedisCloud, setSSOFlow } from 'uiSrc/slices/instances/cloud'
1313
import { OAuthSocialAction, OAuthSocialSource } from 'uiSrc/slices/interfaces'
@@ -24,6 +24,7 @@ jest.mock('uiSrc/slices/oauth/cloud', () => ({
2424
loading: false,
2525
data: null,
2626
error: '',
27+
initialLoading: false
2728
}),
2829
}))
2930

@@ -53,6 +54,12 @@ describe('OAuthUserProfile', () => {
5354
})
5455

5556
it('should render loading spinner initially', () => {
57+
(oauthCloudUserSelector as jest.Mock).mockReturnValueOnce({
58+
loading: false,
59+
data: null,
60+
error: '',
61+
initialLoading: true
62+
})
5663
render(<OAuthUserProfile {...mockedProps} />)
5764

5865
expect(screen.getByTestId('oath-user-profile-spinner')).toBeInTheDocument()
@@ -132,7 +139,11 @@ describe('OAuthUserProfile', () => {
132139
}
133140
})
134141

135-
expect(store.getActions()).toEqual([setSSOFlow(OAuthSocialAction.Import), loadSubscriptionsRedisCloud()]);
142+
expect(store.getActions()).toEqual([
143+
setInitialLoadingState(false),
144+
setSSOFlow(OAuthSocialAction.Import),
145+
loadSubscriptionsRedisCloud()
146+
]);
136147

137148
(sendEventTelemetry as jest.Mock).mockRestore()
138149
})
@@ -154,7 +165,7 @@ describe('OAuthUserProfile', () => {
154165

155166
fireEvent.click(screen.getByTestId('profile-account-2'))
156167

157-
expect(store.getActions()).toEqual([getUserInfo()]);
168+
expect(store.getActions()).toEqual([setInitialLoadingState(false), getUserInfo()]);
158169

159170
(sendEventTelemetry as jest.Mock).mockRestore()
160171
})
@@ -195,6 +206,6 @@ describe('OAuthUserProfile', () => {
195206

196207
fireEvent.click(screen.getByTestId('profile-logout'))
197208

198-
expect(store.getActions()).toEqual([logoutUser(), setSSOFlow()])
209+
expect(store.getActions()).toEqual([setInitialLoadingState(false), logoutUser(), setSSOFlow()])
199210
})
200211
})

redisinsight/ui/src/components/oauth/oauth-user-profile/OAuthUserProfile.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { EuiIcon, EuiLink, EuiLoadingSpinner, EuiPopover, EuiText } from '@elast
55
import cx from 'classnames'
66
import { useHistory } from 'react-router-dom'
77
import OAuthSignInButton from 'uiSrc/components/oauth/oauth-sign-in-button'
8-
import { activateAccount, logoutUserAction, oauthCloudUserSelector } from 'uiSrc/slices/oauth/cloud'
8+
import {
9+
activateAccount,
10+
logoutUserAction,
11+
oauthCloudUserSelector,
12+
setInitialLoadingState
13+
} from 'uiSrc/slices/oauth/cloud'
914
import CloudIcon from 'uiSrc/assets/img/oauth/cloud.svg?react'
1015

1116
import { getUtmExternalLink } from 'uiSrc/utils/links'
@@ -27,26 +32,25 @@ export interface Props {
2732
const OAuthUserProfile = (props: Props) => {
2833
const { source } = props
2934
const [selectingAccountId, setSelectingAccountId] = useState<number>()
30-
const { loading, error, data } = useSelector(oauthCloudUserSelector)
35+
const { error, data, initialLoading } = useSelector(oauthCloudUserSelector)
3136
const { server } = useSelector(appInfoSelector)
3237

3338
const [isProfileOpen, setIsProfileOpen] = useState(false)
3439
const [isImportLoading, setIsImportLoading] = useState(false)
35-
const [profileLoading, setProfileLoading] = useState(true)
3640

3741
const dispatch = useDispatch()
3842
const history = useHistory()
3943

4044
useEffect(() => {
41-
if (!loading && (error || data)) {
42-
setProfileLoading(false)
45+
if (data || error) {
46+
dispatch(setInitialLoadingState(false))
4347
}
44-
}, [data, loading, error])
48+
}, [data, error])
4549

4650
if (!data) {
4751
if (server?.packageType === PackageType.Mas) return null
4852

49-
if (profileLoading) {
53+
if (initialLoading) {
5054
return (
5155
<div className={styles.loadingContainer}>
5256
<EuiLoadingSpinner

redisinsight/ui/src/contexts/themeContext.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ export class ThemeProvider extends React.Component<Props> {
3939

4040
getSystemTheme = () => (window.matchMedia?.(THEME_MATCH_MEDIA_DARK)?.matches ? Theme.Dark : Theme.Light)
4141

42-
changeTheme = (themeValue: any) => {
42+
changeTheme = async (themeValue: any) => {
4343
let actualTheme = themeValue
4444
if (themeValue === Theme.System) {
4545
actualTheme = this.getSystemTheme()
4646
}
4747

48-
ipcThemeChange(themeValue)
48+
// since change theme is async need to wait to have a proper prefers-color-scheme
49+
await ipcThemeChange(themeValue)
4950

5051
this.setState({ theme: actualTheme, usingSystemTheme: themeValue === Theme.System }, () => {
5152
themeService.applyTheme(themeValue)

redisinsight/ui/src/pages/autodiscover-cloud/redis-cloud-databases/RedisCloudDatabasesPage.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const RedisCloudDatabasesPage = () => {
4848
} = useSelector(cloudSelector)
4949
const { data: userOAuthProfile } = useSelector(oauthCloudUserSelector)
5050
const currentAccountIdRef = useRef(userOAuthProfile?.id)
51+
const ssoFlowRef = useRef(ssoFlow)
5152

5253
setTitle('Redis Cloud Databases')
5354

@@ -60,7 +61,7 @@ const RedisCloudDatabasesPage = () => {
6061
}, [])
6162

6263
useEffect(() => {
63-
if (ssoFlow !== OAuthSocialAction.Import) return
64+
if (ssoFlowRef.current !== OAuthSocialAction.Import) return
6465

6566
if (!userOAuthProfile) {
6667
dispatch(resetDataRedisCloud())
@@ -73,7 +74,7 @@ const RedisCloudDatabasesPage = () => {
7374
history.push(Pages.redisCloudSubscriptions)
7475
}))
7576
}
76-
}, [ssoFlow, userOAuthProfile])
77+
}, [userOAuthProfile])
7778

7879
useEffect(() => {
7980
if (instancesAdded.length) {

redisinsight/ui/src/pages/autodiscover-cloud/redis-cloud-subscriptions/RedisCloudSubscriptionsPage.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const RedisCloudSubscriptionsPage = () => {
4848
} = useSelector(cloudSelector)
4949
const { data: userOAuthProfile } = useSelector(oauthCloudUserSelector)
5050
const currentAccountIdRef = useRef(userOAuthProfile?.id)
51+
const ssoFlowRef = useRef(ssoFlow)
5152

5253
setTitle('Redis Cloud Subscriptions')
5354

@@ -58,7 +59,7 @@ const RedisCloudSubscriptionsPage = () => {
5859
}, [])
5960

6061
useEffect(() => {
61-
if (ssoFlow !== OAuthSocialAction.Import) return
62+
if (ssoFlowRef.current !== OAuthSocialAction.Import) return
6263

6364
if (!userOAuthProfile) {
6465
history.push(Pages.home)
@@ -69,7 +70,7 @@ const RedisCloudSubscriptionsPage = () => {
6970
dispatch(fetchSubscriptionsRedisCloud(null, true))
7071
currentAccountIdRef.current = userOAuthProfile?.id
7172
}
72-
}, [ssoFlow, userOAuthProfile])
73+
}, [userOAuthProfile])
7374

7475
useEffect(() => {
7576
if (instancesLoaded) {

redisinsight/ui/src/services/theme.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ class ThemeService {
1616
let actualTheme = newTheme
1717

1818
if (newTheme === Theme.System) {
19-
if (window.matchMedia?.(THEME_MATCH_MEDIA_DARK)?.matches) {
20-
actualTheme = Theme.Dark
21-
} else {
22-
actualTheme = Theme.Light
23-
}
19+
actualTheme = window.matchMedia?.(THEME_MATCH_MEDIA_DARK)?.matches ? Theme.Dark : Theme.Light
2420
}
2521

2622
const sheet = new CSSStyleSheet()

redisinsight/ui/src/slices/interfaces/cloud.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface StateAppOAuth {
1717
source: Nullable<OAuthSocialSource>
1818
job: Nullable<CloudJobInfoState>
1919
user: {
20+
initialLoading: boolean
2021
error: string
2122
loading: boolean
2223
data: Nullable<CloudUser>

redisinsight/ui/src/slices/oauth/cloud.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const initialState: StateAppOAuth = {
5252
isOpenSelectAccountDialog: false,
5353
showProgress: true,
5454
user: {
55+
initialLoading: true,
5556
loading: false,
5657
error: '',
5758
data: null,
@@ -101,6 +102,7 @@ const oauthCloudSlice = createSlice({
101102
getUserInfoFailure: (state, { payload }: PayloadAction<string>) => {
102103
state.user.loading = false
103104
state.user.error = payload
105+
state.user.data = null
104106
},
105107
addFreeDb: (state) => {
106108
state.user.freeDb.loading = true
@@ -191,6 +193,9 @@ const oauthCloudSlice = createSlice({
191193
logoutUserFailure: (state) => {
192194
state.user.loading = false
193195
state.user.data = null
196+
},
197+
setInitialLoadingState: (state, { payload }: PayloadAction<boolean>) => {
198+
state.user.initialLoading = payload
194199
}
195200
},
196201
})
@@ -228,7 +233,8 @@ export const {
228233
removeAllCapiKeysFailure,
229234
logoutUser,
230235
logoutUserSuccess,
231-
logoutUserFailure
236+
logoutUserFailure,
237+
setInitialLoadingState
232238
} = oauthCloudSlice.actions
233239

234240
// A selector

redisinsight/ui/src/slices/tests/oauth/cloud.spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import reducer, {
5656
logoutUser,
5757
logoutUserSuccess,
5858
logoutUserFailure,
59-
logoutUserAction, oauthCloudUserSelector
59+
logoutUserAction, oauthCloudUserSelector, setInitialLoadingState
6060
} from '../../oauth/cloud'
6161

6262
let store: typeof mockedStore
@@ -848,6 +848,27 @@ describe('oauth cloud slice', () => {
848848
})
849849
})
850850

851+
describe('setInitialLoadingState', () => {
852+
it('should properly set the state', () => {
853+
// Arrange
854+
const userState = {
855+
...initialState.user,
856+
initialLoading: false
857+
}
858+
859+
// Act
860+
const nextState = reducer(initialState as any, setInitialLoadingState(false))
861+
862+
// Assert
863+
const rootState = Object.assign(initialStateDefault, {
864+
oauth: {
865+
cloud: nextState
866+
}
867+
})
868+
expect(oauthCloudUserSelector(rootState)).toEqual(userState)
869+
})
870+
})
871+
851872
describe('thunks', () => {
852873
describe('fetchUserInfo', () => {
853874
it('call both fetchUserInfo and getUserInfoSuccess when fetch is successed', async () => {

0 commit comments

Comments
 (0)