<commit_before>import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';
import Typography from '@material-ui/core/Typography';
import SponsorshipPlusTabs from './SponsorshipPlusTabs';
import Button from '@material-ui/core/Button';
import ModalForm from './ModalForm';
import getRequestId from "./getRequestId";
import jsonRPC from "./jsonRPC";
import i18n from '../i18n';
import Snackbar from "@material-ui/core/Snackbar/Snackbar";
import IconButton from "@material-ui/core/IconButton/IconButton";
import CloseIcon from '@material-ui/icons/Close';
import SnackbarContent from "@material-ui/core/SnackbarContent/SnackbarContent";
import ErrorIcon from '@material-ui/icons/Error';
import classNames from 'classnames';

const styles = theme => ({
    container: {
        display: 'flex',
        flexWrap: 'wrap',
    },
    textField: {
        marginLeft: theme.spacing.unit,
        marginRight: theme.spacing.unit,
        width: '100%',
    },
    sponsorButton: {
        marginRight: 7,
        marginBottom: 20,
        float: 'right',
    },
    sponsorPlusTabs: {
        marginTop: 15
    },
    icon: {
        fontSize: 20,
    },
    iconVariant: {
        opacity: 0.9,
        marginRight: theme.spacing.unit,
    },
    message: {
        display: 'flex',
        alignItems: 'center',
    },
});


