Commit 5e3900bea137e3493e74b21f4b9588509dfb36ab

Authored by OpenWapp Developer
1 parent da5fdfe65f

# This is a combination of 2 commits.

# The first commit's message is:

Auto fill profile data from WA server if exists

# The 2nd commit message will be skipped:

#	Comments addressed

Showing 3 changed files with 88 additions and 16 deletions

app/scripts/models/auth.js View file @ 5e3900b
... ... @@ -176,6 +176,42 @@
176 176 );
177 177 },
178 178  
  179 + _onceLogged: function (callback) {
  180 + if (!callback) { return; }
  181 + if (global.client.isOnline) {
  182 + callback.call(this);
  183 + } else {
  184 + this.once('login:success', callback.call(this));
  185 + this.checkCredentials();
  186 + }
  187 + },
  188 +
  189 + getProfilePicture: function (callback) {
  190 + this._onceLogged(function () {
  191 + global.client.getContactPicture(this.get('msisdn'),
  192 + function (error, pictureId, picture) {
  193 + if (error) {
  194 + callback(error);
  195 + return;
  196 + }
  197 + callback(null, picture);
  198 + });
  199 + });
  200 + },
  201 +
  202 + getProfileStatus: function (callback) {
  203 + this._onceLogged(function () {
  204 + global.client.getContactsState([this.get('msisdn')],
  205 + function (error, phoneMap) {
  206 + if (error) {
  207 + callback(error);
  208 + return;
  209 + }
  210 + callback(null, phoneMap && phoneMap[this.get('msisdn')]);
  211 + }.bind(this));
  212 + });
  213 + },
  214 +
179 215 updateProfileData: function (screenName, status, photo, thumb) {
180 216 this.set({
181 217 screenName: screenName,
... ... @@ -189,7 +225,7 @@
189 225 this._sendProfileData();
190 226 }
191 227 else {
192   - this.on('login:success', this._sendProfileData);
  228 + this.once('login:success', this._sendProfileData);
193 229 }
194 230 },
195 231  
app/scripts/templates/profile.hbs View file @ 5e3900b
... ... @@ -28,14 +28,15 @@
28 28 <p>
29 29 <input type="text"
30 30 name="screen-name" placeholder="{{translate 'screenNamePlaceHolder'}}"
31   - maxlength="30" required value="{{screenName}}">
  31 + maxlength="30" required value="{{screenName}}"
  32 + id="input-screen-name">
32 33 </input>
33 34 </p>
34 35 <p>{{translate 'screenNameDescription'}}</p>
35 36 <p>
36 37 <input type="text"
37 38 name="status" placeholder="{{translate 'statusPlaceHolder'}}" required
38   - value="{{status}}">
  39 + value="{{status}}" id="input-status">
39 40 </input>
40 41 </p>
41 42 <p>{{translate 'statusDescription'}}</p>
app/scripts/views/profile.js View file @ 5e3900b
... ... @@ -27,6 +27,7 @@
27 27 initialize: function () {
28 28 this.picture = this.model.get('photo');
29 29 this.thumb = this.model.get('thumb');
  30 + this.updateProfileDataFromServer();
30 31 },
31 32  
32 33 render: function () {
... ... @@ -60,6 +61,33 @@
60 61 button.prop('disabled', name.length < 3);
61 62 },
62 63  
  64 + updateProfileDataFromServer: function () {
  65 + var _this = this;
  66 +
  67 + global.auth.getProfilePicture(function (error, picture) {
  68 + if (error || !picture) {
  69 + return;
  70 + }
  71 + _this.generatePicture(picture, function (err, resizedPic) {
  72 + if (err) { return; }
  73 + _this.picture = resizedPic;
  74 + _this._replacePhoto(resizedPic);
  75 + });
  76 +
  77 + _this.generateThumbnail(picture, function (err, thumb) {
  78 + if (err) { return; }
  79 + _this.thumb = thumb;
  80 + });
  81 + });
  82 +
  83 + global.auth.getProfileStatus(function (error, status) {
  84 + if (error || !status) {
  85 + return;
  86 + }
  87 + _this.$el.find('#input-status').attr('value', status);
  88 + });
  89 + },
  90 +
63 91 updateProfileData: function (evt) {
64 92 evt.preventDefault();
65 93  
66 94  
67 95  
... ... @@ -83,19 +111,16 @@
83 111 var _this = this;
84 112 requestPicture.onsuccess = function () {
85 113 var picture = requestPicture.result.blob;
86   - Thumbnail.setMaxSize(_this.PICTURE_MAX_SIZE);
87   - Thumbnail.generate(picture, function (err, picture) {
  114 + _this.generatePicture(picture, function (err, resizedPic) {
88 115 if (err) { return; }
89   - _this.picture = picture;
90   - _this._replacePhoto(picture);
  116 + _this.picture = resizedPic;
  117 + _this._replacePhoto(resizedPic);
  118 + });
91 119  
92   - // Generate thumb
93   - Thumbnail.setMaxSize(_this.THUMB_MAX_SIZE);
94   - Thumbnail.generate(picture, function (err, thumb) {
95   - if (err) { return; }
96   - _this.thumb = thumb;
97   - }, { asBlob: true });
98   - }, { asBlob: true });
  120 + _this.generateThumbnail(picture, function (err, thumb) {
  121 + if (err) { return; }
  122 + _this.thumb = thumb;
  123 + });
99 124 };
100 125  
101 126 requestPicture.onerror = function () {
... ... @@ -103,6 +128,16 @@
103 128 };
104 129 },
105 130  
  131 + generatePicture: function (picture, callback) {
  132 + Thumbnail.setMaxSize(this.PICTURE_MAX_SIZE);
  133 + Thumbnail.generate(picture, callback, { asBlob: true });
  134 + },
  135 +
  136 + generateThumbnail: function (picture, callback) {
  137 + Thumbnail.setMaxSize(this.THUMB_MAX_SIZE);
  138 + Thumbnail.generate(picture, callback, { asBlob: true });
  139 + },
  140 +
106 141 showSelect: function () {
107 142 var $select = this.$el.find('select');
108 143 $select.focus();
... ... @@ -121,7 +156,7 @@
121 156 _replacePhoto: function (newPictureBlob) {
122 157 this.clear();
123 158 if (newPictureBlob instanceof window.Blob) {
124   - this.$el.find('img')
  159 + this.$el.find('#profile-picture')
125 160 .attr('src', window.URL.createObjectURL(newPictureBlob));
126 161 }
127 162 },