Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
fix(cart): remove and undo updates properly
Browse files Browse the repository at this point in the history
- remove only fires cart-update event once
- change method signature of CartService.remove() to take IFile[] instead of string[], so 'files added/remove' notifications never have 'undefined' for file name as CartService no longer stores file names
- delete existing CartService.removeFiles(files: IFile[]) which plucked file_id and sent to remove()

Closes #1857
  • Loading branch information
Christine Yu committed Mar 22, 2016
1 parent 252ae45 commit b926698
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 54 deletions.
9 changes: 5 additions & 4 deletions app/scripts/cart/cart.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ module ngApp.cart.controllers {
private CartState) {
var data = $state.current.data || {};
this.CartState.setActive("tabs", data.tab);
CoreService.setPageTitle("Cart", "(" + this.files.length + ")");
this.lastModified = this.CartService.lastModified;
this.cartTableConfig = CartTableModel;

Expand Down Expand Up @@ -141,6 +140,7 @@ module ngApp.cart.controllers {

refresh(): void {
const fileIds = this.CartService.getFileIds();
this.CoreService.setPageTitle("Cart", "(" + fileIds.length + ")");
// in the event that our cart is empty
if (fileIds.length < 1) {
this.files = {};
Expand Down Expand Up @@ -221,7 +221,7 @@ module ngApp.cart.controllers {
}

removeFromCart(): void {
this.CartService.removeFiles([this.file]);
this.CartService.remove([this.file]);
}
}

Expand Down Expand Up @@ -259,13 +259,14 @@ module ngApp.cart.controllers {

this.FilesService.getFiles({
fields:[
"file_id"
"file_id",
"file_name"
],
filters: filters,
size: size,
from: 0
}, 'POST').then((data) => {
this.CartService.remove(_.pluck(data.hits, "file_id"));
this.CartService.remove(data.hits);
});
}

Expand Down
16 changes: 11 additions & 5 deletions app/scripts/cart/cart.directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ module ngApp.cart.directives {
templateUrl: "cart/templates/remove-single.html",
controllerAs: 'ctrl',
controller: function($scope: ng.IScope, CartService: ICartService) {
disabled: boolean = false;
this.remove = function() {
CartService.remove([this.file.file_id]);
$scope.$emit("cart-update");
CartService.remove([{file_id: this.file.file_id,
file_name: this.file.file_name }]);
this.disabled = true;
}
}
};
Expand Down Expand Up @@ -218,8 +220,12 @@ module ngApp.cart.directives {
};

this.calculateFileCount = function() {
this.inBoth = _.intersection(_.pluck(CartService.getFiles(), "file_id"),
_.pluck(this.files, "file_id"));
this.inBoth = this.files.reduce((acc, f) => {
if (CartService.getFiles().find(cartF => cartF.file_id === f.file_id)){
return acc.concat(f);
}
return acc;
}, []);
}
}
}
Expand All @@ -241,7 +247,7 @@ module ngApp.cart.directives {
},true);

