<commit_before>import React from 'react'
import Header from '../components/Header'
import NProgress from 'nprogress'
import Router from 'next/router'
import LinkCard from '../components/LinkCard'
import { initGA, logPageView } from '../lib/analytics'
import db from '../lib/db'

import SecretPage from '../hocs/SecretPage'

class SubmitLink extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      info: 'Simply give us the URL we will fetch metadata and show preview before you can submit it to linklet',
      url: '',
      showPreview: false,
      linkData: {},
      error: ''
    }
  }
  componentDidMount () {
    initGA()
    logPageView()
  }
  handleFetch () {
    let url = this.state.url
    if (url && !/^https?:\/\//i.test(url)) {
      url = 'http://' + url
    }
    NProgress.start()
    db
      .getMetaData(url)
      .then(({ data }) => {
        NProgress.done()
        this.setState({
          info: 'Click Save to submit link to linklet',
          linkData: data,
          showPreview: true,
          error: ''
        })
      })
      .catch(e => {
        NProgress.done()
        console.log(e.response)
        this.setState({ error: e.response.data.message })
      })
  }
  handleSave () {
    console.log('---Saving---')
    NProgress.start()
    db
      .saveLink(this.state.linkData)
      .then(link => {
        NProgress.done()
        Router.push('/my-links')
      })
      .catch(e => {
        NProgress.done()
        let message
        console.log(e.response)
        if (e.response.data.code === 11000) {
          message =
            'Sorry, its seems like this link already exist in linklet!...'
        } else {
          message = e.message
        }
        console.log(message)
        this.setState({ error: message })
      })
  }
  handleBack () {
    this.setState({
      info: 'Simply give us the URL we will fetch metadata and show preview before you can submit it to linklet',
      linkData: {},
      showPreview: false,
      error: ''
    })
  }
  render () {
    return (
      <div className='SubmitLink'>
        <Header
          user={this.props.user}
          url={this.props.url}
          title='Linklet | SubmitLink'
        />
        <main>
          <p className='error'>
            {this.state.error}
          </p>
          {!this.state.showPreview &&
            <div className='card'>
              <div className='group'>
                <input
                  onChange={e => {
                    this.setState({ url: e.target.value })
                  }}
                  value={this.state.url}
                  type='text'
                  required
                />
                <span className='highlight' />
                <span className='bar' />
                <label>URL</label>
              </div>
              <div className='group'>
                <button onClick={this.handleFetch.bind(this)}>Fetch</button>
              </div><commit_msg>closes #14<commit_after>import React from 'react'
import Header from '../components/Header'
import NProgress from 'nprogress'
import Router from 'next/router'
import LinkCard from '../components/LinkCard'
import { initGA, logPageView } from '../lib/analytics'
import db from '../lib/db'

import SecretPage from '../hocs/SecretPage'

class SubmitLink extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      info: 'Simply give us the URL we will fetch metadata and show preview before you can submit it to linklet',
      url: '',
      showPreview: false,
      linkData: {},
      error: ''
    }
  }
  componentDidMount () {
    initGA()
    logPageView()
    this.input.focus()
  }
  handleFetch (e) {
    e && e.preventDefault()
    let url = this.state.url
    if (url && !/^https?:\/\//i.test(url)) {
      url = 'http://' + url
    }
    NProgress.start()
    db
      .getMetaData(url)
      .then(({ data }) => {
        NProgress.done()
        this.setState({
          info: 'Click Save to submit link to linklet',
          linkData: data,
          showPreview: true,
          error: ''
        })
      })
      .catch(e => {
        NProgress.done()
        console.log(e.response)
        this.setState({ error: e.response.data.message })
      })
  }
  handleSave () {
    console.log('---Saving---')
    NProgress.start()
    db
      .saveLink(this.state.linkData)
      .then(link => {
        NProgress.done()
        Router.push('/my-links')
      })
      .catch(e => {
        NProgress.done()
        let message
        console.log(e.response)
        if (e.response.data.code === 11000) {
          message =
            'Sorry, its seems like this link already exist in linklet!...'
        } else {
          message = e.message
        }
        console.log(message)
        this.setState({ error: message })
      })
  }
  handleBack () {
    this.setState({
      info: 'Simply give us the URL we will fetch metadata and show preview before you can submit it to linklet',
      linkData: {},
      showPreview: false,
      error: ''
    })
  }
  render () {
    return (
      <div className='SubmitLink'>
        <Header
          user={this.props.user}
          url={this.props.url}
          title='Linklet | SubmitLink'
        />
        <main>
          <p className='error'>
            {this.state.error}
          </p>
          {!this.state.showPreview &&
            <div className='card'>
              <form onSubmit={this.handleFetch.bind(this)}>
                <div className='group'>
                  <input
                    ref={node => (this.input = node)}
                    onChange={e => {
                      this.setState({ url: e.target.value })
                    }}
                    value={this.state.url}
                    type='text'
                    required
                  />
                  <span className='highlight' />
                  <span className='bar' />
                  <label>URL</label>
                </div>
                <div className='group'>
                  <button type='submit'>Fetch</button>
                </div>
              </form>
{'fraction_non_alphanumeric': 0.13407730874979784, 'fraction_numerical': 0.0019408054342552159, 'mean_word_length': 1.5564282761471682, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 50, 'https://': 0, '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': '312114', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1738, 'n_words': 416}