Skip to content

Commit

Permalink
feat(frontend/settings): refresh overlay dropdown on focus (#553)
Browse files Browse the repository at this point in the history
Co-Authored-By: Harjot1Singh <[email protected]>
  • Loading branch information
saihaj and Harjot1Singh authored May 30, 2020
1 parent 3bcf985 commit 0a990ac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
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

0 comments on commit 0a990ac

Please sign in to comment.