import React from "react";
import { SpinnerCircular } from 'spinners-react';

import { getFile } from "../../../actions";

export default class DownloadFileById extends React.Component 
{
	constructor(props)
	{
		super(props);
		this.state = {
			downloading: false, 
		};
	}

	_handle_onDownloadFile = () => 
	{
		const { id, filename } = this.props;
		const { downloading } = this.state;

		if(!downloading)
		{
			this.setState({ downloading: true }, () => 
			{
				getFile({ id, filename })
				.then(() => { this.setState({ downloading: false }); })
				.catch(() => { this.setState({ downloading: false }); });	
			});
		}
	}

	render() 
	{
		const { id, filename, url, bitrix, title } = this.props;
		const { downloading } = this.state;

		return (
			<span style={{ position: "relative" }} onClick={ this._handle_onDownloadFile }>
				{ downloading ? (
					<SpinnerCircular style={{ position: "absolute", top: "0px", left: "10px", }} size={ 18 } thickness={ 100 } speed={ 100 } color="rgba(255, 255, 255, 1)" secondaryColor="rgba(28, 1, 169, 1)" />
				) : ( 
					this.props.children 
				) }
			</span>
		)
	}
}