@dile/crud 2.9.4 → 2.9.5

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.
@@ -360,3 +360,94 @@ describe('dile-crud list/grid view switch', () => {
360
360
  expect(el.listElement.viewMode).toBe('item');
361
361
  });
362
362
  });
363
+
364
+ describe('dile-crud scope and refresh', () => {
365
+ afterEach(() => {
366
+ document.body.innerHTML = '';
367
+ });
368
+
369
+ const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
370
+
371
+ async function createCrud() {
372
+ const config = new CrudConfigBuilder('https://example.test/api/invoices', {
373
+ customization: {
374
+ disableInsert: true,
375
+ disableHelp: true,
376
+ disableKeywordSearch: true,
377
+ disableSort: true,
378
+ disableFilter: true,
379
+ disablePagination: true,
380
+ hideCheckboxSelection: true,
381
+ },
382
+ templates: { item: (item) => html`<span>${item.name}</span>` },
383
+ }).getConfig();
384
+
385
+ const el = document.createElement('dile-crud');
386
+ el.config = config;
387
+ el.belongsTo = 'customer';
388
+ el.relationId = '10';
389
+ document.body.appendChild(el);
390
+ await el.updateComplete;
391
+ const list = el.listElement;
392
+ await list.updateComplete;
393
+ await list.elservice.updateComplete;
394
+
395
+ // Intercept the outgoing requests instead of hitting the network,
396
+ // and let the initial load (fired on start) settle first.
397
+ const requests = [];
398
+ const ajax = list.elservice.ajaxget;
399
+ ajax.generateRequest = () => requests.push({ url: ajax.url, data: ajax.data });
400
+ list.elservice.delayTime = 10;
401
+ await wait(250);
402
+ requests.length = 0;
403
+ return { el, list, requests, ajax };
404
+ }
405
+
406
+ it('setScope() updates belongsTo and relationId and reloads the list once from page 1', async () => {
407
+ const { el, list, requests, ajax } = await createCrud();
408
+ ajax.url = 'https://example.test/api/invoices?page=2';
409
+
410
+ el.setScope('supplier', '20');
411
+ expect(el.belongsTo).toBe('supplier');
412
+ expect(el.relationId).toBe('20');
413
+ await el.updateComplete;
414
+ await list.updateComplete;
415
+ await wait(50);
416
+
417
+ expect(list.belongsTo).toBe('supplier');
418
+ expect(list.relationId).toBe('20');
419
+ expect(requests).toHaveLength(1);
420
+ expect(requests[0].url).toBe('https://example.test/api/invoices');
421
+ expect(requests[0].data.belongsTo).toBe('supplier');
422
+ expect(requests[0].data.relationId).toBe('20');
423
+ });
424
+
425
+ it('setScope() without arguments clears the scope', async () => {
426
+ const { el, list, requests } = await createCrud();
427
+
428
+ el.setScope();
429
+ await el.updateComplete;
430
+ await list.updateComplete;
431
+ await wait(50);
432
+
433
+ expect(el.belongsTo).toBe('');
434
+ expect(el.relationId).toBe('');
435
+ expect(requests).toHaveLength(1);
436
+ expect(requests[0].data.belongsTo).toBeUndefined();
437
+ expect(requests[0].data.relationId).toBeUndefined();
438
+ });
439
+
440
+ it('refresh() keeps the current page and refresh({ resetPage: true }) goes back to page 1', async () => {
441
+ const { el, requests, ajax } = await createCrud();
442
+ const pagedUrl = 'https://example.test/api/invoices?page=2';
443
+
444
+ ajax.url = pagedUrl;
445
+ el.refresh();
446
+ await wait(50);
447
+ expect(requests.at(-1).url).toBe(pagedUrl);
448
+
449
+ el.refresh({ resetPage: true });
450
+ await wait(50);
451
+ expect(requests.at(-1).url).toBe('https://example.test/api/invoices');
452
+ });
453
+ });
@@ -525,8 +525,13 @@ export class DileCrud extends DileI18nMixin(DileCrudMixin(LitElement)) {
525
525
  this.refresh();
526
526
  }
527
527
 
528
- refresh() {
529
- this.listElement.refresh();
528
+ refresh({ resetPage = false } = {}) {
529
+ this.listElement.refresh({ resetPage });
530
+ }
531
+
532
+ setScope(belongsTo = '', relationId = '') {
533
+ this.belongsTo = belongsTo;
534
+ this.relationId = relationId;
530
535
  }
531
536
 
532
537
  setCustomizationOption(optionName, value) {
@@ -371,3 +371,85 @@ describe('dile-crud-list grid template integration', () => {
371
371
  expect(el.shadowRoot.querySelector('dile-crud-data-grid')).toBeTruthy();
372
372
  });
373
373
  });
