<commit_before>import * as AWS from 'aws-sdk';
import * as React from 'react';
import * as xml2js from 'xml2js';
import { RadioGroup, RadioButton } from 'react-toolbox/lib/radio';

interface numbersState {
  phoneNumbers: { [name: string]: string } | null
  activeNumber: string | null
}

export default class extends React.Component<{}, numbersState> {
  private s3: AWS.S3;

  constructor() {
    super();
    this.state = { phoneNumbers: null, activeNumber: null }
  }

  async componentDidMount() {
    this.s3 = new AWS.S3({ apiVersion: '2006-03-01' });
    const [phoneNumbersRes, numbersRes] = await Promise.all([
      this.s3.getObject({ Bucket: 'number-switcher-4000', Key: 'phone-numbers.json' }).promise(),
      this.s3.getObject({ Bucket: 'number-switcher-4000', Key: 'number.xml' }).promise()
    ]);

    if (!phoneNumbersRes.Body) {
      throw new Error('No body');
    }
    if (!Buffer.isBuffer(phoneNumbersRes.Body)) {
      throw new Error('Body is not a buffer');
    }
    const phoneNumbers = JSON.parse(phoneNumbersRes.Body.toString('utf-8'));
    this.setState({ phoneNumbers })

    if (!numbersRes.Body) {
      throw new Error('No numbers body');
    }
    if (!Buffer.isBuffer(numbersRes.Body)) {
      throw new Error('Number body is not a buffer');
    }
    const numbersBody: Buffer = numbersRes.Body;
    const currentNumberXml: any = await new Promise((resolve, reject) => {
      xml2js.parseString(numbersBody, (err, result) => {
        if (err) {
          reject(err);
          return;
        }
        resolve(result);
      });
    });
    const currentNumer = currentNumberXml.Response && (currentNumberXml.Response.Dial && currentNumberXml.Response.Dial[0]) || (currentNumberXml.Response.Play && 'auto-dial-in');
    this.setState({ activeNumber: currentNumer });
  }

  onAutoDialIn = async () => {
    await this.updateS3(`<?xml version="1.0" encoding="UTF-8"?><Response><Play digits="6"></Play></Response>`);
    this.setState({ activeNumber: 'auto-dial-in' });
  }

  onUpdateNumber = async (number: string) => {
    await this.updateS3(`<?xml version="1.0" encoding="UTF-8"?><Response><Dial>${number}</Dial></Response>`);
    this.setState({ activeNumber: number });
  }

  async updateS3(body: string) {
    return await this.s3.putObject({
      Bucket: 'number-switcher-4000',
      Key: 'number.xml',
      Body: body,
      ContentType: 'application/xml'
    }).promise();
  }

  render() {
    if (!this.state.phoneNumbers) {
      return <p>Loading...</p>;
    }
    const phoneNumbers = this.state.phoneNumbers;
    return (
      <RadioGroup name="number" value={this.state.activeNumber}>
        {Object.keys(phoneNumbers).map(name => (
          <RadioButton key={name} label={`${name}: ${phoneNumbers[name]}`} value={phoneNumbers[name]} onClick={() => this.onUpdateNumber(phoneNumbers[name])} />
        ))}
        <RadioButton label="Automatically dial in" value="auto-dial-in" onClick={this.onAutoDialIn} />
      </RadioGroup>
    );
  }
}<commit_msg>Fix radio button selection<commit_after>import * as AWS from 'aws-sdk';
import * as React from 'react';
import * as xml2js from 'xml2js';
import { RadioGroup, RadioButton } from 'react-toolbox/lib/radio';

interface numbersState {
  phoneNumbers: { [name: string]: string } | null
  activeNumber: string | null
}

export default class extends React.Component<{}, numbersState> {
  private s3: AWS.S3;

  constructor() {
    super();
    this.state = { phoneNumbers: null, activeNumber: null }
  }

  async componentDidMount() {
    this.s3 = new AWS.S3({ apiVersion: '2006-03-01' });
    const [phoneNumbersRes, numbersRes] = await Promise.all([
      this.s3.getObject({ Bucket: 'number-switcher-4000', Key: 'phone-numbers.json' }).promise(),
      this.s3.getObject({ Bucket: 'number-switcher-4000', Key: 'number.xml' }).promise()
    ]);

    if (!phoneNumbersRes.Body) {
      throw new Error('No body');
    }
    if (!Buffer.isBuffer(phoneNumbersRes.Body)) {
      throw new Error('Body is not a buffer');
    }
    const phoneNumbers = JSON.parse(phoneNumbersRes.Body.toString('utf-8'));
    this.setState({ phoneNumbers })

    if (!numbersRes.Body) {
      throw new Error('No numbers body');
    }
    if (!Buffer.isBuffer(numbersRes.Body)) {
      throw new Error('Number body is not a buffer');
    }
    const numbersBody: Buffer = numbersRes.Body;
    const currentNumberXml: any = await new Promise((resolve, reject) => {
      xml2js.parseString(numbersBody, (err, result) => {
        if (err) {
          reject(err);
          return;
        }
        resolve(result);
      });
    });
    const currentNumer = currentNumberXml.Response && (currentNumberXml.Response.Dial && currentNumberXml.Response.Dial[0]) || (currentNumberXml.Response.Play && 'auto-dial-in');
    this.setState({ activeNumber: currentNumer });
  }

  onNumberChange = async (number: string) => {
    this.setState({ activeNumber: number });
    const body = number === 'auto-dial-in' ?
      `<?xml version="1.0" encoding="UTF-8"?><Response><Play digits="6"></Play></Response>` :
      `<?xml version="1.0" encoding="UTF-8"?><Response><Dial>${number}</Dial></Response>`;
    await this.s3.putObject({
      Bucket: 'number-switcher-4000',
      Key: 'number.xml',
      Body: body,
      ContentType: 'application/xml'
    }).promise();
  }

  render() {
    if (!this.state.phoneNumbers) {
      return <p>Loading...</p>;
    }
    const phoneNumbers = this.state.phoneNumbers;
    return (
      <RadioGroup name="number" value={this.state.activeNumber} onChange={this.onNumberChange}>
        {Object.keys(phoneNumbers).map(name => (
          <RadioButton key={name} label={`${name}: ${phoneNumbers[name]}`} value={phoneNumbers[name]} />
        ))}
        <RadioButton label="Automatically dial in" value="auto-dial-in" />
      </RadioGroup>
    );
  }
}
{'fraction_non_alphanumeric': 0.1647940074906367, 'fraction_numerical': 0.013789581205311542, 'mean_word_length': 3.2945906432748537, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 4, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, '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': '487446', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1820, 'n_words': 392}