Skip to content

Commit

Permalink
This change fixes the issue with order persistance between guest user (
Browse files Browse the repository at this point in the history
…#246)

### Why?
- When a guest user adds products to the cart, he/she is not able to persist the cart after logging in.

### This change fixes need by:-
1. Guest user
   *    1. Check for localstorage order details, to find the order.
   *    2. Create new order if not found.
2. Logged in User
   *    1. Get user current user from api.
   *    2. If not current order, then check for localstorage order details, to find the order.
   *    3. Create new order if not found.
  • Loading branch information
pkrawat1 authored Aug 30, 2018
1 parent 2deb61b commit 5786acd
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions src/app/core/services/checkout.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { map, tap } from 'rxjs/operators';
import { map, tap, switchMap } from 'rxjs/operators';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { getOrderNumber } from './../../checkout/reducers/selectors';
import { CheckoutActions } from './../../checkout/actions/checkout.actions';
Expand All @@ -17,11 +17,12 @@ export class CheckoutService {
private orderNumber: number;

/**
* Creates an instance of CheckoutService.
* @param {HttpService} http
*Creates an instance of CheckoutService.
* @param {HttpClient} http
* @param {CheckoutActions} actions
* @param {Store<AppState>} store
*
* @param {ToastrService} toastyService
* @param {*} platformId
* @memberof CheckoutService
*/
constructor(
Expand Down Expand Up @@ -60,23 +61,39 @@ export class CheckoutService {
}

/**
*
*
* This method fetches the current order in two ways.
* 1. Guest user
* 1. Check for localstorage order details, to find the order.
* 2. Create new order if not found.
* 2. Logged in User
* 1. Get user current user from api.
* 2. If not current order, then check for localstorage order details, to find the order.
* 3. Create new order if not found.
* @returns
*
* @memberof CheckoutService
*/
fetchCurrentOrder() {
return this.http.get<Order>('api/v1/orders/current').pipe(
map(order => {
tap(order => {
if (order) {
const token = order.token;
this.setOrderTokenInLocalStorage({ order_token: token });
this.setOrderTokenInLocalStorage({ order_token: token, order_number: order.number });
return this.store.dispatch(
this.actions.fetchCurrentOrderSuccess(order)
);
} else {
this.createEmptyOrder().subscribe();
}
}),
switchMap(order => {
if (!order) {
if (this.getOrderToken()) {
const s_order = JSON.parse(localStorage.getItem('order'));
return this
.getOrder(s_order.order_number)
.pipe(tap(_order => this.store.dispatch(this.actions.fetchCurrentOrderSuccess(_order))));
} else {
return this.createEmptyOrder();
}
}
})
);
Expand Down Expand Up @@ -108,10 +125,8 @@ export class CheckoutService {
.post<Order>('api/v1/orders.json', null, { headers: headers })
.pipe(
map(order => {
this.setOrderTokenInLocalStorage({ order_token: order.token });
return this.store.dispatch(
this.actions.fetchCurrentOrderSuccess(order)
);
this.setOrderTokenInLocalStorage({ order_token: order.token, order_number: order.number });
return this.store.dispatch(this.actions.fetchCurrentOrderSuccess(order));
}),
tap(
_ => _,
Expand Down Expand Up @@ -270,8 +285,7 @@ export class CheckoutService {
*/
private getOrderToken() {
const order = isPlatformBrowser(this.platformId) ? JSON.parse(localStorage.getItem('order')) : {};
const token = order.order_token;
return token;
return order ? order.order_token : null;
}

shipmentAvailability(pincode: number) {
Expand All @@ -288,6 +302,7 @@ export class CheckoutService {
*/
private setOrderTokenInLocalStorage(token: any): void {
const jsonData = JSON.stringify(token);
this.orderNumber = token.order_number
if (isPlatformBrowser(this.platformId)) {
localStorage.setItem('order', jsonData) ;
}
Expand Down

0 comments on commit 5786acd

Please sign in to comment.