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: lg tablelist bugfix & regressions #3868

Merged
merged 4 commits into from
Aug 20, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,15 @@ const TableView: React.FC<TableViewProps> = (props) => {
const moreLabel = formatMessage('Actions');

const onClickEdit = useCallback(
(template: LgTemplate) => {
const { name } = template;
(name: string) => {
navigateTo(`/bot/${projectId}/language-generation/${dialogId}/edit?t=${encodeURIComponent(name)}`);
},
[dialogId, projectId]
);

const onCreateNewTemplate = useCallback(() => {
if (file) {
const newName = lgUtil.increaseNameUtilNotExist(templates, 'TemplateName');
const newName = lgUtil.increaseNameUtilNotExist(file.templates, 'TemplateName');
const payload = {
id: file.id,
template: {
Expand All @@ -80,39 +79,38 @@ const TableView: React.FC<TableViewProps> = (props) => {
} as LgTemplate,
};
createLgTemplate(payload);
setFocusedIndex(templates.length);
setFocusedIndex(file.templates.length);
}
}, [templates, file, projectId]);
}, [file]);

const onRemoveTemplate = useCallback(
(index) => {
(name) => {
if (file) {
const payload = {
id: file.id,
templateName: templates[index].name,
templateName: name,
};

removeLgTemplate(payload);
setFocusedIndex(file.templates.findIndex((item) => item.name === name));
}
},
[templates, file, projectId]
[file]
);

const onCopyTemplate = useCallback(
(index) => {
(name) => {
if (file) {
const name = templates[index].name;
const resolvedName = lgUtil.increaseNameUtilNotExist(templates, `${name}_Copy`);
const resolvedName = lgUtil.increaseNameUtilNotExist(file.templates, `${name}_Copy`);
const payload = {
id: file.id,
fromTemplateName: name,
toTemplateName: resolvedName,
};
copyLgTemplate(payload);
setFocusedIndex(templates.length);
setFocusedIndex(file.templates.length);
}
},
[templates, file, projectId]
[file]
);

const handleTemplateUpdate = useCallback(
Expand All @@ -126,7 +124,7 @@ const TableView: React.FC<TableViewProps> = (props) => {
updateLgTemplate(payload);
}
},
[templates, file, projectId]
[file]
);

const handleTemplateUpdateDefaultLocale = useCallback(
Expand All @@ -140,33 +138,33 @@ const TableView: React.FC<TableViewProps> = (props) => {
updateLgTemplate(payload);
}
},
[templates, file, projectId]
[defaultLangFile]
);

const getTemplatesMoreButtons = useCallback(
(item, index) => {
(item) => {
const buttons = [
{
key: 'edit',
name: formatMessage('Edit'),
onClick: () => {
onClickEdit(templates[index]);
onClickEdit(item.name);
},
},
{
key: 'delete',
name: formatMessage('Delete'),
onClick: () => {
setMessage('item deleted');
onRemoveTemplate(index);
onRemoveTemplate(item.name);
},
},
{
key: 'copy',
name: formatMessage('Make a copy'),
onClick: () => {
setMessage('item copied');
onCopyTemplate(index);
onCopyTemplate(item.name);
},
},
];
Expand Down Expand Up @@ -337,15 +335,15 @@ const TableView: React.FC<TableViewProps> = (props) => {
maxWidth: 50,
fieldName: 'buttons',
data: 'string',
onRender: (item, index) => {
onRender: (item) => {
return (
<TooltipHost calloutProps={{ gapSpace: 10 }} content={moreLabel}>
<IconButton
ariaLabel={moreLabel}
menuIconProps={{ iconName: 'MoreVertical' }}
menuProps={{
shouldFocusOnMount: true,
items: getTemplatesMoreButtons(item, index),
items: getTemplatesMoreButtons(item),
}}
styles={{ menuIcon: { color: NeutralColors.black, fontSize: FontSizes.size16 } }}
/>
Expand Down
7 changes: 1 addition & 6 deletions Composer/packages/client/src/recoilModel/dispatchers/lu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,7 @@ export const luDispatcher = () => {

// body change, only update current locale file
} else {
const intentToUpdate: any = { Body: intent.Body };
// if current update intent not found in origin luFile, do create.
if (!luFile.intents.find((item) => item.Name === intentName)) {
zhixzhan marked this conversation as resolved.
Show resolved Hide resolved
intentToUpdate.Name = intent.Name;
}
const updatedFile = luUtil.updateIntent(luFile, intentName, intentToUpdate);
const updatedFile = luUtil.updateIntent(luFile, intentName, { Body: intent.Body });
return luFiles.map((file) => {
return file.id === id ? updatedFile : file;
});
Expand Down
43 changes: 43 additions & 0 deletions Composer/packages/lib/indexers/__tests__/lgUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,49 @@ describe('update lg template', () => {
expect(templates[0].body).toEqual('-Bye');
});

it('should update lg template with only name', () => {
const content = `# Exit
-Thanks for using todo bot.

# Greeting
-What's up bro`;

const lgFile = parse('a.lg', content, []);
const updatedLgFile = updateTemplate(lgFile, 'Exit', { name: 'Exit1' });
const templates = Templates.parseText(updatedLgFile.content).toArray();
expect(templates.length).toEqual(2);
expect(templates[0].name).toEqual('Exit1');
expect(templates[0].body).toContain('-Thanks for using todo bot.');
});

it('should update lg template with only body', () => {
const content = `# Exit
-Thanks for using todo bot.

# Greeting
-What's up bro`;

const lgFile = parse('a.lg', content, []);
const updatedLgFile = updateTemplate(lgFile, 'Exit', { body: '-Bye' });
const templates = Templates.parseText(updatedLgFile.content).toArray();
expect(templates.length).toEqual(2);
expect(templates[0].name).toEqual('Exit');
expect(templates[0].body).toEqual('-Bye');
});

it('update lg template with empty, should perform a remove', () => {
const content = `# Exit
-Thanks for using todo bot.

# Greeting
-What's up bro`;

const lgFile = parse('a.lg', content, []);
const updatedLgFile = updateTemplate(lgFile, 'Exit', {});
const templates = Templates.parseText(updatedLgFile.content).toArray();
expect(templates.length).toEqual(1);
});

it('should update lg template with error', () => {
const content = `# Exit
-Thanks for using todo bot.\${
Expand Down
54 changes: 54 additions & 0 deletions Composer/packages/lib/indexers/__tests__/luUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,60 @@ hi
expect(luresource2.Sections[1].UtteranceAndEntitiesMap[2].utterance).toEqual('check my mail box please');
});

it('update section with only name', () => {
const intentName = 'CheckEmail';

const luFile1 = luIndexer.parse(fileContent, fileId1);
const updatedLuFile = updateIntent(luFile1, intentName, { Name: 'CheckEmail1' });
const luresource = updatedLuFile.resource;

const { Sections, Errors } = luresource;

expect(Errors.length).toEqual(0);
expect(Sections.length).toEqual(2);
expect(Sections[1].Errors.length).toEqual(0);
expect(luresource.Sections[1].SectionType).toEqual(luSectionTypes.SIMPLEINTENTSECTION);
expect(luresource.Sections[1].Name).toEqual('CheckEmail1');
expect(luresource.Sections[1].UtteranceAndEntitiesMap.length).toEqual(2);
expect(luresource.Sections[1].UtteranceAndEntitiesMap[0].utterance).toEqual('check my email');
expect(luresource.Sections[1].UtteranceAndEntitiesMap[1].utterance).toEqual('show my emails');
});

it('update section with only body', () => {
const intentName = 'CheckEmail';
const updatedBody = `- check my email
- show my emails 2
- check my mail box please`;

const luFile1 = luIndexer.parse(fileContent, fileId1);
const updatedLuFile = updateIntent(luFile1, intentName, { Body: updatedBody });
const luresource = updatedLuFile.resource;

const { Sections, Errors } = luresource;

expect(Errors.length).toEqual(0);
expect(Sections.length).toEqual(2);
expect(Sections[1].Errors.length).toEqual(0);
expect(luresource.Sections[1].SectionType).toEqual(luSectionTypes.SIMPLEINTENTSECTION);
expect(luresource.Sections[1].Name).toEqual('CheckEmail');
expect(luresource.Sections[1].UtteranceAndEntitiesMap.length).toEqual(3);
expect(luresource.Sections[1].UtteranceAndEntitiesMap[0].utterance).toEqual('check my email');
expect(luresource.Sections[1].UtteranceAndEntitiesMap[1].utterance).toEqual('show my emails 2');
expect(luresource.Sections[1].UtteranceAndEntitiesMap[2].utterance).toEqual('check my mail box please');
});

it('update section with empty, should perform a remove', () => {
const intentName = 'CheckEmail';
const luFile1 = luIndexer.parse(fileContent, fileId1);
const updatedLuFile = updateIntent(luFile1, intentName, null);
const luresource = updatedLuFile.resource;

const { Sections, Errors } = luresource;

expect(Errors.length).toEqual(0);
expect(Sections.length).toEqual(1);
});

it('update section with syntax error: missing -', () => {
const intentName = 'CheckEmail';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { createSingleMessage, combineMessage, findErrors, isValid } from '../../src/utils';
import { createSingleMessage, combineMessage, findErrors, isValid } from '../../src/utils/diagnosticUtil';

const diagnostics = [
{
Expand Down
21 changes: 16 additions & 5 deletions Composer/packages/lib/indexers/src/utils/lgUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Templates, Diagnostic as LGDiagnostic, ImportResolverDelegate } from 'b
import { LgTemplate, importResolverGenerator, TextFile, Diagnostic, Position, Range, LgFile } from '@bfc/shared';
import get from 'lodash/get';
import formatMessage from 'format-message';
import isEmpty from 'lodash/isEmpty';

import { lgIndexer } from '../lgIndexer';

Expand Down Expand Up @@ -72,22 +73,32 @@ export function increaseNameUtilNotExist(templates: LgTemplate[], name: string):
export function updateTemplate(
lgFile: LgFile,
templateName: string,
{ name, parameters, body }: { name?: string; parameters?: string[]; body?: string },
template: { name?: string; parameters?: string[]; body?: string },
importResolver?: ImportResolverDelegate
): LgFile {
const { id, content } = lgFile;
const { name, parameters, body } = template;
const resource = Templates.parseText(content, undefined, importResolver);
const originTemplate = resource.toArray().find((t) => t.name === templateName);
const templateToUpdate = {
name: name || originTemplate?.name || templateName,
parameters: parameters || originTemplate?.parameters || [],
body: body || originTemplate?.body || '',
};

let templates;
// add if not exist
if (!originTemplate) {
templates = resource.addTemplate(templateName, parameters || [], body || '');
templates = resource.addTemplate(templateName, templateToUpdate.parameters, templateToUpdate.body);
// remove if template is null
} else if (!template || isEmpty(template)) {
templates = resource.deleteTemplate(templateName);
} else {
templates = resource.updateTemplate(
templateName,
name || originTemplate.name,
parameters || originTemplate.parameters,
body || originTemplate.body
templateToUpdate.name,
templateToUpdate.parameters,
templateToUpdate.body
);
}

Expand Down
2 changes: 1 addition & 1 deletion Composer/packages/lib/indexers/src/utils/luUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export function updateIntent(
const orginSection = Sections.find(({ Name }) => Name === intentName);
const intentToUpdate: LuIntentSection = {
...orginSection,
Name: intent?.Name || orginSection?.Name || '',
Name: intent?.Name || orginSection?.Name || intentName,
Body: intent?.Body || orginSection?.Body || '',
};

Expand Down