Skip to content

Commit

Permalink
Merge pull request #1081 from CruGlobal/2447-preserve-cart-order
Browse files Browse the repository at this point in the history
[EP-2447] Preserve cart order after edit
  • Loading branch information
canac authored Jan 17, 2024
2 parents bc2b37f + bc07bb4 commit acf4225
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/app/cart/cart.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,18 @@ class CartController {
} else {
this.loading = true
}
// Remember the order of the existing items in the cart
const orderByCode = this.cartData?.items?.map(item => item.code) || []
this.cartService.get()
.subscribe(data => {
if (reload) {
// Sort the incoming cart to match the order of the previous cart, with new items at the top
// The code of recurring gifts have a suffix and look like 0123456_MON or 0123456_QUARTERLY.
// We will be able to maintain the order of items in the cart as long as the user doesn't
// change the frequency of a gift. The server prevents carts from containing multiple gifts
// with the same frequency and designation account, which would interfere with sorting.
data.items?.sort((item1, item2) => orderByCode.indexOf(item1.code) - orderByCode.indexOf(item2.code))
}
this.cartData = data
this.setLoadCartVars(reload)
},
Expand Down Expand Up @@ -97,7 +107,6 @@ class CartController {
.configureProduct(item.code, item.config, true, item.uri)
modal && modal.result
.then(() => {
pull(this.cartData.items, item)
this.loadCart(true)
}, angular.noop)
}
Expand Down
18 changes: 17 additions & 1 deletion src/app/cart/cart.component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ describe('cart', () => {
expect(self.controller.updating).toEqual(false)
})

it('should preserve the order', () => {
self.controller.cartService.get.mockReturnValue(Observable.of({ items: [{ code: '1' }, { code: '2' }, { code: '3' }] }))
self.controller.loadCart(true)

self.controller.cartService.get.mockReturnValue(Observable.of({ items: [{ code: '3' }, { code: '2' }, { code: '1' }, { code: '4' }] }))
self.controller.loadCart(true)

expect(self.controller.cartData).toEqual({ items: [{ code: '4' }, { code: '1' }, { code: '2' }, { code: '3' }] })
})

it('should handle empty data', () => {
self.controller.cartService.get.mockReturnValue(Observable.of({}))
self.controller.loadCart(true)

expect(self.controller.cartData).toEqual({})
})

it('should handle an error loading cart data', () => {
self.controller.cartData = 'previous data'
self.controller.cartService.get.mockReturnValue(Observable.throw('error'))
Expand Down Expand Up @@ -178,7 +195,6 @@ describe('cart', () => {

expect(self.controller.productModalService.configureProduct).toHaveBeenCalledWith('0123456', 'some config', true, 'uri1')
expect(self.controller.loadCart).toHaveBeenCalledWith(true)
expect(self.controller.cartData.items).toEqual([{ uri: 'uri2' }])
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/app/cart/cart.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h3 class="panel-name" translate>Your Gift Cart</h3>
<tbody>
<tr class="giftsum-gift-row" ng-repeat="i in $ctrl.cartData.items">
<td class="td-gift">
<img desig-src="{{i.designationNumber}}" class="giftsum-profile pull-left">
<img desig-src="{{i.designationNumber}}" class="giftsum-profile pull-left" width="90" height="51">
<span class="giftsum-person giftsum-title"><a ng-href="/{{i.designationNumber}}">{{i.displayName}}</a></span>
<span class="giftsum-accountnum giftsum-detail">#{{i.designationNumber}}</span>
</td>
Expand Down
2 changes: 1 addition & 1 deletion src/app/checkout/step-3/step-3.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
</tr>
<tr class="giftsum-gift-row" ng-repeat-end>
<td class="td-gift">
<img desig-src="{{i.designationNumber}}" class="giftsum-profile pull-left">
<img desig-src="{{i.designationNumber}}" class="giftsum-profile pull-left" width="90" height="51">
<span class="giftsum-person giftsum-title">{{i.displayName}}</span>
<span class="giftsum-accountnum giftsum-detail">#{{i.designationNumber}}</span>
</td>
Expand Down
1 change: 0 additions & 1 deletion src/assets/cru-scss/_cart.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
padding-bottom: 30px;
}
.giftsum-profile {
width: 90px;
height: auto;
margin-right: 15px;
}
Expand Down
1 change: 0 additions & 1 deletion src/assets/scss/_cart.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
}
}
.giftsum-profile {
width: 90px;
height: auto;
margin-right: 15px;
@include respond-to(extrasmall) {
Expand Down

0 comments on commit acf4225

Please sign in to comment.