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

feat: create lg template when schema provides a default value #2487

Merged
merged 2 commits into from
Apr 2, 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
2 changes: 1 addition & 1 deletion Composer/packages/server/schemas/sdk.schema
Original file line number Diff line number Diff line change
Expand Up @@ -9401,7 +9401,7 @@
"description": "Text for no match option.",
"default": "None of the above."
},
"cardNoMatchResponse ": {
"cardNoMatchResponse": {
"$kind": "Microsoft.IActivityTemplate",
"title": "Card no match response",
"description": "Custom response when no match option was selected.",
Expand Down
45 changes: 26 additions & 19 deletions Composer/packages/ui-plugins/lg/src/LgField.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import React, { useCallback, useState, useRef, useEffect } from 'react';
import React, { useCallback, useState, useRef, useEffect, useMemo } from 'react';
import { LgEditor } from '@bfc/code-editor';
import { FieldProps, useShellApi } from '@bfc/extension';
import { FieldLabel } from '@bfc/adaptive-form';
Expand Down Expand Up @@ -37,7 +37,7 @@ const LgField: React.FC<FieldProps<string>> = props => {
const { designerId, currentDialog, lgFiles, shellApi, projectId, locale } = useShellApi();

const singleLgRefMatched = value && value.match(`@\\{([A-Za-z_][-\\w]+)(\\([^\\)]*\\))?\\}`);
const lgName = singleLgRefMatched ? singleLgRefMatched[1] : new LgMetaData(name, designerId || '').toString();
const lgName = singleLgRefMatched ? singleLgRefMatched[1] : new LgMetaData(name.trim(), designerId || '').toString();
const lgFileId = `${currentDialog.lgFile}.${locale}`;
const lgFile = lgFiles && lgFiles.find(file => file.id === lgFileId);

Expand All @@ -48,19 +48,39 @@ const LgField: React.FC<FieldProps<string>> = props => {
[lgName, lgFileId]
);

const template = (lgFile &&
lgFile.templates &&
lgFile.templates.find(template => {
const hasTemplate = useMemo(() => lgFile?.templates.find(t => t.name === lgName), []);
const template = (hasTemplate &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here seems no need to find again, if hasTemplate is not a boolean value. it's equal to

const template = hasTemplate || {
     name: lgName,
     parameters: [],
     body: getInitialTemplate(name, value),
   };

lgFile?.templates.find(template => {
return template.name === lgName;
})) || {
name: lgName,
parameters: [],
body: getInitialTemplate(name, value),
};

const [localValue, setLocalValue] = useState(template.body);
const onChange = (body: string) => {
setLocalValue(body);
if (designerId) {
if (body) {
updateLgTemplate(body);
props.onChange(new LgTemplateRef(lgName).toString());
} else {
shellApi.removeLgTemplate(lgFileId, lgName);
props.onChange();
}
}
};

useEffect(() => {
// create the lg template if the schema porvides a default value
if (template.body && !hasTemplate) {
onChange(template.body);
}
}, []);

const diagnostics = lgFile ? filterTemplateDiagnostics(lgFile.diagnostics, template) : [];

const [localValue, setLocalValue] = useState(template.body);
const sync = useRef(
debounce((shellData: any, localData: any) => {
if (!isEqual(shellData, localData)) {
Expand All @@ -83,19 +103,6 @@ const LgField: React.FC<FieldProps<string>> = props => {
templateId: lgName,
};

const onChange = (body: string) => {
setLocalValue(body);
if (designerId) {
if (body) {
updateLgTemplate(body);
props.onChange(new LgTemplateRef(lgName).toString());
} else {
shellApi.removeLgTemplate(lgFileId, lgName);
props.onChange();
}
}
};

return (
<React.Fragment>
<FieldLabel id={id} label={label} description={description} helpLink={uiOptions?.helpLink} />
Expand Down