Ejegg has submitted this change and it was merged.

Change subject: Fetch/use user default board for lib widget adds
......................................................................


Fetch/use user default board for lib widget adds

Change-Id: Id4242ee05d6edaa73cfe0d9f3cbea0c2f0797ef5
---
M src/components/app-content/app-content.html
M src/components/app-content/app-content.js
2 files changed, 54 insertions(+), 43 deletions(-)

Approvals:
  Ejegg: Looks good to me, approved



diff --git a/src/components/app-content/app-content.html 
b/src/components/app-content/app-content.html
index c3ac744..5dd94f8 100644
--- a/src/components/app-content/app-content.html
+++ b/src/components/app-content/app-content.html
@@ -31,7 +31,7 @@
                                        <h3 data-bind="text: displayName"></h3>
                                        <p class="widgetDesc" data-bind="text: 
description"></p>
                                        <button class="btn btn-block btn-col 
btn-primary addToBoardBtn" data-bind="attr: { id: 'add-widget-'+id }, click: 
$parent.addWidgetToBoard">Add to my default board</button>
-                                       <button class="hide btn btn-block 
btn-col btn-success addToBoardBtn" data-bind="attr: { id: 'saved-widget-'+id }, 
click: $parent.addWidgetToBoard"><i class="fa fa-check-circle-o"></i> 
Added.</button>
+                                       <button class="hide btn btn-block 
btn-col btn-success addToBoardBtn" data-bind="attr: { id: 'saved-widget-'+id 
}"><i class="fa fa-check-circle-o"></i> Added.</button>
                                </li>
                        </ul>
                </div>
