Skip to content

Commit 0b7a812

Browse files
author
Alyar
committed
Fix the sendAiColumnRequest method
1 parent 28d9be0 commit 0b7a812

8 files changed

Lines changed: 91 additions & 208 deletions

File tree

packages/devextreme/js/__internal/grids/grid_core/ai_column/ai_column.integration.test.ts

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -567,91 +567,6 @@ describe('Public methods', () => {
567567

568568
expect(sendRequestSpy).toHaveBeenCalled();
569569
});
570-
571-
it('should call callback on completion', async () => {
572-
const sendRequestSpy = jest.fn();
573-
const onCompleteSpy = jest.fn();
574-
575-
const aiIntegration = new AIIntegration({
576-
sendRequest(): RequestResult {
577-
return {
578-
promise: new Promise<string>((resolve) => {
579-
sendRequestSpy();
580-
resolve('1');
581-
}),
582-
abort: (): void => { },
583-
};
584-
},
585-
});
586-
587-
const { instance } = await createDataGrid({
588-
dataSource: [
589-
{ id: 1, name: 'Name 1', value: 10 },
590-
],
591-
keyExpr: 'id',
592-
aiIntegration,
593-
columns: [
594-
{ dataField: 'id', caption: 'ID' },
595-
{ dataField: 'name', caption: 'Name' },
596-
{ dataField: 'value', caption: 'Value' },
597-
{
598-
type: 'ai',
599-
caption: 'AI Column',
600-
name: 'myColumn',
601-
},
602-
],
603-
});
604-
605-
instance.sendAIColumnRequest('myColumn', { onComplete: onCompleteSpy });
606-
jest.runAllTimers();
607-
await Promise.resolve();
608-
609-
expect(sendRequestSpy).toHaveBeenCalled();
610-
expect(onCompleteSpy).toHaveBeenCalledWith(1);
611-
});
612-
613-
it('should call callback on error', async () => {
614-
const sendRequestSpy = jest.fn();
615-
const onErrorSpy = jest.fn();
616-
617-
const aiIntegration = new AIIntegration({
618-
sendRequest(): RequestResult {
619-
return {
620-
promise: new Promise<string>((_resolve, reject) => {
621-
sendRequestSpy();
622-
reject(new Error('Error'));
623-
}),
624-
abort: (): void => { },
625-
};
626-
},
627-
});
628-
629-
const { instance } = await createDataGrid({
630-
dataSource: [
631-
{ id: 1, name: 'Name 1', value: 10 },
632-
],
633-
keyExpr: 'id',
634-
aiIntegration,
635-
columns: [
636-
{ dataField: 'id', caption: 'ID' },
637-
{ dataField: 'name', caption: 'Name' },
638-
{ dataField: 'value', caption: 'Value' },
639-
{
640-
type: 'ai',
641-
caption: 'AI Column',
642-
name: 'myColumn',
643-
},
644-
],
645-
});
646-
647-
instance.sendAIColumnRequest('myColumn', { onError: onErrorSpy });
648-
jest.runAllTimers();
649-
await Promise.resolve();
650-
await Promise.resolve();
651-
652-
expect(sendRequestSpy).toHaveBeenCalled();
653-
expect(onErrorSpy).toHaveBeenCalledWith(new Error('Error'));
654-
});
655570
});
656571
});
657572

packages/devextreme/js/__internal/grids/grid_core/ai_column/m_ai_column_controller.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
2-
import type { GenerateGridColumnCommandResult, RequestCallbacks } from '@js/common/ai-integration';
2+
import type { Callback } from '@js/core/utils/callbacks';
33

44
import type { ColumnsController } from '../columns_controller/m_columns_controller';
55
import type { DataController } from '../data_controller/m_data_controller';
@@ -18,6 +18,14 @@ export class AiColumnController extends Controller {
1818

1919
private dataChangedHandler!: (e) => any;
2020

21+
public aiRequestCompleted!: Callback;
22+
23+
public aiRequestRejected!: Callback;
24+
25+
protected callbackNames(): string[] {
26+
return ['aiRequestCompleted', 'aiRequestRejected'];
27+
}
28+
2129
public init(): void {
2230
this.columnsController = this.getController('columns');
2331
this.dataController = this.getController('data');
@@ -49,7 +57,7 @@ export class AiColumnController extends Controller {
4957
.filter((col) => col.type === 'ai' && col.ai.mode === 'auto');
5058

5159
for (const col of aiColumns) {
52-
this.refreshAIColumn(col.name, {});
60+
this.refreshAIColumn(col.name);
5361
}
5462
}
5563

@@ -75,16 +83,21 @@ export class AiColumnController extends Controller {
7583

7684
public sendAIColumnRequest(
7785
columnName: string,
78-
callbacks?: RequestCallbacks<GenerateGridColumnCommandResult>,
7986
): void {
80-
this.aiColumnIntegrationController.sendRequest(columnName, callbacks);
87+
this.aiColumnIntegrationController.sendRequest(columnName, {
88+
onComplete: (data) => {
89+
this.aiRequestCompleted.fire(data);
90+
},
91+
onError: (error: Error) => {
92+
this.aiRequestRejected.fire(error);
93+
},
94+
});
8195
}
8296

8397
public refreshAIColumn(
8498
columnName: string,
85-
callbacks: RequestCallbacks<GenerateGridColumnCommandResult>,
8699
): void {
87-
this.aiColumnIntegrationController.sendRequest(columnName, callbacks);
100+
this.sendAIColumnRequest(columnName);
88101
}
89102

90103
public clearAIColumn(columnName: string): void {

0 commit comments

Comments
 (0)