$scope.remove = function() {
CartService.removeFiles($scope.files);
CartService.remove($scope.files);
}

}
Expand Down
38 changes: 13 additions & 25 deletions app/scripts/cart/cart.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ module ngApp.cart.services {
isInCart(fileId: string): boolean;
areInCart(files: IFile[]): boolean;
removeAll(): void;
remove(fileIds: string[]): void;
removeFiles(files: IFile[]): void;
remove(files: IFile[]): void;
buildAddedMsg(added: Array<Object>, alreadyIn: Array<Object>): string;
buildRemovedMsg(removedFiles: IFile[]): string;
undoAdded(): void;
Expand Down Expand Up @@ -291,46 +290,35 @@ module ngApp.cart.services {
}

removeAll(): void {
this.notify.closeAll();
this.notify({
message: "",
messageTemplate: this.buildRemovedMsg(this.files),
container: "#notification",
classes: "alert-warning"
});
this.lastModifiedFiles = this.files;
this.files = [];
this._sync();
this.remove(this.files);
}

remove(fileIds: string[]): void {
var remaining = _.reject(this.files, function (hit: IFile) {
return fileIds.indexOf(hit.file_id) !== -1;
});
this.lastModifiedFiles = _.difference(this.files, remaining);
this._sync();
remove(filesToRemove: IFile[]): void {
var partitioned = this.files.reduce((acc, f) => {
var fileToRemove = _.find(filesToRemove, f2r => f2r.file_id === f.file_id);
if (fileToRemove) {
return { remaining: acc.remaining, removed: acc.removed.concat(fileToRemove)};
}
return { remaining: acc.remaining.concat(f), removed: acc.removed};
} ,{ remaining: [], removed: [] });
this.lastModifiedFiles = partitioned.removed;
this.notify.closeAll();
this.notify({
message: "",
messageTemplate: this.buildRemovedMsg(this.lastModifiedFiles),
container: "#notification",
classes: "alert-warning"
});
this.files = remaining;
this.files = partitioned.remaining;
this._sync();
}

removeFiles(files: IFile[]): void {
var ids: string[] = _.pluck(files, "file_id");
this.remove(ids);
}

getFileIds(): string[] {
return _.pluck(this.files, "file_id");
}

undoAdded(): void {
this.removeFiles(this.lastModifiedFiles);
this.remove(this.lastModifiedFiles);
}

undoRemoved(): void {
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/cart/templates/add-to-cart-all-dropdown.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<button data-ng-if="ctrl.CartService.areInCart(ctrl.files)"
type="button"
class="btn btn-success fa fa-shopping-cart"
data-ng-click="ctrl.CartService.removeFiles(ctrl.files)">
data-ng-click="ctrl.CartService.remove(ctrl.files)">
<span class="icon-btn-label">Add to Cart</span>
</button>
<button data-ng-if="!ctrl.CartService.areInCart(ctrl.files)"
Expand Down
5 changes: 3 additions & 2 deletions app/scripts/cart/templates/remove-single.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
data-tooltip-popup-delay=1000
data-uib-tooltip="Remove"
class="btn btn-default fa fa-trash-o"
data-ng-click="ctrl.remove()">
<span class="icon-btn-label">Add to Cart</span>
data-ng-click="ctrl.remove()"
data-ng-disabled="ctrl.disabled">
<span class="icon-btn-label">Remove from Cart</span>
</button>
18 changes: 4 additions & 14 deletions app/scripts/cart/tests/cart.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ describe('Cart:', function () {
var files = [
{
file_id: "AAA",
file_name: "aaa.bam",
file_size: 20,
file_url: "urlA",
participantId: []
},
{
file_id: "BBB",
file_name: "bbb.bam",
file_size: 10,
file_url: "urlB",
participantId: []
Expand Down Expand Up @@ -127,23 +129,11 @@ describe('Cart:', function () {
expect(CartService).to.have.property('files').to.be.empty;
}));

it('should remove files by ids', inject(function (CartService) {
CartService.add(fileA);
CartService.add(fileB);
var removeByIdCallback = sinon.spy(CartService, 'remove');
CartService.remove(['AAA']);
expect(removeByIdCallback).to.have.been.calledOnce;
var files = CartService.getFiles();
expect(files).to.have.length(1);
expect(files[0]).to.have.property('file_id', 'BBB');
}));


it('should remove files', inject(function (CartService) {
CartService.add(fileA);
CartService.add(fileB);
var removeByIdCallback = sinon.spy(CartService, 'removeFiles');
CartService.removeFiles([fileA]);
var removeByIdCallback = sinon.spy(CartService, 'remove');
CartService.remove([fileA]);
expect(removeByIdCallback).to.have.been.calledOnce;
var files = CartService.getFiles();
expect(files).to.have.length(1);
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/query/query.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ module ngApp.query.controllers {
}

removeFiles(files: IFile[]): void {
this.CartService.remove(_.pluck(files, "file_id"));
this.CartService.remove(files);
}
}
angular
Expand All @@ -187,4 +187,4 @@ module ngApp.query.controllers {
"files.services"
])
.controller("QueryController", QueryController);
}
}
2 changes: 1 addition & 1 deletion app/scripts/search/search.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ module ngApp.search.controllers {
}

removeFiles(files: IFile[]): void {
this.CartService.remove(_.pluck(files, "file_id"));
this.CartService.remove(files);
}

gotoQuery() {
Expand Down

0 comments on commit b926698

Please sign in to comment.