add save functionality to ssh-keys

This commit is contained in:
Lisa Passing 2015-07-21 14:10:37 +02:00
parent 87ddf9979a
commit 7293e06c0e
2 changed files with 40 additions and 26 deletions

View File

@ -1,4 +1,4 @@
`import Ember from 'ember'`
# `import Ember from 'ember'`
AddSshKeyComponent = Ember.Component.extend
@ -7,52 +7,63 @@ AddSshKeyComponent = Ember.Component.extend
store: Ember.inject.service()
isSaving: false
didInsertElement: () ->
id = @get('repo.id')
model = @get('store').recordForId('sshKey', id)
# TODO: this can be removed in favor of simply unloading record
# once https://github.com/emberjs/data/pull/2867
# and https://github.com/emberjs/data/pull/2870 are merged
if model
@get('store').dematerializeRecord(model)
typeMap = @get('store').typeMapFor('sshKey')
idToRecord = typeMap.idToRecord
delete idToRecord[id]
model = @get('store').createRecord('sshKey', id: id)
@set('model', model)
isValid: () ->
true
if Ember.isBlank(@get('value'))
this.set('valueError', 'Value can\'t be blank.')
false
else
true
reset: ->
@setProperties(description: null, value: null)
valueChanged: (->
this.set('valueError', false)
).observes('value')
addErrorsFromResponse: (errArr) ->
error = errArr[0]
if error.code == 'not_a_private_key'
this.set('valueError', 'This key is not a private key.')
else if error.code == 'key_with_a_passphrase'
this.set('valueError', 'We can\'t use key with a passphrase.')
actions:
add: ->
# not sure how to integrate that with 'save' in the compotent
id = @get('repo.id')
model = @store.recordForId('sshKey', id)
# TODO: this can be removed in favor of simply unloading record
# once https://github.com/emberjs/data/pull/2867
# and https://github.com/emberjs/data/pull/2870 are merged
if model
@store.dematerializeRecord(model)
typeMap = @store.typeMapFor('sshKey')
idToRecord = typeMap.idToRecord
delete idToRecord[id]
model = @store.createRecord('sshKey', id: id)
@set('model', model)
@set('isEditing', true)
save: ->
this.set('valueError', false)
return if @get('isSaving')
@set('isSaving', true)
if @isValid()
ssh_key = @get('store').createRecord('ssh_key',
ssh_key = @get('model').setProperties(
description: @get('description')
value: @get('value')
fingerprint: 'I dont know?'
repo: @get('repo')
)
self = this
ssh_key.save().then =>
@set('isSaving', false)
@reset()
, =>
, (error) =>
@set('isSaving', false)
if error.errors
@addErrorsFromResponse(error.errors)
# if error.errors
# @addErrorsFromResponse(error.errors)
else
@set('isSaving', false)

View File

@ -4,6 +4,9 @@
</div>
<div class="form-elem">
{{textarea value=value class="ssh-value" rows="10" placeholder="SSH Key"}}
{{#if valueError}}
<p>{{valueError}}</p>
{{/if}}
</div>
<div class="form-elem">
{{#if isSaving}}