Skip to content

Commit

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

This reverts commit 448ce21.
  • Loading branch information
pkrawat1 authored Aug 31, 2018
1 parent cd1e72e commit 08a7960
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 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, switchMap } from 'rxjs/operators';
import { map, tap } 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,12 +17,11 @@ export class CheckoutService {
private orderNumber: number;

/**
*Creates an instance of CheckoutService.
* @param {HttpClient} http
* Creates an instance of CheckoutService.
* @param {HttpService} http
* @param {CheckoutActions} actions
* @param {Store<AppState>} store
* @param {ToastrService} toastyService
* @param {*} platformId
*
* @memberof CheckoutService
*/
constructor(
Expand Down Expand Up @@ -61,39 +60,23 @@ 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(
tap(order => {
map(order => {
if (order) {
const token = order.token;
this.setOrderTokenInLocalStorage({ order_token: token, order_number: order.number });
this.setOrderTokenInLocalStorage({ order_token: token });
return this.store.dispatch(
this.actions.fetchCurrentOrderSuccess(order)
);
}
}),
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();
}
} else {
this.createEmptyOrder().subscribe();
}
})
);
Expand Down Expand Up @@ -125,8 +108,10 @@ export class CheckoutService {
.post<Order>('api/v1/orders.json', null, { headers: headers })
.pipe(
map(order => {
this.setOrderTokenInLocalStorage({ order_token: order.token, order_number: order.number });
return this.store.dispatch(this.actions.fetchCurrentOrderSuccess(order));
this.setOrderTokenInLocalStorage({ order_token: order.token });
return this.store.dispatch(
this.actions.fetchCurrentOrderSuccess(order)
);
}),
tap(
_ => _,
Expand Down Expand Up @@ -285,7 +270,8 @@ export class CheckoutService {
*/
private getOrderToken() {
const order = isPlatformBrowser(this.platformId) ? JSON.parse(localStorage.getItem('order')) : {};
return order ? order.order_token : null;
const token = order.order_token;
return token;
}

shipmentAvailability(pincode: number) {
Expand All @@ -302,7 +288,6 @@ 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 08a7960

Please sign in to comment.