import { WebviewTag } from 'electron';
import React from 'react';

interface IProps {
  onSuccess: () => void;
}

export class LoginWebview extends React.Component<IProps, any> {
  private webview: WebviewTag;

  constructor(props: IProps) {
    super(props);
  }

  componentDidMount() {
    this.webview = this.refs['webview'] as any;

    this.webview.addEventListener('did-stop-loading', this.didStopLoading);
  }

  didStopLoading = (event: any) => {
    if ((event.target.src as string).includes('my-account')) {
      this.props.onSuccess();
    }
  };

  render() {
    return (
      <webview
        ref="webview"
        partition="persist:poe"
        src="https://www.pathofexile.com/login"
        style={{ height: '500px', width: '100%' }}
      />
    );
  }
}
