Skip to content

Commit

Permalink
improvements to list saving
Browse files Browse the repository at this point in the history
  • Loading branch information
takeit committed Mar 19, 2015
1 parent 033ca23 commit 7553d46
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,12 @@ angular.module('playlistsApp').controller('PlaylistsController', [
* @param {Object} list Playlist
*/
$scope.setPlaylistInfoOnChange = function (list) {
$scope.featuredArticles = Playlist.getArticlesByListId(list);
Playlist.getArticlesByListId(list).then(function (data) {
$scope.featuredArticles = data.items;
}, function(response) {
flashMessage(Translator.trans('Could not refresh the list'), 'error');
});

Playlist.setListId(list.id);
$scope.playlistInfo = list;
$scope.playlist.selected.oldLimit = list.maxItems;
Expand Down Expand Up @@ -503,50 +508,13 @@ angular.module('playlistsApp').controller('PlaylistsController', [
}
}

/**
* Saves, updates the list and make post actions on promise resolve, such
* clearing log list, showing/hiding flash messages etc.
*/
var saveList = function () {
var flash = flashMessage(Translator.trans('Processing', {}, 'messages'), null, true);
$scope.processing = true;

doSaveAPICalls().then(function (response) {
flash.fadeOut();
$scope.processing = false;
Playlist.clearLogList();
flashMessage(Translator.trans('List saved'));
$scope.featuredArticles = Playlist.getArticlesByListId({id: Playlist.getListId(), maxItems: $scope.playlist.selected.maxItems});
$scope.playlist.selected.id = Playlist.getListId();
$scope.page = 2;
$scope.isRunning = false;
$scope.isEmpty = false;

if (response && response[0] !== undefined && response[0].object.articlesModificationTime !== undefined) {
$scope.playlist.selected.articlesModificationTime = response[0].object.articlesModificationTime;
}
}, function(response) {
if (response.errors[0].code === 409) {
flashMessage(Translator.trans(
'This list is already in a different state than the one in which it was loaded.'
), 'error');
// automatically refresh playlist
$scope.featuredArticles = Playlist.getArticlesByListId({id: Playlist.getListId()});
} else {
flashMessage(Translator.trans('Could not save the list'), 'error');
}
flash.fadeOut();
$scope.processing = false;
});
}

/**
* Saves, updates playlist with all articles on server side.
* It makes batch link or unlink of the articles. It also
* saves a proper order of the articles. All list's changes are saved
* by clicking Save button.
*/
var doSaveAPICalls = function () {
var saveList = function () {
var deferred,
listname,
logList = [],
Expand All @@ -565,24 +533,91 @@ angular.module('playlistsApp').controller('PlaylistsController', [
update = true;
}

deferred = $q.defer();
list = deferred;
$scope.playlist.selected.title = listname;
var flash = flashMessage(Translator.trans('Processing', {}, 'messages'), null, true);
$scope.processing = true;

if (!playlistExists && !update && $scope.playlist.selected.id === undefined) {
return Playlist.createPlaylist($scope.playlist.selected);
Playlist.createPlaylist($scope.playlist.selected).then(function (response) {
$scope.playlist.selected.id = response.id;
logList = Playlist.getLogList();
if (logList.length == 0) {
afterSave(response);
flash.fadeOut();
return;
}

Playlist.batchUpdate(logList, $scope.playlist.selected).then(function (data) {
afterSave(data);
flash.fadeOut();
}, function(response) {
flash.fadeOut();
afterSaveError(response);
});
}, function(response) {
flash.fadeOut();
afterSaveError(response);
});

return;
}

if ($scope.playlist.selected !== undefined) {
list = Playlist.updatePlaylist($scope.playlist.selected);
deferred.resolve();
Playlist.updatePlaylist($scope.playlist.selected).then(function (response) {
logList = Playlist.getLogList();
if (logList.length == 0) {
afterSave(response);
flash.fadeOut();
return;
}

Playlist.batchUpdate(logList, $scope.playlist.selected).then(function (data) {
afterSave(data);
flash.fadeOut();
}, function(response) {
flash.fadeOut();
afterSaveError(response);
});
}, function(response) {
flash.fadeOut();
afterSaveError(response);
});
}
}

logList = Playlist.getLogList();
if (logList.length == 0) {
return list;
var afterSave = function (response) {
$scope.processing = false;
Playlist.clearLogList();
flashMessage(Translator.trans('List saved'));
Playlist.getArticlesByListId({id: Playlist.getListId()}).then(function (data) {
$scope.featuredArticles = data.items;
}, function(response) {
flashMessage(Translator.trans('Could not refresh the list'), 'error');
});

$scope.playlist.selected.id = Playlist.getListId();

if (response[0] !== undefined && response[0].object.articlesModificationTime !== undefined) {
$scope.playlist.selected.articlesModificationTime = response[0].object.articlesModificationTime;
}
}

var afterSaveError = function (response) {
if (response.errors[0].code === 409) {
flashMessage(Translator.trans(
'This list is already in a different state than the one in which it was loaded.'
), 'error');
// automatically refresh playlist
Playlist.getArticlesByListId({id: Playlist.getListId()}).then(function (data) {
$scope.featuredArticles = data.items;
$scope.playlist.selected.articlesModificationTime = data.articlesModificationTime;
}, function(response) {
flashMessage(Translator.trans('Could not refresh the list'), 'error');
});
} else {
flashMessage(Translator.trans('Could not save the list'), 'error');
}

return Playlist.batchUpdate(logList, $scope.playlist.selected);
$scope.processing = false;
}
}]);
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ angular.module('playlistsApp').factory('Playlist', [
deferredGet = $q.defer(),
url;

articles.$promise = deferredGet.promise;

var params = {id: playlist.id};

if (playlist.maxItems !== null && playlist.maxItems > 0) {
Expand All @@ -87,12 +85,12 @@ angular.module('playlistsApp').factory('Playlist', [
articles.push(item);
});
playlistArticles = articles;
deferredGet.resolve(articles);
deferredGet.resolve(response);
}).error(function (responseBody) {
deferredGet.reject(responseBody);
});

return articles;
return deferredGet.promise;
};

Playlist.getArticle = function (number, language) {
Expand Down Expand Up @@ -281,7 +279,7 @@ angular.module('playlistsApp').factory('Playlist', [
data: $.param(formData)
}).success(function (response) {
listId = response.id;
deferred.resolve();
deferred.resolve(response);
}).error(function (responseBody) {
deferred.reject(responseBody);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ Comments: Comments
'List limit': 'List limit'
'Last item of the list has been deleted to add this item to the list. Revert?': 'Last item of the list has been deleted to add this item to the list. Revert?'
'Revert': 'Revert'
'Could not automatically refresh the list': 'Could not automatically refresh the list'
articles:
playlists:
namechanged: 'Are you sure you want to change the list name? The name might be used in the Newscoop templates to render the list in the frontend. Please check with your siteadmin if you have doubts.'
Expand Down

0 comments on commit 7553d46

Please sign in to comment.