 ArticleList = require '../../../components/article_list/index.coffee'
 query = require '../query.coffee'
 { API_URL } = require('sharify').data
-Lokka = require('lokka')
-Transport = require('lokka-transport-http')
+request = require 'superagent'
 
 module.exports.QueueView = QueueView = React.createClass
 

    queuedArticles: @props.queuedArticles or []
    scheduledArticles: @props.scheduledArticles or []
    feed: @props.feed or 'scheduled'

  componentDidMount: ->
    headers =
      'X-Access-Token': sd.USER.get('access_token')
    @client = new Lokka
      transport: new Transport(API_URL + '/graphql', {headers})

  saveSelected: (data, isQueued) ->
    article = new Article


  setFeed: (type) ->
    @setState feed: type
    @fetchFeed type

  fetchFeed: (type) ->
    published = type in ['daily_email', 'weekly_email']
    feedQuery = query "#{type}: true, published: #{published}, channel_id: \"#{sd.CURRENT_CHANNEL.id}\""
    request
      .post API_URL + '/graphql'
      .set 'X-Access-Token', sd.USER?.access_token
      .send query: feedQuery
      .end (err, res) =>
        return if err or not res.body?.data
        if type is 'scheduled'
          @setState scheduledArticles: res.body.data.articles
        else
          @setState queuedArticles: res.body.data.articles
          @fetchLatest()

  fetchLatest: ->
    latestQuery = query "channel_id: \"#{sd.CURRENT_CHANNEL.id}\", published: true, sort: \"-published_at\", daily_email: false"
    request
      .post API_URL + '/graphql'
      .set 'X-Access-Token', sd.USER?.access_token
      .send query: latestQuery
      .end (err, res) =>
        return if err or not res.body?.data
        @setState publishedArticles: res.body.data.articles

  render: ->
    div {