class TextFields extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            sp_plus_value: 1,
            partner: props.appContext.state.partner,
            snackBarOpen: false,
            formErrors: false,
            message: ''
        };

        if (!this.state.partner) {
            this.state.partner = {
                firstname: '',
                lastname: '',
                email: ''
            }
        }
    }

    handleChange = name => event => {
        let initialPartner = {
            ...this.state.partner
        };
        initialPartner[name] = event.target.value;
        this.setState({
            partner: {
                ...initialPartner
            }
        });
    };

    handleCloseSnackbar = (event, reason) => {
        if (reason === 'clickaway') {
            return;
        }
        this.setState({ snackBarOpen: false });
    };

    sponsorFormHandler = () => {
        let requestId = getRequestId();
        let url = "/sms_sponsorship/step1/" + requestId + "/confirm";
        let lang = i18n.language;
        let sponsor_form = document.forms.sponsor_form;

        // Validate name
        if (! sponsor_form.firstname.value) {
            this.setState({
                snackBarOpen: true,
                message: this.props.t("error_missingFirstname"),
                formErrors: 'firstname'
            });
            return;
        }
        if (! sponsor_form.lastname.value) {
            this.setState({
                snackBarOpen: true,
                message: this.props.t("error_missingLastname"),
                formErrors: 'lastname'
            });
            return;
        }
        // Validate email field
        let email = sponsor_form.email.value;
        if (email !== sponsor_form.email2.value || email.indexOf('@') < 0) {
            this.setState({
                snackBarOpen: true,
                message: this.props.t("error_invalidMail"),
                formErrors: 'email'
            });
            return;
        }

        let data = {
            firstname: sponsor_form.firstname.value,
            lastname: sponsor_form.lastname.value,
            email: email,
            sponsorship_plus: sponsor_form.sponsorship_plus.checked,
            lang: lang,
        };
        const {preferred_name, image_url} = this.props.appContext.state.child;
        this.props.appContext.setState({child: false});
        jsonRPC(url, data, (res) => {
            if (JSON.parse(res.responseText).result.result === 'success') {
                this.props.appContext.setState({success: {preferred_name: preferred_name, image_url: image_url}});
            }
        });
    };

    render() {
        const { classes, t } = this.props;

        let partner = this.state.partner;

        return (
            <div>
                <div style={{marginBottom: '25px'}}>
                        <Button variant="outlined"
                            onClick={() => {
                                partner.firstname = '';
                                partner.lastname = '';
                                partner.email = '';
                                this.setState({
                                    partner: partner
                                });
                            }
                            }
                            color="primary"
                            size="medium"
                            fullWidth
                    >
                        {t('newSponsor')}
                    </Button>
                </div>
                <Typography variant="title" style={{color: '#555555', marginLeft: '8px'}}>
                    {t("coordinates")}
                </Typography>
                <form id="sponsor_form" className={classes.container} noValidate autoComplete="off">
                    <TextField
                        id="firstname"
                        label={t("firstname")}
                        className={classes.textField}
                        onChange={this.handleChange('firstname')}
                        value={partner.firstname}
                        margin="dense"
                        required={true}
                        error={this.state.formErrors === 'firstname'}
                    />
                    <TextField
                        id="lastname"
                        label={t("lastname")}
                        className={classes.textField}
                        onChange={this.handleChange('lastname')}
                        value={partner.lastname}
                        margin="dense"
                        required={true}
                        error={this.state.formErrors === 'lastname'}
                    />
                    <TextField<commit_msg>CO-2956 FIX email2 field not cleared with the button. Fixed layout.<commit_after>import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';
import Typography from '@material-ui/core/Typography';
import SponsorshipPlusTabs from './SponsorshipPlusTabs';
import Button from '@material-ui/core/Button';
import ModalForm from './ModalForm';
import getRequestId from "./getRequestId";
import jsonRPC from "./jsonRPC";
import i18n from '../i18n';
import Snackbar from "@material-ui/core/Snackbar/Snackbar";
import IconButton from "@material-ui/core/IconButton/IconButton";
import CloseIcon from '@material-ui/icons/Close';
import SnackbarContent from "@material-ui/core/SnackbarContent/SnackbarContent";
import ErrorIcon from '@material-ui/icons/Error';
import classNames from 'classnames';

const styles = theme => ({
    container: {
        display: 'flex',
        flexWrap: 'wrap',
    },
    textField: {
        marginLeft: theme.spacing.unit,
        marginRight: theme.spacing.unit,
        width: '100%',
    },
    sponsorButton: {
        marginRight: 7,
        marginBottom: 20,
        float: 'right',
    },
    sponsorPlusTabs: {
        marginTop: 15
    },
    icon: {
        fontSize: 20,
    },
    iconVariant: {
        opacity: 0.9,
        marginRight: theme.spacing.unit,
    },
    message: {
        display: 'flex',
        alignItems: 'center',
    },
});


class TextFields extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            sp_plus_value: 1,
            partner: props.appContext.state.partner,
            snackBarOpen: false,
            formErrors: false,
            message: '',
        };

        if (!this.state.partner) {
            this.state.partner = {
                firstname: '',
                lastname: '',
                email: ''
            }
        }
    }

    handleChange = name => event => {
        let initialPartner = {
            ...this.state.partner
        };
        initialPartner[name] = event.target.value;
        this.setState({
            partner: {
                ...initialPartner
            }
        });
    };

    handleCloseSnackbar = (event, reason) => {
        if (reason === 'clickaway') {
            return;
        }
        this.setState({ snackBarOpen: false });
    };

    sponsorFormHandler = () => {
        let requestId = getRequestId();
        let url = "/sms_sponsorship/step1/" + requestId + "/confirm";
        let lang = i18n.language;
        let sponsor_form = document.forms.sponsor_form;

        // Validate name
        if (! sponsor_form.firstname.value) {
            this.setState({
                snackBarOpen: true,
                message: this.props.t("error_missingFirstname"),
                formErrors: 'firstname'
            });
            return;
        }
        if (! sponsor_form.lastname.value) {
            this.setState({
                snackBarOpen: true,
                message: this.props.t("error_missingLastname"),
                formErrors: 'lastname'
            });
            return;
        }
        // Validate email field
        let email = sponsor_form.email.value;
        if (email !== sponsor_form.email2.value || email.indexOf('@') < 0) {
            this.setState({
                snackBarOpen: true,
                message: this.props.t("error_invalidMail"),
                formErrors: 'email'
            });
            return;
        }

        let data = {
            firstname: sponsor_form.firstname.value,
            lastname: sponsor_form.lastname.value,
            email: email,
            sponsorship_plus: sponsor_form.sponsorship_plus.checked,
            lang: lang,
        };
        const {preferred_name, image_url} = this.props.appContext.state.child;
        this.props.appContext.setState({child: false});
        jsonRPC(url, data, (res) => {
            if (JSON.parse(res.responseText).result.result === 'success') {
                this.props.appContext.setState({success: {preferred_name: preferred_name, image_url: image_url}});
            }
        });
    };

    render() {
        const { classes, t } = this.props;

        let partner = this.state.partner;

        return (
            <div>
                <Typography variant="title" style={{color: '#555555', marginLeft: '8px'}}>
                    {t("coordinates")}
                </Typography>
                <div style={{marginTop: '25px'}}>
                        <Button variant="outlined"
                            onClick={() => {
                                let sponsor_form = document.forms.sponsor_form;
                                partner.firstname = '';
                                partner.lastname = '';
                                partner.email = '';
                                sponsor_form.email2.value = '';
                                this.setState({
                                    partner: partner,
                                });
                            }
                            }
                            color="primary"
                            size="medium"
                            fullWidth
                    >
                        {t('newSponsor')}
                    </Button>
                </div>
                <form id="sponsor_form" className={classes.container} noValidate autoComplete="off">
                    <TextField
                        id="firstname"
                        label={t("firstname")}
                        className={classes.textField}
                        onChange={this.handleChange('firstname')}
                        value={partner.firstname}
                        margin="dense"
                        required={true}
                        error={this.state.formErrors === 'firstname'}
                    />
                    <TextField
                        id="lastname"
                        label={t("lastname")}
                        className={classes.textField}
                        onChange={this.handleChange('lastname')}
                        value={partner.lastname}
                        margin="dense"
                        required={true}
                        error={this.state.formErrors === 'lastname'}
                    />
                    <TextField
{'fraction_non_alphanumeric': 0.11458906802988596, 'fraction_numerical': 0.005348014156508062, 'mean_word_length': 1.3648874837269853, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5446175', 'n_tokens_mistral': 3347, 'n_tokens_neox': 3189, 'n_words': 605}