-
-
Notifications
You must be signed in to change notification settings - Fork 132
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
Add Theme Preferences Setting #3696
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c95ebbf
Set up
dem4ron d4af2ab
Add ThemeButtons
dem4ron 340f184
Change wording slightly
dem4ron 36fff42
Tweaks
iHiD 3dbe582
Update colors, add infomessages, set up buttons correctly
dem4ron 8c71237
Add JS function to update theme pref
dem4ron aead3f5
Simplify function
dem4ron 0bd40e1
Update fn
dem4ron 2efeb0f
Change boolean name to be more descriptive
dem4ron 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 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 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
21 changes: 21 additions & 0 deletions
21
app/helpers/react_components/settings/theme_preference_form.rb
This file contains 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,21 @@ | ||
module ReactComponents | ||
module Settings | ||
class ThemePreferenceForm < ReactComponent | ||
def to_s | ||
super("settings-theme-preference-form", { | ||
default_theme_preference:, | ||
insiders_status: current_user.insiders_status, | ||
links: { | ||
# TODO: add this field too, check this update URL | ||
update: Exercism::Routes.api_settings_url | ||
} | ||
}) | ||
end | ||
|
||
# TODO: add these as default values in DB | ||
def default_theme_preference | ||
%i[eligible eligible_lifetime].include?(current_user.insiders_status) ? 'system' : 'light' | ||
end | ||
end | ||
end | ||
end |
This file contains 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
182 changes: 182 additions & 0 deletions
182
app/javascript/components/settings/ThemePreferenceForm.tsx
This file contains 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,182 @@ | ||
import React, { useState, useCallback } from 'react' | ||
import { FormButton, GraphicalIcon, Icon } from '../common' | ||
import { useSettingsMutation } from './useSettingsMutation' | ||
import { FormMessage } from './FormMessage' | ||
import { ExercismTippy } from '../misc/ExercismTippy' | ||
|
||
type Links = { | ||
update: string | ||
} | ||
|
||
type RequestBody = { | ||
user: { | ||
theme_preference: string | ||
} | ||
} | ||
|
||
type Theme = { | ||
label: string | ||
background: string | ||
iconFilter: string | ||
value: string | ||
} | ||
|
||
const DEFAULT_ERROR = new Error('Unable to update theme preference') | ||
const THEME_BUTTON_SIZE = 128 | ||
const THEMES: Theme[] = [ | ||
{ | ||
label: 'Light', | ||
value: 'light', | ||
background: 'white', | ||
iconFilter: 'textColor1', | ||
}, | ||
{ | ||
label: 'System', | ||
value: 'system', | ||
background: | ||
'linear-gradient(90deg, rgba(255,255,255,1) 50%, rgba(48,43,66,1) 50%)', | ||
iconFilter: 'gray', | ||
}, | ||
{ | ||
label: 'Dark', | ||
value: 'dark', | ||
background: '#302b42', //russianViolet | ||
iconFilter: 'aliceBlue', | ||
}, | ||
{ | ||
label: 'Accessibility Dark', | ||
value: 'accessibility-dark', | ||
background: 'black', | ||
iconFilter: 'white-no-dark', | ||
}, | ||
] | ||
|
||
export const ThemePreferenceForm = ({ | ||
defaultThemePreference, | ||
insidersStatus, | ||
links, | ||
}: { | ||
defaultThemePreference: string | ||
insidersStatus: string | ||
links: Links | ||
}): JSX.Element => { | ||
const [theme, setTheme] = useState<string>(defaultThemePreference || '') | ||
|
||
const { mutation, status, error } = useSettingsMutation<RequestBody>({ | ||
endpoint: links.update, | ||
method: 'PATCH', | ||
body: { user: { theme_preference: theme } }, | ||
}) | ||
|
||
const handleSubmit = useCallback( | ||
(e) => { | ||
e.preventDefault() | ||
|
||
mutation() | ||
}, | ||
[mutation] | ||
) | ||
|
||
return ( | ||
<form data-turbo="false" onSubmit={handleSubmit}> | ||
<h2>Theme</h2> | ||
<div className="flex gap-32"> | ||
{THEMES.map((t: Theme) => ( | ||
<ThemeButton | ||
key={t.label} | ||
theme={t} | ||
currentTheme={theme} | ||
disabled={isDisabled(insidersStatus, t.value)} | ||
onClick={() => setTheme(t.value)} | ||
/> | ||
))} | ||
</div> | ||
<div className="form-footer"> | ||
<FormButton status={status} className="btn-primary btn-m"> | ||
Update theme preference | ||
</FormButton> | ||
<FormMessage | ||
status={status} | ||
error={error} | ||
defaultError={DEFAULT_ERROR} | ||
SuccessMessage={() => <SuccessMessage />} | ||
/> | ||
</div> | ||
</form> | ||
) | ||
} | ||
|
||
const SuccessMessage = () => { | ||
return ( | ||
<div className="status success"> | ||
<Icon icon="completed-check-circle" alt="Success" /> | ||
Your theme preference has been updated! | ||
</div> | ||
) | ||
} | ||
|
||
function ThemeButton({ | ||
theme, | ||
currentTheme, | ||
onClick, | ||
disabled = false, | ||
}: { | ||
theme: Theme | ||
onClick: React.MouseEventHandler<HTMLButtonElement> | ||
currentTheme: string | ||
disabled?: boolean | ||
}) { | ||
const selected = theme.value === currentTheme | ||
|
||
return ( | ||
<ExercismTippy content={disabled && <DisabledTooltip />}> | ||
<div className="flex flex-col gap-16 items-center"> | ||
<button | ||
disabled={disabled} | ||
id={`${theme.value}-theme`} | ||
style={{ | ||
height: `${THEME_BUTTON_SIZE}px`, | ||
width: `${THEME_BUTTON_SIZE}px`, | ||
background: `${theme.background}`, | ||
filter: disabled ? 'grayscale(0.9)' : '', | ||
opacity: disabled ? '60%' : '100%', | ||
}} | ||
className={`flex items-center justify-center border-1 border-borderColor6 rounded-8 ${ | ||
selected && '--selected-theme' | ||
}`} | ||
onClick={onClick} | ||
> | ||
<GraphicalIcon | ||
icon={disabled ? 'lock-circle' : 'logo'} | ||
height={32} | ||
width={32} | ||
className={!disabled ? `filter-${theme.iconFilter}` : ''} | ||
/> | ||
</button> | ||
<label className="text-p" htmlFor={`${theme.value}-theme`}> | ||
{theme.label} | ||
</label> | ||
</div> | ||
</ExercismTippy> | ||
) | ||
} | ||
|
||
function DisabledTooltip(): JSX.Element { | ||
return ( | ||
<div className="flex items-center bg-russianViolet rounded-16 py-8 px-12 text-p-base text-aliceBlue"> | ||
You must be an | ||
<strong style={{ color: 'inherit' }} className="flex items-center"> | ||
Exercism Insider | ||
<GraphicalIcon icon="insiders" height={24} width={24} /> | ||
</strong> | ||
to unlock this theme. | ||
</div> | ||
) | ||
} | ||
|
||
function isDisabled(insidersStatus: string, theme: string): boolean { | ||
const eligible = ['eligible', 'eligible_lifetime'].includes(insidersStatus) | ||
const themeDisabled = ['dark', 'system'].includes(theme) | ||
|
||
return !eligible && themeDisabled | ||
} |
This file contains 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
active
variants, noteligible
variants. Check this in all your PRs pls