From c2efd86086848b0c6426ebce23bd9a107d429aaf Mon Sep 17 00:00:00 2001 From: Rafael Velazco Date: Tue, 6 Jun 2023 18:05:57 -0400 Subject: [PATCH] Fix #25076 Invalidate Cache for Reindex (#25165) * dev: remove retry pages request base on modDate * fix: tests * remove --- .../dot-pages-store/dot-pages.store.spec.ts | 86 +------------- .../dot-pages-store/dot-pages.store.ts | 110 ++++++------------ 2 files changed, 38 insertions(+), 158 deletions(-) diff --git a/core-web/apps/dotcms-ui/src/app/portlets/dot-pages/dot-pages-store/dot-pages.store.spec.ts b/core-web/apps/dotcms-ui/src/app/portlets/dot-pages/dot-pages-store/dot-pages.store.spec.ts index 6540c5fb262d..05abfb5bf55c 100644 --- a/core-web/apps/dotcms-ui/src/app/portlets/dot-pages/dot-pages-store/dot-pages.store.spec.ts +++ b/core-web/apps/dotcms-ui/src/app/portlets/dot-pages/dot-pages-store/dot-pages.store.spec.ts @@ -503,80 +503,8 @@ describe('DotPageStore', () => { expect(dotHttpErrorManagerService.handle).toHaveBeenCalledWith(error500, true); }); - it('should keep fetching Pages data until new value comes from the DB in store', fakeAsync(() => { - dotPageStore.setPages({ items: favoritePagesInitialTestData }); - const old = { - contentTook: 0, - jsonObjectView: { - contentlets: favoritePagesInitialTestData as unknown as DotCMSContentlet[] - }, - queryTook: 1, - resultsSize: 2 - }; - - const updated = { - contentTook: 0, - jsonObjectView: { - contentlets: [ - { ...favoritePagesInitialTestData[0], modDate: '2020-09-02 16:50:15.569' }, - { ...favoritePagesInitialTestData[1] } - ] as unknown as DotCMSContentlet[] - }, - queryTook: 1, - resultsSize: 4 - }; - - const mockFunction = (times) => { - let count = 1; - - return Observable.create((observer) => { - if (count++ > times) { - observer.next(updated); - } else { - observer.next(old); - } - }); - }; - - spyOn(dotESContentService, 'get').and.returnValue(mockFunction(3)); - spyOn(dotPageStore, 'setPagesStatus').and.callThrough(); - - dotPageStore.updateSinglePageData({ identifier: '123', isFavoritePage: false }); - - tick(3000); - - // dotESContentService.get only is called 1 time, but "retryWhen" operator makes several request to the SpyOn - expect(dotESContentService.get).toHaveBeenCalledTimes(1); - - // Testing to setPagesStatus to LOADING on the first fetch - expect((dotPageStore.setPagesStatus as jasmine.Spy).calls.argsFor(0).toString()).toBe( - ComponentStatus.LOADING - ); - - // Testing to pages.status to be LOADED on the last fetch (there can only be 2 calls during the whole process) - dotPageStore.state$.subscribe((data) => { - expect(data.pages.status).toBe(ComponentStatus.LOADED); - }); - - // Since dotESContentService.get can only be called 1 time (and once called the data will be changed on "mockFunction"), - // we test that the last fetch contains the updated data - (dotESContentService.get as jasmine.Spy).calls - .mostRecent() - .returnValue.subscribe((data) => { - expect(data).toEqual(updated); - }); - })); - it('should remove page archived from pages collection and add undefined at the bottom', fakeAsync(() => { dotPageStore.setPages({ items: favoritePagesInitialTestData }); - const old = { - contentTook: 0, - jsonObjectView: { - contentlets: favoritePagesInitialTestData as unknown as DotCMSContentlet[] - }, - queryTook: 1, - resultsSize: 2 - }; const updated = { contentTook: 0, @@ -587,19 +515,7 @@ describe('DotPageStore', () => { resultsSize: 4 }; - const mockFunction = (times) => { - let count = 1; - - return Observable.create((observer) => { - if (count++ > times) { - observer.next(updated); - } else { - observer.next(old); - } - }); - }; - - spyOn(dotESContentService, 'get').and.returnValue(mockFunction(3)); + spyOn(dotESContentService, 'get').and.returnValue(of(updated)); dotPageStore.updateSinglePageData({ identifier: '123', isFavoritePage: false }); diff --git a/core-web/apps/dotcms-ui/src/app/portlets/dot-pages/dot-pages-store/dot-pages.store.ts b/core-web/apps/dotcms-ui/src/app/portlets/dot-pages/dot-pages-store/dot-pages.store.ts index 561081bc8c11..43af95f740a4 100644 --- a/core-web/apps/dotcms-ui/src/app/portlets/dot-pages/dot-pages-store/dot-pages.store.ts +++ b/core-web/apps/dotcms-ui/src/app/portlets/dot-pages/dot-pages-store/dot-pages.store.ts @@ -7,17 +7,7 @@ import { Injectable } from '@angular/core'; import { MenuItem, SelectItem } from 'primeng/api'; import { DialogService } from 'primeng/dynamicdialog'; -import { - catchError, - delay, - filter, - map, - mergeMap, - retryWhen, - switchMap, - take, - tap -} from 'rxjs/operators'; +import { catchError, filter, map, mergeMap, switchMap, take, tap } from 'rxjs/operators'; import { DotFavoritePageService } from '@dotcms/app/api/services/dot-favorite-page/dot-favorite-page.service'; import { DotHttpErrorManagerService } from '@dotcms/app/api/services/dot-http-error-manager/dot-http-error-manager.service'; @@ -380,76 +370,50 @@ export class DotPageStore extends ComponentStore { ) => { return params$.pipe( mergeMap((params) => { - let localPageData: DotCMSContentlet; - let retries = 0; const { identifier, isFavoritePage } = params; const sortOrderValue = this.getSortOrderValue(); - if (isFavoritePage) { - localPageData = this.get().favoritePages.items.filter( - (item) => item?.identifier === identifier - )[0]; - } else { - localPageData = this.get().pages.items.filter( - (item) => item?.identifier === identifier - )[0]; - this.setPagesStatus(ComponentStatus.LOADING); - } + if (!isFavoritePage) this.setPagesStatus(ComponentStatus.LOADING); + + return this.getPagesDataFn(isFavoritePage, sortOrderValue, identifier).pipe( + tap( + (items) => { + // Finished fetch loop and will proceed to set data on store + if (isFavoritePage) { + const pagesData = this.get().favoritePages.items.map((page) => { + return page?.identifier === identifier + ? items.jsonObjectView.contentlets[0] + : page; + }); + + this.setFavoritePages({ items: pagesData }); + } else { + let pagesData = this.get().pages.items; + + if (items.jsonObjectView.contentlets[0] === undefined) { + pagesData = pagesData.filter((page) => { + return page?.identifier !== identifier; + }); - return this.getPagesDataFn(isFavoritePage, sortOrderValue, identifier) - .pipe( - tap( - (items) => { - retries++; - - // Will continue repeating fetch until data has changed or limit fetch reached - if ( - localPageData?.modDate === - items.jsonObjectView.contentlets[0]?.modDate && - retries < 10 - ) { - throw false; + // Add undefined to keep the same length of the array, + // otherwise the pagination(endless scroll) will break + pagesData.push(undefined); } else { - // Finished fetch loop and will proceed to set data on store - if (isFavoritePage) { - const pagesData = this.get().favoritePages.items.map( - (page) => { - return page?.identifier === identifier - ? items.jsonObjectView.contentlets[0] - : page; - } - ); - - this.setFavoritePages({ items: pagesData }); - } else { - let pagesData = this.get().pages.items; - - if (items.jsonObjectView.contentlets[0] === undefined) { - pagesData = pagesData.filter((page) => { - return page?.identifier !== identifier; - }); - - // Add undefined to keep the same length of the array, - // otherwise the pagination(endless scroll) will break - pagesData.push(undefined); - } else { - pagesData = pagesData.map((page) => { - return page?.identifier === identifier - ? items.jsonObjectView.contentlets[0] - : page; - }); - } - - this.setPages({ items: pagesData }); - } + pagesData = pagesData.map((page) => { + return page?.identifier === identifier + ? items.jsonObjectView.contentlets[0] + : page; + }); } - }, - (error: HttpErrorResponse) => { - return this.httpErrorManagerService.handle(error); + + this.setPages({ items: pagesData }); } - ) + }, + (error: HttpErrorResponse) => { + return this.httpErrorManagerService.handle(error); + } ) - .pipe(retryWhen((errors) => errors.pipe(delay(1000)))); + ); }) ); }