From d5015132577bc84513707ee815243fd76d88271a Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 27 Mar 2015 11:49:26 +1030 Subject: [PATCH] Refresh avatar display after uploading --- .../app/components/user/avatar-editor.js | 24 ++++++++++++++----- .../core/ember/app/helpers/user-avatar.js | 2 +- .../app/initializers/inject-components.js | 1 + .../Api/Actions/Users/UploadAvatarAction.php | 8 +++++-- framework/core/src/Core/Models/User.php | 10 ++++++++ 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/framework/core/ember/app/components/user/avatar-editor.js b/framework/core/ember/app/components/user/avatar-editor.js index 1c742b7ff..ccaae2b57 100644 --- a/framework/core/ember/app/components/user/avatar-editor.js +++ b/framework/core/ember/app/components/user/avatar-editor.js @@ -9,12 +9,15 @@ export default Ember.Component.extend({ classNames: ['avatar-editor', 'dropdown'], classNameBindings: ['loading'], - click: function(e) { - if (! this.get('user.avatarUrl')) { - e.preventDefault(); - e.stopPropagation(); - this.send('upload'); - } + didInsertElement: function() { + var component = this; + this.$('.dropdown-toggle').click(function(e) { + if (! component.get('user.avatarUrl')) { + e.preventDefault(); + e.stopPropagation(); + component.send('upload'); + } + }); }, actions: { @@ -37,9 +40,18 @@ export default Ember.Component.extend({ processData: false, complete: function() { component.set('loading', false); + }, + success: function(data) { + Ember.run.next(function() { + component.get('store').pushPayload(data); + }); } }); }); + }, + + remove: function() { + this.get('store').push('user', {id: this.get('user.id'), avatarUrl: null}); } } }); diff --git a/framework/core/ember/app/helpers/user-avatar.js b/framework/core/ember/app/helpers/user-avatar.js index eb5ca54fb..257952c2b 100644 --- a/framework/core/ember/app/helpers/user-avatar.js +++ b/framework/core/ember/app/helpers/user-avatar.js @@ -18,5 +18,5 @@ export default Ember.Handlebars.makeBoundHelper(function(user, options) { } return new Ember.Handlebars.SafeString(''+content+''); -}); +}, 'avatarUrl', 'username', 'color'); diff --git a/framework/core/ember/app/initializers/inject-components.js b/framework/core/ember/app/initializers/inject-components.js index d0dd4c7f6..2061cb641 100644 --- a/framework/core/ember/app/initializers/inject-components.js +++ b/framework/core/ember/app/initializers/inject-components.js @@ -6,5 +6,6 @@ export default { application.inject('component', 'composer', 'controller:composer') application.inject('model', 'session', 'simple-auth-session:main') application.inject('component', 'session', 'simple-auth-session:main') + application.inject('component', 'store', 'store:main') } }; diff --git a/framework/core/src/Api/Actions/Users/UploadAvatarAction.php b/framework/core/src/Api/Actions/Users/UploadAvatarAction.php index 73541abb4..417a0e9a6 100644 --- a/framework/core/src/Api/Actions/Users/UploadAvatarAction.php +++ b/framework/core/src/Api/Actions/Users/UploadAvatarAction.php @@ -2,6 +2,7 @@ use Flarum\Api\Actions\BaseAction; use Flarum\Core\Commands\UploadAvatarCommand; +use Flarum\Api\Serializers\UserSerializer; use Illuminate\Http\Request; class UploadAvatarAction extends BaseAction @@ -11,11 +12,14 @@ class UploadAvatarAction extends BaseAction $userId = array_get($routeParams, 'id'); $file = $request->file('avatar'); - $this->dispatch( + $user = $this->dispatch( new UploadAvatarCommand($userId, $file, $this->actor->getUser()), $routeParams ); - return $this->respondWithoutContent(201); + $serializer = new UserSerializer; + $document = $this->document()->setData($serializer->resource($user)); + + return $this->respondWithDocument($document); } } diff --git a/framework/core/src/Core/Models/User.php b/framework/core/src/Core/Models/User.php index 578491041..55f7527e6 100755 --- a/framework/core/src/Core/Models/User.php +++ b/framework/core/src/Core/Models/User.php @@ -226,6 +226,16 @@ class User extends Model return $this; } + /** + * Get the URL of the user's avatar. + * + * @return string + */ + public function getAvatarUrlAttribute() + { + return $this->avatar_path ? asset('avatars/'.$this->avatar_path) : null; + } + /** * Check if a given password matches the user's password. *