<commit_before>import './inputSignature.scss';
import React, { Component } from 'react';
import { func, object, string, bool } from 'prop-types';
import axios from 'axios';
import SignaturePad from 'react-signature-pad';
import { Label, SubmitStatusIcon, IconSmall } from 'views/components';

class InputSignature extends Component {
    static defaultProps = {
        labelDraw: 'Draw your signature in the box below!',
        labelType: 'Type your legal name into the field below to see your generated signature!',
        labelUpload: 'Upload a image of your signature!',
    }

    static propTypes = {
        onChange: func.isRequired,
        value: object,
        labelDraw: string,
        labelType: string,
        labelUpload: string,
        required: bool,
    };

    state = {
        selected: 'draw',
        textValue: '',
        timer: null,
    };

    componentDidMount() {
        if (this.props.value)
            this.updateSelected();
    }

    componentDidUpdate(prevProps) {
        if (prevProps.value !== this.props.value)
            if (this.props.value)
                this.updateSelected();
    }

    updateSelected() {
        const { value } = this.props;

        if (value.type === 'text') {
            this.setState({
                selected: 'text',
                textValue: value.data,
            });
        }
        else if (value.type === 'draw') {
            this.setState({
                selected: 'draw',
            });

            this.signaturePad.fromDataURL(value.data);
        }
    }

    getOptions() {
        return [
            {
                id: 'draw',
                name: 'Draw',
                icon: 'gesture',
                instructions: this.props.labelDraw,
            },
            {
                id: 'text',
                name: 'Type',
                icon: 'text_fields',
                instructions: this.props.labelType,
            },
            {
                id: 'upload',
                name: 'Upload',
                icon: 'file_upload',
                instructions: this.props.labelUpload,
                disabled: true,
            },
        ];
    }

    handleClickOption = option => () => {
        this.setState({
            selected: option.id,
        });
    };

    handleSignatureChange = type => data => {
        this.postRequest({ type, data });
    };

    postRequest = ( data ) => {

        const url = 'https://signatures.pleased.property/signature';

        axios({
            method: 'post',
            url: url,
            data: data,
        }).then( response => {
            const uploadURL = response.data.signatureURL;
            this.setState({
                uploadedUrl: uploadURL,
            }, () => {
                if (this.props.validation) this.props.validation( uploadURL, this.props.identifier, this.props.validationList);
                if (this.props.onChange) {
                    this.props.onChange( uploadURL );
                }
            });
        });
    }

    handleClear = () => {

        if ( this.signaturePad ) {
            this.signaturePad.clear();
        } else {
            this.setState({
                textValue: '',
            });
        }
    }

    handleRemove = () => {
        if (this.props.validation) this.props.validation( '', this.props.identifier, this.props.validationList);
        if (this.props.onChange) {
            this.props.onChange( '' );
        }
    }

    onChange = event => {<commit_msg>updated signature component to use URL from project attribute<commit_after>import './inputSignature.scss';
import React, { Component } from 'react';
import { func, object, string, bool } from 'prop-types';
import axios from 'axios';
import SignaturePad from 'react-signature-pad';
import { Label, SubmitStatusIcon, IconSmall } from 'views/components';
import { BaseEntityQuery, GennyBridge } from 'utils/genny';

class InputSignature extends Component {
    static defaultProps = {
        labelDraw: 'Draw your signature in the box below!',
        labelType: 'Type your legal name into the field below to see your generated signature!',
        labelUpload: 'Upload a image of your signature!',
    }

    static propTypes = {
        onChange: func.isRequired,
        value: object,
        labelDraw: string,
        labelType: string,
        labelUpload: string,
        required: bool,
    };

    state = {
        selected: 'draw',
        textValue: '',
        timer: null,
    };

    componentDidMount() {
        if (this.props.value)
            this.updateSelected();
    }

    componentDidUpdate(prevProps) {
        if (prevProps.value !== this.props.value)
            if (this.props.value)
                this.updateSelected();
    }

    updateSelected() {
        const { value } = this.props;

        if (value.type === 'text') {
            this.setState({
                selected: 'text',
                textValue: value.data,
            });
        }
        else if (value.type === 'draw') {
            this.setState({
                selected: 'draw',
            });

            this.signaturePad.fromDataURL(value.data);
        }
    }

    getOptions() {
        return [
            {
                id: 'draw',
                name: 'Draw',
                icon: 'gesture',
                instructions: this.props.labelDraw,
            },
            {
                id: 'text',
                name: 'Type',
                icon: 'text_fields',
                instructions: this.props.labelType,
            },
            {
                id: 'upload',
                name: 'Upload',
                icon: 'file_upload',
                instructions: this.props.labelUpload,
                disabled: true,
            },
        ];
    }

    handleClickOption = option => () => {
        this.setState({
            selected: option.id,
        });
    };

    handleSignatureChange = type => data => {
        this.postRequest({ type, data });
    };

    postRequest = ( data ) => {

        //const url = 'https://signatures.pleased.property/signature';
        const url = BaseEntityQuery.getBaseEntityAttribute(GennyBridge.getProject(), 'PRI_SIGNATURE_URL');
        console.log(url);

        if (url != null && url.value != null) {
            axios({
                method: 'post',
                url: url.value,
                data: data,
            }).then( response => {
                console.log(response);
                const uploadURL = response.data.signatureURL;
                this.setState({
                    uploadedUrl: uploadURL,
                }, () => {
                    if (this.props.validation) this.props.validation( uploadURL, this.props.identifier, this.props.validationList);
                    if (this.props.onChange) {
                        this.props.onChange( uploadURL );
                    }
                });
            });
        }
    }

    handleClear = () => {

        if ( this.signaturePad ) {
            this.signaturePad.clear();
        } else {
            this.setState({
                textValue: '',
            });
        }
    }

    handleRemove = () => {
        if (this.props.validation) this.props.validation( '', this.props.identifier, this.props.validationList);
        if (this.props.onChange) {
            this.props.onChange( '' );
        }
    }

    onChange = event => {
{'fraction_non_alphanumeric': 0.12150537634408602, 'fraction_numerical': 0.0, 'mean_word_length': 1.4606481481481481, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4441526', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1906, 'n_words': 426}