Skip to content

Commit

Permalink
Broken?
Browse files Browse the repository at this point in the history
  • Loading branch information
samuele-mattiuzzo committed Jan 9, 2016
1 parent e0960ff commit 00e6a9e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
14 changes: 3 additions & 11 deletions lib/atom-slack-snippets.coffee
@@ -1,6 +1,5 @@
request = require 'request-promise'
{CompositeDisposable} = require 'atom'
AtomSlackSnippetsView = require './select-token-view'
SelectTokenView = require './select-token-view'


module.exports =
Expand All @@ -10,15 +9,8 @@ module.exports =
type: 'array'
default: []
items:
type: 'object'
properties:
name:
type: 'string'
default: ''
token:
type: 'string'
default: ''

type: 'string'
default: ''

activate: (state) ->
@subscriptions = new CompositeDisposable
Expand Down
6 changes: 4 additions & 2 deletions lib/post-view.coffee
Expand Up @@ -4,7 +4,7 @@ request = require 'request-promise'

module.exports =

class PostView extends View
class PostView extends SelectListView
# fetches the selected text and posts to the channel (item)
# using token
initialize: (item, token)->
Expand Down Expand Up @@ -32,9 +32,10 @@ class PostView extends View
json: true })
.then( (body)=>
if body['ok'] == false
# handle error message
console.log body['error']
else
@panel.hide()
# handle success message
)
.catch( (err) => console.log err )

Expand All @@ -45,6 +46,7 @@ class PostView extends View
json: true })
.then( (body)=>
if body['ok'] == false
# handle error message
console.log body['error']
else
@_postToChannel(body['channel']['id'])
Expand Down
30 changes: 20 additions & 10 deletions lib/select-channel-view.coffee
Expand Up @@ -15,11 +15,8 @@ class SelectChannelView extends SelectListView
@channels = []
@users = []

@addClass 'overlay from-top'
@setItems @_createItems()
@panel ?= atom.workspace.addModalPanel(item: @)
@panel.show()
@focusFilterEditor()
@_create()


viewForItem: (item) -> "<li>#{ item.name }</li>"

Expand All @@ -30,24 +27,33 @@ class SelectChannelView extends SelectListView
cancelled: -> @panel.hide()

# PRIVATE METHODS
_createItems: ->
_create: ->
@_getAllItems()
items = []
for i in [channels..., users...]
[ch, u] = [null, null]
while not ch? and not u?
[ch, u] = [@channels, @users]
for i in [ch..., u...]
[v, t] = if i.profile? then [i.profile.real_name, 'user'] else [i.name, 'channel']
items.push({id:i.id, name:v, type:t})
items
@setItems items

@addClass 'overlay from-top'
@panel ?= atom.workspace.addModalPanel(item: @)
@panel.show()
@focusFilterEditor()

_getAllItems: ->
# loops till we have channels and users to post to,
# then quits
# needs improvement or better logic elsewhere, otherwise blocks
# atom's loading for a bit too much
if @channels.length == 0 and @users.length == 0
console.log('fetching')
@_getChannels()
@_getUsers()
setTimeout @_getAllItems.bind(@), 5 * 1000
timer = setTimeout @_getAllItems.bind(@), 5 * 1000
else
clearTimeout timer

_getChannels: ->
request({
Expand All @@ -56,8 +62,10 @@ class SelectChannelView extends SelectListView
json: true })
.then( (body)=>
if body['ok'] == false
# handle error message
console.log(body['error'])
else
console.log('got the chans')
@channels = body['channels']
)
.catch( (err) => console.log(err) )
Expand All @@ -69,8 +77,10 @@ class SelectChannelView extends SelectListView
json: true })
.then( (body)=>
if body['ok'] == false
# handle error message
console.log(body['error'])
else
console.log('got the users')
@users = body['members']
)
.catch( (err) => console.log(err) )
9 changes: 7 additions & 2 deletions lib/select-token-view.coffee
@@ -1,4 +1,3 @@
request = require 'request-promise'
{SelectListView} = require 'atom-space-pen-views'
SelectChannelView = require './select-channel-view'

Expand Down Expand Up @@ -26,4 +25,10 @@ class SelectTokenView extends SelectListView

cancelled: -> @panel.hide()

_createItems: -> atom.config.get('atom-slack-snippets.tokens')
_createItems: ->
tokens = atom.config.get('atom-slack-snippets.tokens')
items = []
for item in tokens
[n, t] = item.split "|"
items.push({'name': n, 'token': t})
items

0 comments on commit 00e6a9e

Please sign in to comment.