Skip to content

Commit

Permalink
fix: PostProduct review error is now normalized (#17810)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpawelczak authored Sep 5, 2023
1 parent 0402fcd commit 4ca9969
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 35 deletions.
2 changes: 2 additions & 0 deletions projects/assets/src/translations/en/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export const product = {
less: 'Show Less Reviews',
thankYouForReview:
'Thank you for the review! Note that reviews may require review before appearing here.',
postReviewFail:
'Something went wrong while posting your review. Please try again later.',
},
productCarousel: {
carouselLabel: 'Carousel, {{title}}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class LoadProductReferences implements Action {

export class LoadProductReferencesFail implements Action {
readonly type = LOAD_PRODUCT_REFERENCES_FAIL;
constructor(public payload: ErrorModel) {}
constructor(public payload?: ErrorModel) {}
}

export class LoadProductReferencesSuccess implements Action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Action } from '@ngrx/store';
import { ErrorModel } from '../../../model/misc.model';
import { ErrorModel, HttpErrorModel } from '../../../model/misc.model';
import { Review } from '../../../model/product.model';

export const LOAD_PRODUCT_REVIEWS = '[Product] Load Product Reviews Data';
Expand All @@ -25,7 +25,7 @@ export class LoadProductReviews implements Action {

export class LoadProductReviewsFail implements Action {
readonly type = LOAD_PRODUCT_REVIEWS_FAIL;
constructor(public payload: ErrorModel) {}
constructor(public payload?: ErrorModel) {}
}

export class LoadProductReviewsSuccess implements Action {
Expand All @@ -40,7 +40,7 @@ export class PostProductReview implements Action {

export class PostProductReviewFail implements Action {
readonly type = POST_PRODUCT_REVIEW_FAIL;
constructor(public payload: string) {}
constructor(public payload?: HttpErrorModel) {}
}

export class PostProductReviewSuccess implements Action {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable } from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Observable, of } from 'rxjs';
import { catchError, map, mergeMap } from 'rxjs/operators';
import { ErrorModel } from '../../../model/misc.model';
import { ProductReferencesConnector } from '../../connectors/references/product-references.connector';
import { ProductActions } from '../actions/index';
import { normalizeHttpError } from '../../../util/normalize-http-error';
import { LoggerService } from '../../../logger';

@Injectable()
export class ProductReferencesEffects {
protected logger = inject(LoggerService);
loadProductReferences$: Observable<
| ProductActions.LoadProductReferencesSuccess
| ProductActions.LoadProductReferencesFail
Expand All @@ -31,11 +33,11 @@ export class ProductReferencesEffects {
list: data,
});
}),
catchError((_error) =>
catchError((error) =>
of(
new ProductActions.LoadProductReferencesFail({
message: payload.productCode,
} as ErrorModel)
new ProductActions.LoadProductReferencesFail(
normalizeHttpError(error, this.logger)
)
)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {
import { cold, hot } from 'jasmine-marbles';
import { Observable, of } from 'rxjs';
import { Review } from '../../../model/product.model';
import { defaultOccProductConfig } from '../../../occ/adapters/product/default-occ-product-config';
import { OccConfig } from '../../../occ/config/occ-config';
import { ProductActions } from '../actions/index';
import * as fromEffects from '../effects/product-reviews.effect';
import { ProductReviewsConnector } from '../../connectors/index';
import { defaultOccProductConfig } from '../../../occ/adapters/product/default-occ-product-config';
import createSpy = jasmine.createSpy;
import { OccConfig } from '../../../occ/config/occ-config';
import { ProductReviewsConnector } from '../../connectors/reviews/product-reviews.connector';

const reviewData: Review[] = [
{
Expand All @@ -27,21 +27,13 @@ const reviewData: Review[] = [
},
];

const MockOccModuleConfig: OccConfig = {
backend: {
occ: {
baseUrl: '',
prefix: '',
},
},
};
class GlobalMessageServiceMock {
add(_message: GlobalMessage): void {}
}

class MockProductReviewsConnector {
get = createSpy('getList').and.returnValue(of(reviewData));
}

class GlobalMessageServiceMock {
add(_message: GlobalMessage): void {}
add = createSpy('addReview').and.returnValue(of({}));
}

describe('Product reviews effect', () => {
Expand All @@ -56,7 +48,6 @@ describe('Product reviews effect', () => {
provide: ProductReviewsConnector,
useClass: MockProductReviewsConnector,
},
{ provide: OccConfig, useValue: MockOccModuleConfig },
{ provide: OccConfig, useValue: defaultOccProductConfig },
fromEffects.ProductReviewsEffects,
provideMockActions(() => actions$),
Expand All @@ -68,7 +59,7 @@ describe('Product reviews effect', () => {
});

describe('loadProductReviews$', () => {
it('should return specified product reviews', () => {
it('should return specified product reviews on success', () => {
const productCode = '12345';
const action = new ProductActions.LoadProductReviews(productCode);
const completion = new ProductActions.LoadProductReviewsSuccess({
Expand All @@ -82,4 +73,17 @@ describe('Product reviews effect', () => {
expect(effects.loadProductReviews$).toBeObservable(expected);
});
});

describe('postProductReview', () => {
it('should post a product review and return success action on success', () => {
const reviewPayload = { productCode: '12345', review: {} };
const action = new ProductActions.PostProductReview(reviewPayload);
const completion = new ProductActions.PostProductReviewSuccess({});

actions$ = hot('-a', { a: action });
const expected = cold('-b', { b: completion });

expect(effects.postProductReview).toBeObservable(expected);
});
});
});
36 changes: 28 additions & 8 deletions projects/core/src/product/store/effects/product-reviews.effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Injectable } from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Observable, of } from 'rxjs';
import { catchError, map, mergeMap, tap } from 'rxjs/operators';
import { ErrorModel } from '../../../model/misc.model';
import { ProductReviewsConnector } from '../../connectors/reviews/product-reviews.connector';
import { ProductActions } from '../actions/index';
import {
GlobalMessageService,
GlobalMessageType,
} from '../../../global-message/index';
import { normalizeHttpError } from '../../../util/normalize-http-error';
import { LoggerService } from '../../../logger';

@Injectable()
export class ProductReviewsEffects {
protected logger = inject(LoggerService);
loadProductReviews$: Observable<
| ProductActions.LoadProductReviewsSuccess
| ProductActions.LoadProductReviewsFail
Expand All @@ -33,11 +35,11 @@ export class ProductReviewsEffects {
list: data,
});
}),
catchError((_error) =>
catchError((error) =>
of(
new ProductActions.LoadProductReviewsFail({
message: productCode,
} as ErrorModel)
new ProductActions.LoadProductReviewsFail(
normalizeHttpError(error, this.logger)
)
)
)
);
Expand All @@ -61,8 +63,12 @@ export class ProductReviewsEffects {
reviewResponse
);
}),
catchError((_error) =>
of(new ProductActions.PostProductReviewFail(payload.productCode))
catchError((error) =>
of(
new ProductActions.PostProductReviewFail(
normalizeHttpError(error, this.logger)
)
)
)
);
})
Expand All @@ -83,6 +89,20 @@ export class ProductReviewsEffects {
{ dispatch: false }
);

showGlobalMessageOnPostProductReviewFail$ = createEffect(
() =>
this.actions$.pipe(
ofType(ProductActions.POST_PRODUCT_REVIEW_FAIL),
tap(() => {
this.globalMessageService.add(
{ key: 'productReview.postReviewFail' },
GlobalMessageType.MSG_TYPE_ERROR
);
})
),
{ dispatch: false }
);

constructor(
private actions$: Actions,
private productReviewsConnector: ProductReviewsConnector,
Expand Down

0 comments on commit 4ca9969

Please sign in to comment.