import React from 'react';
import superagent from 'superagent';
import cookie from 'react-cookies';

import * as ApiUtils from '../CloudApi/ApiUtils.js';
import FabricatorWrapper from './FabricatorWrapper.jsx';
import ScanRequestComponent from './ScanRequestComponent.jsx';

export default class ScanRequestHome extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            scanRequest: null
        };
    }

    async componentDidMount() {
        await this.reload();
    }

    async reload() {
        try {
            let schemas = await ApiUtils.getFabricatorSchemas();
            this.setState({ schemas });

            const res = await superagent.get(`/api/admin/fabricator/scan_request/${this.props.match.params.scanRequestId}`).accept('json');
            console.log(res.body);
            this.setState({scanRequest: res.body });
        } catch (e) {
            console.log(e);
        }
    }

    render() {
        if (this.state.scanRequest) {
            return (
                <FabricatorWrapper title={`Scan Request ${this.state.scanRequest.id}`} loading={false}>    
                    <ScanRequestComponent schemas={this.state.schemas} scanRequest={this.state.scanRequest} />
                </FabricatorWrapper>
            );
        } else {
            return null;
        }
    }
}
