Skip to content

Commit

Permalink
Fix #25937: Template Builder Enhancing error and request handling (#2…
Browse files Browse the repository at this point in the history
…5942)

* fix (template save and publish): enhancing error and request handling

* feedback (dot templates service): delete old save and publish

* fix (gridstack utils): EMPTY_ROWS_VALUE was being modified by reference
  • Loading branch information
zJaaal authored and fmontes committed Aug 31, 2023
1 parent 8278b29 commit 509f73d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ describe('DotTemplatesService', () => {
}
});
});

it('should put to save and publish a template', () => {
service
.saveAndPublish({
Expand All @@ -211,6 +212,7 @@ describe('DotTemplatesService', () => {
}
});
});

it('should delete a template', () => {
service.delete(['testId01']).subscribe();
const req = httpMock.expectOne(TEMPLATE_API_URL);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Observable } from 'rxjs';

import { HttpErrorResponse } from '@angular/common/http';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';

import { catchError, map, pluck, take } from 'rxjs/operators';
Expand All @@ -21,7 +21,8 @@ export const TEMPLATE_API_URL = '/api/v1/templates/';
export class DotTemplatesService {
constructor(
private coreWebService: CoreWebService,
private httpErrorManagerService: DotHttpErrorManagerService
private httpErrorManagerService: DotHttpErrorManagerService,
private http: HttpClient
) {}

/**
Expand Down Expand Up @@ -91,11 +92,11 @@ export class DotTemplatesService {
* @memberof DotTemplatesService
*/
saveAndPublish(values: DotTemplate): Observable<DotTemplate> {
return this.request<DotTemplate>({
method: 'PUT',
url: `${TEMPLATE_API_URL}_savepublish`,
body: values
});
return this.http
.put<DotTemplate>(`${TEMPLATE_API_URL}_savepublish`, {
...values
})
.pipe(pluck('entity'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentStore } from '@ngrx/component-store';
import { ComponentStore, tapResponse } from '@ngrx/component-store';
import * as _ from 'lodash';
import { Observable, of, zip } from 'rxjs';

Expand Down Expand Up @@ -142,22 +142,25 @@ export class DotTemplateStore extends ComponentStore<DotTemplateState> {
switchMap((template: DotTemplateItem) => {
this.dotGlobalMessageService.loading(this.dotMessageService.get('publishing'));

return this.dotTemplateService.saveAndPublish(this.cleanTemplateItem(template));
}),
tap((template: DotTemplate) => {
this.dotGlobalMessageService.success(
this.dotMessageService.get('message.template.published')
);
this.dotRouterService.allowRouteDeactivation();
this.updateTemplateState(template);
}),
catchError((err: HttpErrorResponse) => {
this.dotGlobalMessageService.error(err.statusText);
this.dotHttpErrorManagerService.handle(err).subscribe(() => {
this.dotRouterService.allowRouteDeactivation();
});

return of(null);
return this.dotTemplateService
.saveAndPublish(this.cleanTemplateItem(template))
.pipe(
tapResponse(
(template: DotTemplate) => {
this.dotGlobalMessageService.success(
this.dotMessageService.get('message.template.published')
);
this.dotRouterService.allowRouteDeactivation();
this.updateTemplateState(template);
},
(err: HttpErrorResponse) => {
this.dotGlobalMessageService.error(err.statusText);
this.dotHttpErrorManagerService.handle(err).subscribe(() => {
this.dotRouterService.allowRouteDeactivation();
});
}
)
);
})
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export function parseFromDotObjectToGridStack(
body: DotLayoutBody | undefined
): DotGridStackWidget[] {
if (!body || !body.rows?.length) {
return EMPTY_ROWS_VALUE;
return structuredClone(EMPTY_ROWS_VALUE);
}

return body.rows.map((row, i) => ({
Expand Down

0 comments on commit 509f73d

Please sign in to comment.