import {extractGithubUrl, renderError, cinvoke} from '../lib/utils'
import {all as qall} from 'q'

const cmp = (one, other) => 0 + (one < other) - (one > other)
const cmpKey = key => (one, other) => cmp(one[key], other[key])

export function index(req, res) {
  const path = extractGithubUrl(req.query.url)
  if (path) { return res.redirect(path) }

  const client = req.gh.client

  // client.me().info(function(err, me, headers) {  })

  if (!client) { return res.render('index') }

  cinvoke(client.me(), 'orgs')
  .then(orgs => orgs.map(org => client.org(org.login)).concat(client.me()))
  .then(users => qall(users.map(user => cinvoke(user, 'repos'))))
  .then(repos => [].concat.apply([], repos))
  .then(repos => repos.sort(cmpKey('pushed_at')))
  .then(repos => res.render('index', {repos}))
  .catch(renderError(res))
}

import {extractGithubUrl, renderError, cinvoke} from '../lib/utils'
import {all as qall} from 'q'

const cmp = (one, other) => 0 + (one < other) - (one > other)
const cmpKey = key => (one, other) => cmp(one[key], other[key])

export async function index(req, res) {
  const path = extractGithubUrl(req.query.url)
  if (path) { return res.redirect(path) }

  const client = req.gh.client

  if (!client) { return res.render('index') }

  try {
    var repos = await cinvoke(client.me(), 'repos')
    repos = repos.sort(cmpKey('pushed_at'))
    res.render('index', {repos})
  }
  catch (error) {
    renderError(res, error)
  }
}

Fix double rendering projects. First async function :tada:
Fix double rendering projects. First async function :tada:
