import queryString from 'query-string'
import events from '../core/events'
import * as constants from '../constants'
import { actions } from '../store/actions'
import ReconnectingWebSocket from 'reconnectingwebsocket'

const {
  setDocumentCaptured,
  setToken,
  setAuthenticated
} = actions

export default class Socket {

  connect(jwt) {
    const query = queryString.stringify({ jwt: jwt })
    const url = `${constants.DEV_SOCKET_URL}?${query}`
    const socket = new ReconnectingWebSocket(url)
    socket.onopen = () => {
      this.socket = socket
      this.onMessage()
      setToken(jwt)
      setAuthenticated(true)
    }
  }

  handleData(data) {
    if (data.is_document || data.has_passport) {
      setDocumentCaptured(true)
    }
  }

  onMessage() {
    this.socket.onmessage = (e) => {
      const data = JSON.parse(e.data)
      this.handleData(data)
    }
  }

  sendMessage(message) {
    this.socket.send(message)
  }

}

@@ -681,29 +681,8 @@
 ment
- %7C%7C data.has_passport
 ) %7B%0A
@@ -705,32 +705,100 @@
 tCaptured(true)%0A
+    %7D else if (!data.is_document) %7B%0A      events.emit('noDocument')%0A
     %7D%0A  %7D%0A%0A  onM

Add event callback for no document found