fga-eps-mds/2019.2-Vsign

View on GitHub
frontend/src/components/ContractsPage/SignButton.js

Summary

Maintainability
A
0 mins
Test Coverage
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { getContractQuery } from '../../graphql/queries';
import { setContractAction } from '../../actions/contract';

class SignButton extends Component {
    constructor(props) {
        super(props);
        this.handleClick = this.handleClick.bind(this);
        this.state = {
            fetching: false
        };
    }

    async handleClick(event) {
        event.preventDefault();
        this.toggleFetching();

        try {
            const { contractId } = this.props;
            const variables = { id: contractId };
            const { data } = await getContractQuery(variables);
            const { getContract } = data || {};
            this.props.setContractAction(getContract);
        } catch {
            alert('Não foi possível pegar informações do contrato');
        } finally {
            this.toggleFetching();
        }
        
    }
    

    toggleFetching() {
        this.setState({
            fetching: !this.state.fetching
        });
    }
    
    
    render() {
        const { fetching } = this.state;
        const text = (!fetching) ? 'Assinar' : 'Aguarde...';
        const disable = (fetching) ? 'disabled' : ''
        return (
            <button onClick={this.handleClick} className={`btn btn-primary ${disable}`}>{text}</button>
        );
    }
}

const mapDispatchToProps = {
    setContractAction
};

export default connect(null, mapDispatchToProps)(SignButton);