@dile/crud 0.0.26 → 0.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,8 @@
1
1
  import { LitElement, html } from 'lit';
2
2
  import { DileAxios } from '../../../lib/DileAxios.js';
3
+ import { DileI18nMixin } from '../../../lib/DileI18nMixin.js';
3
4
 
4
- export class DileAjax extends DileAxios(LitElement) {
5
+ export class DileAjax extends DileAxios(DileI18nMixin(LitElement)) {
5
6
  static get properties() {
6
7
  return {
7
8
  data: { type: Object },
@@ -55,7 +56,7 @@ export class DileAjax extends DileAxios(LitElement) {
55
56
  }));
56
57
  }
57
58
  } else {
58
- this.dispatchError('Unhandled success server response');
59
+ this.dispatchError(this.translations.http_unhandled_success);
59
60
  }
60
61
  })
61
62
  .catch(err => {
@@ -75,26 +76,29 @@ export class DileAjax extends DileAxios(LitElement) {
75
76
  if(err.response.data.message) {
76
77
  this.dispatchError(err.response.data.message);
77
78
  } else {
78
- this.dispatchError("Not found error");
79
+ this.dispatchError(this.translations.http_404);
79
80
  }
80
81
  break;
81
82
  case 401:
82
- this.dispatchError('Unauthorized. Your session may have expired');
83
+ this.dispatchError(this.translations.http_401);
83
84
  break;
85
+ case 405:
86
+ this.dispatchError(this.translations.http_405);
87
+ break;
84
88
  case 419:
85
- this.dispatchError('Your session has expired. Please refresh the page');
89
+ this.dispatchError(this.translations.http_419);
86
90
  break;
87
91
  case 502:
88
- this.dispatchError('Connection error, invalid gateway');
92
+ this.dispatchError(this.translations.http_502);
89
93
  break;
90
94
  case 504:
91
- this.dispatchError('Connection timeout with the gateway');
95
+ this.dispatchError(this.translations.http_504);
92
96
  break;
93
97
  default:
94
- this.dispatchError('Action not completed due to a server error');
98
+ this.dispatchError(this.translations.http_other_error);
95
99
  }
96
100
  } else {
97
- this.dispatchError('No response received from the server');
101
+ this.dispatchError(this.translations.http_no_response);
98
102
  }
99
103
  }
100
104
 
@@ -4,8 +4,9 @@ import '@dile/ui/components/button/button.js';
4
4
  import '@dile/ui/components/inline-feedback/inline-feedback.js';
5
5
  import {capitalizeFirstLetter} from '../../../lib/capitalizeString.js';
6
6
  import { ResponseApiAdapter } from '../../../lib/ResponseApiAdapter.js';
7
+ import { DileI18nMixin } from '../../../lib/DileI18nMixin.js';
7
8
 
