Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bnTable stop loading when error in response #880

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion projects/ion/src/core/bn-table/bn-table.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Observable, of } from 'rxjs';
import { Observable, of, throwError } from 'rxjs';
import BnTable, { IBnTable } from './bn-table';
import { BnService, IResponse } from '../api/http.interfaces';
import { SmartTableEvent } from '../../public-api';
Expand Down Expand Up @@ -26,6 +26,14 @@ class MockEmptyService implements BnService<MockItemData> {
}
}

// Defina um erro de exemplo
const mockError = new Error('Request Error');
class MockServiceError implements BnService<MockItemData> {
list(): Observable<IResponse<MockItemData>> {
return throwError(mockError);
}
}

describe('BnTable', () => {
let bnTable: BnTable<MockItemData>;
const mockService: MockService = new MockService();
Expand Down Expand Up @@ -281,3 +289,20 @@ describe('BnTable', () => {
});
});
});

describe('BnTable Error', () => {
let bnTable: BnTable<MockItemData>;
const mockServiceWithError: MockServiceError = new MockServiceError();

it('should stop loading when receive an error', () => {
const config: IBnTable<MockItemData> = {
service: mockServiceWithError,
tableConfig: {
columns: [{ label: 'Name', key: 'name' }],
actions: [{ label: 'Remove', icon: 'trash' }],
},
};
bnTable = new BnTable<MockItemData>({ ...config });
expect(bnTable.configTable.loading).toBeFalsy();
});
});
56 changes: 28 additions & 28 deletions projects/ion/src/core/bn-table/bn-table.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { forkJoin, of } from 'rxjs';
import { take } from 'rxjs/operators';
import { finalize, take } from 'rxjs/operators';
import { LIST_OF_PAGE_OPTIONS } from '../../lib/pagination/pagination.component';
import { ConfigTable, EventTable } from '../../lib/table/utilsTable';
import {
Expand Down Expand Up @@ -95,34 +95,34 @@ export default class BnTable<DataType> {
.list({ ...this.payload, total: false })
.pipe(take(1));

forkJoin([totalRequest$, dataRequest$]).subscribe(
(response) => {
this.configTable.loading = false;

const [totalResponse, dataResponse]: [
IResponse<DataType>,
IResponse<DataType>
] = response;

if (totalResponse && totalResponse.total !== null) {
this.configTable.pagination = {
...this.configTable.pagination,
total: totalResponse.total || 0,
};
}

this.configTable.data = dataResponse.dados || dataResponse.data || [];

if (this.formatData) {
this.configTable.data = this.formatData(this.configTable.data);
forkJoin([totalRequest$, dataRequest$])
.pipe(finalize(() => (this.configTable.loading = false)))
.subscribe(
(response) => {
const [totalResponse, dataResponse]: [
IResponse<DataType>,
IResponse<DataType>
] = response;

if (totalResponse && totalResponse.total !== null) {
this.configTable.pagination = {
...this.configTable.pagination,
total: totalResponse.total || 0,
};
}

this.configTable.data = dataResponse.dados || dataResponse.data || [];

if (this.formatData) {
this.configTable.data = this.formatData(this.configTable.data);
}
},
() => {
// TODO: add notification service
// const msg: string = error.msg || error.error.msg;
// this.notify.error('Erro', msg);
}
},
(error) => {
// TODO: add notification service
// const msg: string = error.msg || error.error.msg;
// this.notify.error('Erro', msg);
}
);
);
}

events(event: SmartTableEvent): void {
Expand Down
Loading