diff --git a/src/components/app-content/app-content.js 
b/src/components/app-content/app-content.js
index a6e51c1..c24ee35 100644
--- a/src/components/app-content/app-content.js
+++ b/src/components/app-content/app-content.js
@@ -2,37 +2,34 @@
     [   'jquery',
         'knockout',
         'text!./app-content.html'
-    ], function( $, ko, templateMarkup) {
+    ], function( $, ko, templateMarkup ) {
 
-    function AppContent(params) {
+    function AppContent( params ) {
         var self = this;
 
-        self.url = params.url || 'hi';
-        self.displayedBoard = ko.observable();
-        self.userBoards = ko.observableArray();
-        self.userdata = ko.observableArray();
-        self.displayPage = ko.observable('Home');
-        self.loggedIn = ko.observable(false);
-        self.welcome = ko.observable('');
-        self.widgetTemplates = ko.observableArray();
-        self.widgetInstances = ko.observableArray();
+        self.url                = params.url || 'hi';
+        self.displayedBoard     = ko.observable();
+        self.userBoards         = ko.observableArray();
+        self.userdata           = ko.observableArray();
+        self.displayPage        = ko.observable('Home');
+        self.loggedIn           = ko.observable(false);
+        self.welcome            = ko.observable('');
+        self.widgetTemplates    = ko.observableArray();
+        self.widgetInstances    = ko.observableArray();
 
-
-
-        //Get user info and configs like default board
-        $.get('/user/info', function(userInfo) {
-            if (userInfo) {
+        $.get( '/user/info', function( userInfo ) {
+            if ( userInfo ) {
                 self.userdata( userInfo );
                 self.welcome( userInfo.name.charAt(0).toUpperCase() + 
userInfo.name.slice(1) );
                 self.loggedIn( true );
 
-                $.get('board/' + self.userdata().defaultBoard, function( 
moredata ){
+                $.get( 'board/' + self.userdata().defaultBoard, function( 
moredata ){
                     self.displayedBoard( moredata );
                 });
 
-                $.get('board/', function (boards){
-                    $.each(boards, function(i, board){
-                        if(board.ownerId === self.userdata().id){
+                $.get( 'board/', function ( boards ){
+                    $.each( boards, function( i, board ){
+                        if( board.ownerId === self.userdata().id ){
                             self.userBoards.push(board);
                         }
                     });
@@ -42,10 +39,7 @@
 
         self.addWidgetToBoard = function( event, data ){
             var widgetIDToAdd;
-            console.log('event: ', event);
-            console.log('data', data.target.id);
 
-            //create an instance of the widget
             $.ajax({
                 method: 'POST',
                 url: '/widget-instance',
@@ -57,35 +51,52 @@
                     isShared: false
                 }),
                 success: function( data ) {
-                    widgetIDToAdd = data.id;
-                    self.displayedBoard().widgets.push(data.id);
+                    widgetIDToAdd = data.id; var gettingBoard, 
defaultBoardConfig;
 
-                    $.ajax({
-                        method: 'PUT',
-                        url: '/board/' + self.userdata().defaultBoard,
-                        contentType: 'application/json; charset=UTF-8',
-                        data: JSON.stringify(self.displayedBoard()),
-                        success: function(stuff) {
-                            //change the look of the add widget button
-                            $('#add-widget-'+event.id).hide();
-                            $('#saved-widget-'+event.id).removeClass('hide');
-                        }
+                    if( self.userdata().defaultBoard !== parseInt( 
self.displayedBoard().id, 10) ){
+                        gettingBoard = $.ajax({
+                                url: '/board/' + self.userdata().defaultBoard,
+                                success: function( stuff ) {
+                                    defaultBoardConfig = stuff;
+                                }
+                            });
+                    } else {
+                        defaultBoardConfig = self.displayedBoard();
+                        gettingBoard = 
$.Deferred().resolve(defaultBoardConfig).promise();
+                    }
+
+                    $.when( gettingBoard ).then( function( returnedData ){
+                        defaultBoardConfig = returnedData;
+                        defaultBoardConfig.widgets.push(data.id);
+
+                        $.ajax({
+                            method: 'PUT',
+                            url: '/board/' + self.userdata().defaultBoard,
+                            contentType: 'application/json; charset=UTF-8',
+                            data: JSON.stringify( defaultBoardConfig ),
+                            success: function( stuff ) {
+                                //change the look of the add widget button
+                                $( '#add-widget-' + event.id ).hide();
+                                $( '#saved-widget-' + event.id ).removeClass( 
'hide' );
+                            }
+                        });
                     });
+
+
                 }
             });
 
         };
 
-        self.setDisplayPage = function(e, data){
-            var pages = ['Library', 'Profile', 'Home'], view = data.target.id;
+        self.setDisplayPage = function( e, data ){
+            var pages = [ 'Library', 'Profile', 'Home' ], view = 
data.target.id;
+
             if( pages.indexOf(data.target.id) > -1 ){
                 self.displayPage(view);
             } else if( isNaN( parseInt( view, 10 ) ) ) {
                 self.displayPage($.trim($(data.target).text()));
             } else {
-                $.get('board/' + view, function( bdata ){
-                    console.log('get board #', data.target.id);
-                    console.log('bdata: ', bdata);
+                $.get( 'board/' + view, function( bdata ){
                     self.displayedBoard( bdata );
                 });
             }
@@ -93,7 +104,7 @@
         };
 
         self.getWidgetTemplates = function(){
-            $.get('/widget', function(widgetTemplates){
+            $.get( '/widget', function( widgetTemplates ){
 
                 var wt = $.map(widgetTemplates, function(n){
                     return n;
@@ -105,7 +116,7 @@
         self.getWidgetTemplates();
 
         self.getUsersWidgetInstances = function(){
-            $.get('/widget-instance', function(widgetInstances){
+            $.get('/widget-instance', function( widgetInstances ){
 
                 var wi = $.map(widgetInstances, function(n){
                     return n;

-- 
To view, visit https://gerrit.wikimedia.org/r/196664
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Id4242ee05d6edaa73cfe0d9f3cbea0c2f0797ef5
Gerrit-PatchSet: 4
Gerrit-Project: wikimedia/fundraising/dash
Gerrit-Branch: master
Gerrit-Owner: Ssmith <ssm...@wikimedia.org>
Gerrit-Reviewer: Ejegg <eeggles...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to