-
Notifications
You must be signed in to change notification settings - Fork 406
save and load theme using settings api #4433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
85b96a7
set theme from settings api
yashschandra 0d41efc
Merge branch 'main' of https://github.com/yashschandra/RedisInsight i…
yashschandra 91448e4
store system theme in react. load theme name from api in settings page
yashschandra a8daf34
Merge branch 'RedisInsight:main' into theme-loading
yashschandra c280c8b
Merge branch 'main' of https://github.com/yashschandra/RedisInsight i…
yashschandra 9389cf4
Merge branch 'theme-loading' of https://github.com/yashschandra/Redis…
yashschandra 92cd43f
Merge branch 'main' of https://github.com/yashschandra/RedisInsight i…
yashschandra 83bfed1
theme settings component added
yashschandra 7395830
eslint fixes
pawelangelow a5c40d4
use local variable instead of mutating
pawelangelow 9597069
add more tests
pawelangelow 505e0b7
Merge pull request #1 from RedisInsight/theme-loading-patch
yashschandra 731ba76
dont update if previous value is equal to new value
yashschandra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
redisinsight/ui/src/pages/settings/components/theme-settings/ThemeSettings.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import React from 'react' | ||
import { cloneDeep } from 'lodash' | ||
import { | ||
cleanup, | ||
fireEvent, | ||
mockedStore, | ||
render, | ||
screen, | ||
waitFor, | ||
} from 'uiSrc/utils/test-utils' | ||
import { Theme, THEMES } from 'uiSrc/constants' | ||
import { TelemetryEvent } from 'uiSrc/telemetry' | ||
import { updateUserConfigSettingsAction } from 'uiSrc/slices/user/user-settings' | ||
|
||
import ThemeSettings from './ThemeSettings' | ||
|
||
jest.mock('uiSrc/telemetry', () => ({ | ||
...jest.requireActual('uiSrc/telemetry'), | ||
sendEventTelemetry: jest.fn(), | ||
})) | ||
|
||
jest.mock('uiSrc/slices/user/user-settings', () => { | ||
const original = jest.requireActual('uiSrc/slices/user/user-settings') | ||
return { | ||
...original, | ||
updateUserConfigSettingsAction: jest.fn(() => ({ type: 'TEST_ACTION' })), | ||
} | ||
}) | ||
|
||
const { sendEventTelemetry } = require('uiSrc/telemetry') | ||
|
||
let store: typeof mockedStore | ||
|
||
describe('ThemeSettings', () => { | ||
beforeEach(() => { | ||
cleanup() | ||
store = cloneDeep(mockedStore) | ||
store.clearActions() | ||
}) | ||
|
||
it('should render', () => { | ||
expect(render(<ThemeSettings />)).toBeTruthy() | ||
}) | ||
|
||
it('should update the selected theme and dispatch telemetry on change', async () => { | ||
const initialTheme = Theme.Dark | ||
const newTheme = Theme.Light | ||
|
||
// @ts-ignore-next-line | ||
store.getState().user.settings.config = { | ||
...store.getState().user.settings.config, | ||
theme: initialTheme, | ||
} | ||
|
||
render(<ThemeSettings />, { store }) | ||
|
||
const dropdownButton = screen.getByTestId('select-theme') | ||
fireEvent.click(dropdownButton) | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('Light Theme')).toBeInTheDocument() | ||
}) | ||
|
||
fireEvent.click(screen.getByText('Light Theme')) | ||
|
||
expect(updateUserConfigSettingsAction).toHaveBeenCalledWith({ | ||
theme: newTheme, | ||
}) | ||
|
||
expect(sendEventTelemetry).toHaveBeenCalledWith({ | ||
event: TelemetryEvent.SETTINGS_COLOR_THEME_CHANGED, | ||
eventData: { | ||
previousColorTheme: initialTheme, | ||
currentColorTheme: newTheme, | ||
}, | ||
}) | ||
}) | ||
|
||
it('should display all theme options from THEMES', async () => { | ||
// @ts-ignore-next-line | ||
store.getState().user.settings.config = { | ||
...store.getState().user.settings.config, | ||
theme: Theme.Dark, | ||
} | ||
|
||
render(<ThemeSettings />, { store }) | ||
|
||
const dropdownButton = screen.getByTestId('select-theme') | ||
fireEvent.click(dropdownButton) | ||
|
||
const darkTheme = THEMES.find((theme) => theme.value === Theme.Dark) | ||
|
||
const rest = THEMES.filter((theme) => theme.value !== Theme.Dark) | ||
|
||
await waitFor(() => { | ||
// Selected theme name appears 2 times in the dropdown | ||
// once in the list and once in the selected value | ||
expect( | ||
screen.queryAllByText(darkTheme?.inputDisplay as string).length, | ||
).toEqual(2) | ||
|
||
rest.forEach((theme) => { | ||
expect( | ||
screen.getByText(theme.inputDisplay as string), | ||
).toBeInTheDocument() | ||
}) | ||
}) | ||
}) | ||
}) |
66 changes: 66 additions & 0 deletions
66
redisinsight/ui/src/pages/settings/components/theme-settings/ThemeSettings.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import React, { useContext, useEffect, useRef, useState } from 'react' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { EuiForm, EuiFormRow, EuiSuperSelect, EuiTitle } from '@elastic/eui' | ||
import { Spacer } from 'uiSrc/components/base/layout/spacer' | ||
import { | ||
updateUserConfigSettingsAction, | ||
userSettingsSelector, | ||
} from 'uiSrc/slices/user/user-settings' | ||
import { ThemeContext } from 'uiSrc/contexts/themeContext' | ||
import { sendEventTelemetry, TelemetryEvent } from 'uiSrc/telemetry' | ||
import { THEMES } from 'uiSrc/constants' | ||
|
||
const ThemeSettings = () => { | ||
const dispatch = useDispatch() | ||
const [selectedTheme, setSelectedTheme] = useState('') | ||
const options = THEMES | ||
const themeContext = useContext(ThemeContext) | ||
const { theme, changeTheme } = themeContext | ||
const { config } = useSelector(userSettingsSelector) | ||
const previousThemeRef = useRef<string>(theme) | ||
|
||
useEffect(() => { | ||
if (config) { | ||
setSelectedTheme(config.theme) | ||
previousThemeRef.current = config.theme | ||
} | ||
}, [config]) | ||
|
||
const onChange = (value: string) => { | ||
const previousValue = previousThemeRef.current | ||
if (previousValue === value) { | ||
return | ||
} | ||
changeTheme(value) | ||
dispatch(updateUserConfigSettingsAction({ theme: value })) | ||
sendEventTelemetry({ | ||
event: TelemetryEvent.SETTINGS_COLOR_THEME_CHANGED, | ||
eventData: { | ||
previousColorTheme: previousValue, | ||
currentColorTheme: value, | ||
}, | ||
}) | ||
} | ||
|
||
return ( | ||
<EuiForm component="form"> | ||
<EuiTitle size="xs"> | ||
<h4>Color Theme</h4> | ||
</EuiTitle> | ||
<Spacer size="m" /> | ||
<EuiFormRow label="Specifies the color theme to be used in Redis Insight:"> | ||
<EuiSuperSelect | ||
options={options} | ||
valueOfSelected={selectedTheme} | ||
onChange={onChange} | ||
style={{ marginTop: '12px' }} | ||
data-test-subj="select-theme" | ||
data-testid="select-theme" | ||
/> | ||
</EuiFormRow> | ||
<Spacer size="xl" /> | ||
</EuiForm> | ||
) | ||
} | ||
|
||
export default ThemeSettings |
3 changes: 3 additions & 0 deletions
3
redisinsight/ui/src/pages/settings/components/theme-settings/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import ThemeSettings from './ThemeSettings' | ||
|
||
export default ThemeSettings |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.