You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.6 KiB
JavaScript

import React from 'react'
import dynamic from 'next/dynamic'
import GlobalHighlights from './GlobalHighlights'
import Dropdown from '../Dropdown'
import { managePopout } from '../Popout'
import ReferralLink from '../ReferralLink'
import ThemeIcon from '../svg/Theme'
import RemoveIcon from '../svg/Remove'
import { COLORS } from '../../lib/constants'
const ThemeCreate = dynamic(() => import('./ThemeCreate'), {
loading: () => null,
})
6 years ago
const ThemeItem = ({ children, item, isSelected, remove }) => (
<div className="theme-item">
{children}
{/* TODO only show if isSelected */}
{item.referral && (
<div style={{ margin: `0 ${isSelected ? 8 : 0}px 0 8px` }}>
<ReferralLink href={item.referral}>Purchase</ReferralLink>
</div>
)}
{item.custom && !isSelected && (
6 years ago
<div
role="button"
tabIndex={0}
className="icon"
onClick={e => {
e.stopPropagation()
remove(item.id)
}}
>
<RemoveIcon color={COLORS.SECONDARY} style={{ transform: 'scale(1.6)' }} />
</div>
)}
<style jsx>
{`
.theme-item {
display: flex;
flex: 1;
justify-content: ${item.id === 'create' ? 'center' : 'space-between'};
align-items: center;
}
.icon {
display: flex;
margin-right: 6px;
}
`}
</style>
</div>
)
const themeIcon = <ThemeIcon />
const getCustomName = themes =>
`Custom Theme ${themes.filter(({ name }) => name.startsWith('Custom Theme')).length + 1}`
class Themes extends React.PureComponent {
state = {
name: '',
}
dropdown = React.createRef()
6 years ago
static getDerivedStateFromProps(props) {
if (!props.isVisible) {
return {
name: getCustomName(props.themes),
6 years ago
}
}
return null
}
6 years ago
handleThemeSelected = theme => {
if (theme) {
const { toggleVisibility, update } = this.props
if (theme.id === 'create') {
toggleVisibility()
this.dropdown.current.closeMenu()
} else {
update(theme.id)
}
}
}
6 years ago
create = theme => {
this.props.toggleVisibility()
this.props.create(theme)
}
6 years ago
itemWrapper = props => <ThemeItem {...props} remove={this.props.remove} />
render() {
6 years ago
const { themes, theme, isVisible, toggleVisibility } = this.props
const highlights = { ...theme.highlights, ...this.props.highlights }
const dropdownValue = isVisible ? { name: this.state.name } : theme
const dropdownList = [
{
id: 'create',
name: 'Create +',
},
...themes,
]
return (
<div className="themes" data-cy="themes-container">
<Dropdown
title="Theme"
innerRef={this.dropdown}
icon={themeIcon}
disableInput={isVisible}
selected={dropdownValue}
list={dropdownList}
itemWrapper={this.itemWrapper}
6 years ago
onChange={this.handleThemeSelected}
onOpen={isVisible && toggleVisibility}
/>
{isVisible && (
<ThemeCreate
theme={theme}
themes={themes}
highlights={highlights}
6 years ago
create={this.create}
updateHighlights={this.props.updateHighlights}
name={this.state.name}
onInputChange={e => this.setState({ name: e.target.value })}
/>
)}
<GlobalHighlights highlights={highlights} />
<style jsx>
{`
.themes {
position: relative;
}
`}
</style>
</div>
)
}
}
6 years ago
export default managePopout(Themes)