import React, {useContext, useState} from "react"; import {getDownloaders, postDownload} from "../api"; import {APIButton, DirectorySearch, 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, FormInput, Header, Loader, TextArea} from "./Theme"; import {AccordionContent, AccordionTitle, Form as SForm, FormDropdown} from "semantic-ui-react"; import {Link} from "react-router-dom"; import {TagsSelector} from "../Tags"; import {toast} from "react-semantic-toasts-2"; import Grid from "semantic-ui-react/dist/commonjs/collections/Grid"; 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, submitted: false, tagNames: [], urls: '', valid: true, destinationRequired: props.destinationRequired !== undefined ? props.destinationRequired : false, }; } submitDownload = async () => { let {urls, downloader, destination, tagNames} = this.state; if (urls) { this.setState({pending: true, submitted: false}); try { let response = await postDownload(urls, downloader, null, null, null, destination, tagNames); if (response.status === 204) { this.setState({urls: '', pending: false, submitted: true}); } else { toast({type: 'error', title: 'Error', description: 'Failed to create download!'}); } } finally { this.setState({pending: false}); } } } handleInputChange = async (event, {name, value}) => { this.setState({[name]: value}, this.validateUrls); } 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}); } } handleKeydown = async (e) => { const {valid, urls} = this.state; if (e.keyCode === 13 && e.ctrlKey && urls && valid) { await this.submitDownload(); } } handleSelectedTags = (tagNames) => { this.setState({tagNames}); } componentDidMount() { document.addEventListener('keydown', this.handleKeydown); } componentWillUnmount() { document.removeEventListener('keydown', this.handleKeydown); } render() { const { advancedOpen, submitted, tagNames, urls, valid, pending, downloader, destination, destinationRequired } = this.state; let disabled = !urls || !valid || pending || !downloader; const {header, withTags, withSearchDirectory} = this.props; let directorySearch; if (withSearchDirectory) { directorySearch =
this.setState({destination: i})}/>
; } if (destinationRequired) { disabled = !destination || disabled; } let tagsSelector; if (withTags) { tagsSelector = ; } const advancedAccordion = this.setState({advancedOpen: !advancedOpen})}> Advanced {directorySearch} ; const viewDownloads = View downloads; return {({i}) => (
{header}