import React, { cloneElement } from "react" import PropTypes from "prop-types" import {parseSearch, serializeSearch} from "core/utils" class TopBar extends React.Component { static propTypes = { layoutActions: PropTypes.object.isRequired, authActions: PropTypes.object.isRequired } constructor(props, context) { super(props, context) this.state = { url: props.specSelectors.url(), selectedIndex: 0 } } UNSAFE_componentWillReceiveProps(nextProps) { this.setState({ url: nextProps.specSelectors.url() }) } onUrlChange =(e)=> { let {target: {value}} = e this.setState({url: value}) } flushAuthData() { const { persistAuthorization } = this.props.getConfigs() if (persistAuthorization) { return } this.props.authActions.restoreAuthorization({ authorized: {} }) } loadSpec = (url) => { this.flushAuthData() this.props.specActions.updateUrl(url) this.props.specActions.download(url) } onUrlSelect =(e)=> { let url = e.target.value || e.target.href this.loadSpec(url) this.setSelectedUrl(url) e.preventDefault() } downloadUrl = (e) => { this.loadSpec(this.state.url) e.preventDefault() } setSearch = (spec) => { let search = parseSearch() search["urls.primaryName"] = spec.name const newUrl = `${window.location.protocol}//${window.location.host}${window.location.pathname}` if(window && window.history && window.history.pushState) { window.history.replaceState(null, "", `${newUrl}?${serializeSearch(search)}`) } } setSelectedUrl = (selectedUrl) => { const configs = this.props.getConfigs() const urls = configs.urls || [] if(urls && urls.length) { if(selectedUrl) { urls.forEach((spec, i) => { if(spec.url === selectedUrl) { this.setState({selectedIndex: i}) this.setSearch(spec) } }) } } } componentDidMount() { const configs = this.props.getConfigs() const urls = configs.urls || [] if(urls && urls.length) { var targetIndex = this.state.selectedIndex let search = parseSearch() let primaryName = search["urls.primaryName"] || configs.urls.primaryName if(primaryName) { urls.forEach((spec, i) => { if(spec.name === primaryName) { this.setState({selectedIndex: i}) targetIndex = i } }) } this.loadSpec(urls[targetIndex].url) } } onFilterChange =(e) => { let {target: {value}} = e this.props.layoutActions.updateFilter(value) } render() { let { getComponent, specSelectors, getConfigs } = this.props const Button = getComponent("Button") const Link = getComponent("Link") const Logo = getComponent("Logo") let isLoading = specSelectors.loadingStatus() === "loading" let isFailed = specSelectors.loadingStatus() === "failed" const classNames = ["download-url-input"] if (isFailed) classNames.push("failed") if (isLoading) classNames.push("loading") const { urls } = getConfigs() let control = [] let formOnSubmit = null if(urls) { let rows = [] urls.forEach((link, i) => { rows.push() }) control.push( ) } else { formOnSubmit = this.downloadUrl control.push( ) control.push() } return (
{control.map((el, i) => cloneElement(el, { key: i }))}
) } } TopBar.propTypes = { specSelectors: PropTypes.object.isRequired, specActions: PropTypes.object.isRequired, getComponent: PropTypes.func.isRequired, getConfigs: PropTypes.func.isRequired } export default TopBar