import React, {useState} from "react";
import {Button, Form, Header, Loader, TextArea} from "semantic-ui-react";
import {getDownloaders, postDownload} from "../api";
import {frequencyOptions, rssFrequencyOptions, WROLModeMessage} from "./Common";
import Icon from "semantic-ui-react/dist/commonjs/elements/Icon";
import Message from "semantic-ui-react/dist/commonjs/collections/Message";
const validUrl = /^(http|https):\/\/[^ "]+$/;
class Downloader extends React.Component {
constructor(props) {
super(props);
this.state = {
urls: '',
valid: true,
pending: false,
downloader: props.downloader,
};
}
submitDownload = async () => {
let {urls, downloader} = this.state;
if (urls) {
this.setState({pending: true});
try {
let response = await postDownload(urls, downloader);
if (response.status === 204) {
this.setState({urls: '', pending: false});
}
} 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});
}
}
render() {
let disabled = !this.state.urls || !this.state.valid || this.state.pending || !this.state.downloader;
const {header} = this.props;
return (