Commit c0ec34f98d6ff8075bebd9c6ef99f6d4d5355a54

Authored by Victor Costa
1 parent 90add01a
Exists in master and in 1 other branch dev-fixes

Refactor error method from notification component

src/app/components/notification/notification.component.spec.ts
... ... @@ -20,7 +20,7 @@ describe("Components", () => {
20 20 let component: Notification = new Notification(<any>helpers.mocks.$log, <any>sweetAlert, <any>helpers.mocks.$translate);
21 21 component.httpError(500, {});
22 22 expect(sweetAlert.swal).toHaveBeenCalledWith(jasmine.objectContaining({
23   - text: Notification.DEFAULT_HTTP_ERROR_MESSAGE,
  23 + text: Notification.DEFAULT_ERROR_MESSAGE,
24 24 type: "error"
25 25 }));
26 26 done();
... ... @@ -33,7 +33,7 @@ describe(&quot;Components&quot;, () =&gt; {
33 33 let component: Notification = new Notification(<any>helpers.mocks.$log, <any>sweetAlert, <any>helpers.mocks.$translate);
34 34 component.httpError(500, null);
35 35 expect(sweetAlert.swal).toHaveBeenCalledWith(jasmine.objectContaining({
36   - text: Notification.DEFAULT_HTTP_ERROR_MESSAGE,
  36 + text: Notification.DEFAULT_ERROR_MESSAGE,
37 37 type: "error"
38 38 }));
39 39 done();
... ...
src/app/components/notification/notification.component.ts
... ... @@ -10,19 +10,22 @@ export class Notification {
10 10 private $translate: angular.translate.ITranslateService
11 11 ) { }
12 12  
13   - public static DEFAULT_HTTP_ERROR_TITLE = "notification.http-error.default.title";
14   - public static DEFAULT_HTTP_ERROR_MESSAGE = "notification.http-error.default.message";
  13 + public static DEFAULT_ERROR_TITLE = "notification.error.default.title";
  14 + public static DEFAULT_ERROR_MESSAGE = "notification.error.default.message";
15 15 public static DEFAULT_SUCCESS_TIMER = 1000;
16 16  
17   - httpError(status: number, data: any): boolean {
18   - this.$log.debug(status, data);
19   -
20   - let message = (data || {}).message || Notification.DEFAULT_HTTP_ERROR_MESSAGE;
  17 + error(message: string = Notification.DEFAULT_ERROR_MESSAGE, title: string = Notification.DEFAULT_ERROR_TITLE) {
  18 + this.$log.debug("Notification error:", title, message);
21 19 this.SweetAlert.swal({
22   - title: this.$translate.instant(Notification.DEFAULT_HTTP_ERROR_TITLE),
  20 + title: this.$translate.instant(title),
23 21 text: this.$translate.instant(message),
24 22 type: "error"
25 23 });
  24 + }
  25 +
  26 + httpError(status: number, data: any): boolean {
  27 + let message = (data || {}).message || Notification.DEFAULT_ERROR_MESSAGE;
  28 + this.error(`notification.http_error.${status}.message`);
26 29 return true; // return true to indicate that the error was already handled
27 30 }
28 31  
... ...
src/app/profile/profile.component.spec.ts
... ... @@ -18,7 +18,7 @@ describe(&quot;Components&quot;, () =&gt; {
18 18 beforeEach(() => {
19 19 $stateParams = jasmine.createSpyObj("$stateParams", ["profile"]);
20 20 profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["setCurrentProfileByIdentifier", "getBoxes"]);
21   - notificationMock = jasmine.createSpyObj("notificationMock", ["httpError"]);
  21 + notificationMock = jasmine.createSpyObj("notificationMock", ["error"]);
22 22  
23 23 let profileResponse = $q.defer();
24 24 profileResponse.resolve({ id: 1 });
... ... @@ -54,7 +54,7 @@ describe(&quot;Components&quot;, () =&gt; {
54 54 $rootScope.$apply();
55 55  
56 56 expect(profileServiceMock.setCurrentProfileByIdentifier).toHaveBeenCalled();
57   - expect(notificationMock.httpError).toHaveBeenCalled();
  57 + expect(notificationMock.error).toHaveBeenCalled();
58 58 expect(component.profile).toBeUndefined();
59 59 done();
60 60 });
... ...
src/app/profile/profile.component.ts
... ... @@ -86,7 +86,7 @@ export class Profile {
86 86 }).then((response: restangular.IResponse) => {
87 87 this.boxes = response.data.boxes;
88 88 }).catch(() => {
89   - notification.httpError(404, { message: "Profile not found!" });
  89 + notification.error("notification.profile.not_found");
90 90 });
91 91 }
92 92 }
... ...
src/languages/en.json
... ... @@ -18,6 +18,8 @@
18 18 "auth.form.password": "Password",
19 19 "auth.form.login_button": "Login",
20 20 "navbar.content_viewer_actions.new_post": "New Post",
21   - "notification.http-error.default.message": "Something went wrong!",
22   - "notification.http-error.default.title": "Oops..."
  21 + "notification.error.default.message": "Something went wrong!",
  22 + "notification.error.default.title": "Oops...",
  23 + "notification.profile.not_found": "Profile not found",
  24 + "notification.http_error.401.message": "Unauthorized"
23 25 }
... ...
src/languages/pt.json
... ... @@ -18,6 +18,8 @@
18 18 "auth.form.password": "Senha",
19 19 "auth.form.login_button": "Login",
20 20 "navbar.content_viewer_actions.new_post": "Novo Artigo",
21   - "notification.http-error.default.message": "Algo deu errado!",
22   - "notification.http-error.default.title": "Oops..."
  21 + "notification.error.default.message": "Algo deu errado!",
  22 + "notification.error.default.title": "Oops...",
  23 + "notification.profile.not_found": "Perfil não encontrado",
  24 + "notification.http_error.401.message": "Não autorizado"
23 25 }
... ...