8
- export class DileAjaxForm extends LitElement {
9
+ export class DileAjaxForm extends DileI18nMixin(LitElement) {
9
10
  static styles = [
10
11
  css`
11
12
  :host {
@@ -88,6 +89,7 @@ export class DileAjaxForm extends LitElement {
88
89
  url="${this.endpoint}/${this.relatedId}"
89
90
  @ajax-success="${this.doSuccessGet}"
90
91
  @ajax-error="${this.doErrorGet}"
92
+ language="${this.language}"
91
93
  ></dile-ajax>
92
94
  <dile-ajax
93
95
  id="ajaxsave"
@@ -95,12 +97,17 @@ export class DileAjaxForm extends LitElement {
95
97
  url="${this.endpoint}${this.operation == 'insert' ? '' : `/${this.relatedId}`}"
96
98
  @ajax-success="${this.doSuccessSave}"
97
99
  @ajax-error="${this.doErrorSave}"
100
+ language="${this.language}"
98
101
  ></dile-ajax>
99
102
  `
100
103
  }
101
104
 
102
105
  loadData() {
103
- this.ajaxget.generateRequest();
106
+ if(this.ajaxget) {
107
+ this.ajaxget.generateRequest();
108
+ } else {
109
+ setTimeout(() => this.loadData(), 100);
110
+ }
104
111
  }
105
112
 
106
113
  doAction() {
@@ -182,7 +189,7 @@ export class DileAjaxForm extends LitElement {
182
189
  case 'update':
183
190
  return 'put';
184
191
  }
185
- throw "Operation not supported in fct-ajax-form use 'insert' or 'update'";
192
+ throw this.translations.ajax_form_not_supported;
186
193
  }
187
194
 
188
195
  clearErrors() {
@@ -216,9 +223,9 @@ export class DileAjaxForm extends LitElement {
216
223
  return message;
217
224
  }
218
225
  if(success) {
219
- return `Success ${this.operation}`;
226
+ return this.translations.success_operation(this.operation);
220
227
  }
221
- return `Error ${this.operation}`;
228
+ return this.translations.error_operation(this.operation);
222
229
  }
223
230
  }
224
231
 
@@ -0,0 +1,21 @@
1
+ import { translationService } from './translationService.js';
2
+
3
+ export const DileI18nMixin = (superclass) => class extends superclass {
4
+ static get properties() {
5
+ return {
6
+ language: { type: String },
7
+ translations: { type: Object },
8
+ }
9
+ }
10
+
11
+ constructor() {
12
+ super();
13
+ this.language = 'en';
14
+ }
15
+
16
+ async connectedCallback() {
17
+ super.connectedCallback();
18
+ this.translations = await translationService.loadTranslations(this.language);
19
+ }
20
+
21
+ }
package/lib/i18n/en.js ADDED
@@ -0,0 +1,14 @@
1
+ export const translations = {
2
+ http_unhandled_success: "Unhandled success server response",
3
+ http_404: "Not found error",
4
+ http_401: "Unauthorized",
5
+ http_405: "Method Not Allowed",
6
+ http_419: "Your session has expired. Please refresh the page",
7
+ http_502: "Connection error, invalid gateway",
8
+ http_504: "Connection timeout with the gateway",
9
+ http_other_error: "Action not completed due to a server error",
10
+ http_no_response: "No response received from the server",
11
+ ajax_form_not_supported: "Operation not supported in dile-ajax-form use 'insert' or 'update'",
12
+ success_operation: (operation) => operation == 'insert' ? 'The new item has been created' : 'Item updated successfully',
13
+ error_operation: (operation) => `${operation == 'insert' ? 'Insertion' : 'Update'} error`,
14
+ };
package/lib/i18n/es.js ADDED
@@ -0,0 +1,14 @@
1
+ export const translations = {
2
+ http_unhandled_success: "Respuesta de éxito no procesada",
3
+ http_404: "Recurso inexistente",
4
+ http_401: "No autorizado",
5
+ http_405: "Método HTTP no permitido",
6
+ http_419: "La sesión ha expirado. Refresca la página.",
7
+ http_502: "Error de conexión, gateway no válido",
8
+ http_504: "Error de timeout con el gateway",
9
+ http_other_error: "Acción no completada por un error del servidor",
10
+ http_no_response: "No se ha recibido respuesta del servidor",
11
+ ajax_form_not_supported: "Operación no soportada por dile-ajax-form. Elige entre 'insert' o 'update'",
12
+ success_operation: (operation) => `${operation == 'insert' ? 'Inserción' : 'Actualización'} completada con éxito`,
13
+ error_operation: (operation) => `Se ha producido un error en la ${operation == 'insert' ? 'inserción' : 'actualización'}`,
14
+ };
@@ -0,0 +1,27 @@
1
+ class TranslationService {
2
+ constructor() {
3
+ this.translations = {};
4
+ }
5
+
6
+ async loadTranslations(language) {
7
+ try {
8
+ if (!this.translations[language]) {
9
+ const module = await import(`./i18n/${language}.js`);
10
+ this.translations[language] = module.translations;
11
+ }
12
+ } catch (error) {
13
+ console.warn(`Could not load translations for language: ${language}, falling back to English`);
14
+
15
+ if (!this.translations['en']) {
16
+ const defaultModule = await import('./i18n/en.js');
17
+ this.translations['en'] = defaultModule.translations;
18
+ }
19
+
20
+ return this.translations['en'];
21
+ }
22
+
23
+ return this.translations[language];
24
+ }
25
+ }
26
+
27
+ export const translationService = new TranslationService();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/crud",
3
- "version": "0.0.26",
3
+ "version": "0.0.28",
4
4
  "description": "Components to create a generic crud system based on Web Components and Lit",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -31,5 +31,5 @@
31
31
  "publishConfig": {
32
32
  "access": "public"
33
33
  },
34
- "gitHead": "97061a86abf5bd422da53498cf94b900a78d1b3f"
34
+ "gitHead": "f656c500a94dd5ca9157ac2af010d0881047ddf6"
35
35
  }