import React, {useContext, useState} from "react"; import {getDownloaders, postDownload} from "../api"; import {DirectoryInput, frequencyOptions, HelpPopup, rssFrequencyOptions, WROLModeMessage} from "./Common"; import Icon from "semantic-ui-react/dist/commonjs/elements/Icon"; import Message from "semantic-ui-react/dist/commonjs/collections/Message"; import {ThemeContext} from "../contexts/contexts"; import {Accordion, Button, Form, FormField, FormGroup, FormInput, Header, Loader, Segment, TextArea} from "./Theme"; import {AccordionContent, AccordionTitle, FormDropdown} from "semantic-ui-react"; const validUrl = /^(http|https):\/\/[^ "]+$/; class Downloader extends React.Component { constructor(props) { super(props); this.state = { advancedOpen: false, destination: '', disabled: this.props.disabled, downloader: props.downloader, pending: false, urls: '', valid: true, }; } submitDownload = async () => { let {urls, downloader, destination} = this.state; if (urls) { this.setState({pending: true}); try { let response = await postDownload(urls, downloader, null, null, null, destination); if (response.status === 204) { this.setState({urls: '', pending: false}); } } finally { this.setState({pending: false}); } } } handleInputChange = async (event, {name, value}) => { console.log(name, value); this.setState({[name]: value}, this.validateUrls); } handleDestinationChange = (value) => { this.setState({destination: value}); } validateUrls = async () => { // Validate that all URLs in state are valid. if (this.state.urls) { let urls = this.state.urls.split(/\r?\n/); let valid = true; urls.forEach((url) => { if (valid && url && !validUrl.test(url)) { valid = false; } }) this.setState({valid}); } } render() { let disabled = !this.state.urls || !this.state.valid || this.state.pending || !this.state.downloader; const {advancedOpen, destination} = this.state; const {header, withDestination} = this.props; const advancedAccordion = this.setState({advancedOpen: !advancedOpen})}> Advanced ; return {({i}) => (
{header}