Skip to content
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

feat(frontend/settings): refresh overlay dropdown on focus #553

Merged
merged 2 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion app/frontend/src/Settings/SettingComponents.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useState, useEffect } from 'react'
import { string, func, any, arrayOf, number, bool } from 'prop-types'

import classNames from 'classnames'
Expand Down Expand Up @@ -99,10 +99,36 @@ Button.defaultProps = {
className: null,
}

export const UrlDropdown = ( { url, ...props } ) => {
const [ isOpen, setOpen ] = useState( false )
const [ values, setValues ] = useState( [] )

useEffect( () => {
fetch( url )
.then( res => res.json() )
.then( values => values.map( value => ( { name: value, value } ) ) )
.then( setValues )
}, [ setValues, isOpen, url ] )

return (
<Dropdown
{...props}
values={values}
onOpen={() => setOpen( true )}
onClose={() => setOpen( false )}
/>
)
}

UrlDropdown.propTypes = {
url: string.isRequired,
}

const typeComponents = {
[ OPTION_TYPES.dropdown ]: GeneralSettingEvent( Dropdown ),
[ OPTION_TYPES.toggle ]: GeneralSettingParam( Toggle ),
[ OPTION_TYPES.slider ]: GeneralSettingParam( Slider ),
[ OPTION_TYPES.urlDropdown ]: GeneralSettingEvent( UrlDropdown ),
}

export default type => typeComponents[ type ]
9 changes: 0 additions & 9 deletions app/frontend/src/Settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,6 @@ const Settings = () => {
} )
}, [] )

// Fetch list of overlay themes from server
useEffect( () => {
fetch( `${BACKEND_URL}/overlay/themes` )
.then( res => res.json() )
.then( themes => {
OPTIONS.overlayName.values = themes.map( theme => ( { name: theme, value: theme } ) )
} )
}, [] )

const openMobileMenu = () => setMobileOpen( true )
const closeMobileMenu = () => setMobileOpen( false )

Expand Down
5 changes: 3 additions & 2 deletions app/frontend/src/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
faPauseCircle,
} from '@fortawesome/free-regular-svg-icons'

import { LANGUAGES } from './consts'
import { LANGUAGES, BACKEND_URL } from './consts'
import SHORTCUTS from './keyMap'

/**
Expand All @@ -55,6 +55,7 @@ export const OPTION_TYPES = {
toggle: Symbol( 'Toggle' ),
slider: Symbol( 'Slider' ),
colorPicker: Symbol( 'Color Picker' ),
urlDropdown: Symbol( 'URL Dropdown' ),
}

export const PRIVACY_TYPES = {
Expand Down Expand Up @@ -144,7 +145,7 @@ export const OPTIONS = {
{ name: 'Urdu', value: LANGUAGES.urdu },
],
},
overlayName: { name: 'Overlay Name', icon: faPalette, type: OPTION_TYPES.dropdown, values: [], privacy: PRIVACY_TYPES.global },
overlayName: { name: 'Overlay Name', icon: faPalette, type: OPTION_TYPES.urlDropdown, values: [], url: `${BACKEND_URL}/overlay/themes`, privacy: PRIVACY_TYPES.global },
}

// Possible options groups
Expand Down