374
+
375
+ describe('dile-crud-list scope changes and page reset', () => {
376
+ afterEach(() => {
377
+ document.body.innerHTML = '';
378
+ });
379
+
380
+ const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
381
+
382
+ async function createList({ belongsTo, relationId } = {}) {
383
+ const config = new CrudConfigBuilder('https://example.test/api/invoices', {
384
+ templates: { item: (item) => html`<span>${item.name}</span>` },
385
+ }).getConfig();
386
+
387
+ const el = document.createElement('dile-crud-list');
388
+ el.disableLoadOnStart = true;
389
+ el.config = config;
390
+ if (belongsTo !== undefined) el.belongsTo = belongsTo;
391
+ if (relationId !== undefined) el.relationId = relationId;
392
+ document.body.appendChild(el);
393
+ await el.updateComplete;
394
+ await el.elservice.updateComplete;
395
+
396
+ // Intercept the outgoing requests instead of hitting the network.
397
+ const requests = [];
398
+ const ajax = el.elservice.ajaxget;
399
+ ajax.generateRequest = () => requests.push({ url: ajax.url, data: ajax.data });
400
+ el.elservice.delayTime = 10;
401
+ return { el, requests, ajax };
402
+ }
403
+
404
+ it('does not fire an extra request on the initial render when scope is set from the start', async () => {
405
+ const { requests } = await createList({ belongsTo: 'customer', relationId: '10' });
406
+ await wait(50);
407
+ expect(requests).toHaveLength(0);
408
+ });
409
+
410
+ it('reloads from the first page with the new scope when belongsTo and relationId change', async () => {
411
+ const { el, requests, ajax } = await createList({ belongsTo: 'customer', relationId: '10' });
412
+ ajax.url = 'https://example.test/api/invoices?page=3';
413
+
414
+ let refreshEvents = 0;
415
+ el.addEventListener('crud-list-refresh', () => refreshEvents++);
416
+
417
+ el.belongsTo = 'supplier';
418
+ el.relationId = '20';
419
+ await el.updateComplete;
420
+ expect(el.loading).toBe(true);
421
+
422
+ await wait(50);
423
+ expect(refreshEvents).toBe(1);
424
+ expect(requests).toHaveLength(1);
425
+ expect(requests[0].url).toBe('https://example.test/api/invoices');
426
+ expect(requests[0].data.belongsTo).toBe('supplier');
427
+ expect(requests[0].data.relationId).toBe('20');
428
+ });
429
+
430
+ it('reloads when only relationId changes', async () => {
431
+ const { el, requests } = await createList({ belongsTo: 'customer', relationId: '10' });
432
+
433
+ el.relationId = '11';
434
+ await el.updateComplete;
435
+ await wait(50);
436
+
437
+ expect(requests).toHaveLength(1);
438
+ expect(requests[0].data.relationId).toBe('11');
439
+ });
440
+
441
+ it('keeps the current page on refresh() and goes back to page 1 on refresh({ resetPage: true })', async () => {
442
+ const { el, requests, ajax } = await createList();
443
+ const pagedUrl = 'https://example.test/api/invoices?page=3';
444
+
445
+ ajax.url = pagedUrl;
446
+ el.refresh();
447
+ await wait(50);
448
+ expect(requests.at(-1).url).toBe(pagedUrl);
449
+
450
+ el.refresh({ resetPage: true });
451
+ await wait(50);
452
+ expect(requests.at(-1).url).toBe('https://example.test/api/invoices');
453
+ expect(requests).toHaveLength(2);
454
+ });
455
+ });
@@ -132,6 +132,12 @@ export class DileCrudList extends DileI18nMixin(DileLoading(LitElement)) {
132
132
  }
133
133
  }
134
134
 
135
+ willUpdate(changedProperties) {
136
+ if (this.hasUpdated && (changedProperties.has('belongsTo') || changedProperties.has('relationId'))) {
137
+ this.refresh({ resetPage: true });
138
+ }
139
+ }
140
+
135
141
  render() {
136
142
  return html`
137
143
  ${this.ajaxTemplate}
@@ -363,7 +369,7 @@ export class DileCrudList extends DileI18nMixin(DileLoading(LitElement)) {
363
369
  this.elservice.goPrev();
364
370
  }
365
371
 
366
- refresh() {
372
+ refresh({ resetPage = false } = {}) {
367
373
  this.dispatchEvent(new CustomEvent('crud-list-refresh', {
368
374
  bubbles: true,
369
375
  composed: true,
@@ -373,7 +379,7 @@ export class DileCrudList extends DileI18nMixin(DileLoading(LitElement)) {
373
379
  if (this.isSelectAllActive) {
374
380
  this.shadowRoot.querySelector('dile-crud-select-all').reset();
375
381
  }
376
- this.elservice.refresh();
382
+ resetPage ? this.elservice.cleanAndRefresh() : this.elservice.refresh();
377
383
  }
378
384
 
379
385
  setKeyword(keyword) {
@@ -166,7 +166,7 @@ export class DileCrudListService extends DileI18nMixin(LitElement) {
166
166
  cleanAndRefresh() {
167
167
  if(this.ajaxget) {
168
168
  this.ajaxget.url = this.cleanUrl;
169
- this.refresh();
170
169
  }
170
+ this.refresh();
171
171
  }
172
172
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/crud",
3
- "version": "2.9.4",
3
+ "version": "2.9.5",
4
4
  "description": "Components to create a generic crud system based on Web Components and Lit",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -32,5 +32,5 @@
32
32
  "publishConfig": {
33
33
  "access": "public"
34
34
  },
35
- "gitHead": "eb8d274006be88ab2b44ed574f5c2829f27a2ec3"
35
+ "gitHead": "388965a96f6e0d8da0fdadb2729eaa4b04b3bba1"
36
36
  }