FIX: form template cooked heading from label (#23359)

This commit is contained in:
Renato Atilio 2023-08-31 16:58:09 -03:00 committed by GitHub
parent f1d8cd529e
commit 3eef3dc88e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 22 additions and 6 deletions

View File

@ -126,6 +126,7 @@
@disableTextarea={{this.composer.disableTextarea}} @disableTextarea={{this.composer.disableTextarea}}
@formTemplateIds={{this.composer.formTemplateIds}} @formTemplateIds={{this.composer.formTemplateIds}}
@formTemplateInitialValues={{this.composer.formTemplateInitialValues}} @formTemplateInitialValues={{this.composer.formTemplateInitialValues}}
@onSelectFormTemplate={{this.composer.onSelectFormTemplate}}
> >
<div class="composer-fields"> <div class="composer-fields">
<PluginOutlet <PluginOutlet

View File

@ -18,6 +18,7 @@
@popupMenuOptions={{this.popupMenuOptions}} @popupMenuOptions={{this.popupMenuOptions}}
@formTemplateIds={{this.formTemplateIds}} @formTemplateIds={{this.formTemplateIds}}
@formTemplateInitialValues={{@formTemplateInitialValues}} @formTemplateInitialValues={{@formTemplateInitialValues}}
@onSelectFormTemplate={{@onSelectFormTemplate}}
@replyingToTopic={{this.composer.replyingToTopic}} @replyingToTopic={{this.composer.replyingToTopic}}
@editingPost={{this.composer.editingPost}} @editingPost={{this.composer.editingPost}}
@disabled={{this.disableTextarea}} @disabled={{this.disableTextarea}}

View File

@ -18,6 +18,7 @@
<FormTemplateField::Wrapper <FormTemplateField::Wrapper
@id={{this.selectedFormTemplateId}} @id={{this.selectedFormTemplateId}}
@initialValues={{@formTemplateInitialValues}} @initialValues={{@formTemplateInitialValues}}
@onSelectFormTemplate={{@onSelectFormTemplate}}
/> />
</form> </form>
{{else}} {{else}}

View File

@ -23,6 +23,8 @@ export default class FormTemplateFieldWrapper extends Component {
_loadTemplate(templateContent) { _loadTemplate(templateContent) {
try { try {
this.parsedTemplate = Yaml.load(templateContent); this.parsedTemplate = Yaml.load(templateContent);
this.args.onSelectFormTemplate?.(this.parsedTemplate);
} catch (e) { } catch (e) {
this.error = e; this.error = e;
} }

View File

@ -1,6 +1,11 @@
import I18n from "I18n"; import I18n from "I18n";
export default function prepareFormTemplateData(form) { export default function prepareFormTemplateData(form, formTemplate) {
const labelMap = formTemplate.reduce((acc, field) => {
acc[field.id] = field.attributes.label;
return acc;
}, {});
const formData = new FormData(form); const formData = new FormData(form);
// Validate the form template // Validate the form template
@ -36,7 +41,7 @@ export default function prepareFormTemplateData(form) {
const key = Object.keys(item)[0]; const key = Object.keys(item)[0];
const value = item[key]; const value = item[key];
if (value) { if (value) {
return `### ${key}\n${value}`; return `### ${labelMap[key]}\n${value}`;
} }
}); });

View File

@ -182,6 +182,11 @@ export default class ComposerService extends Service {
return this.set("_formTemplateInitialValues", values); return this.set("_formTemplateInitialValues", values);
} }
@action
onSelectFormTemplate(formTemplate) {
this.selectedFormTemplate = formTemplate;
}
@discourseComputed("showPreview") @discourseComputed("showPreview")
toggleText(showPreview) { toggleText(showPreview) {
return showPreview return showPreview
@ -920,7 +925,8 @@ export default class ComposerService extends Service {
!this.get("model.editingPost") !this.get("model.editingPost")
) { ) {
const formTemplateData = prepareFormTemplateData( const formTemplateData = prepareFormTemplateData(
document.querySelector("#form-template-form") document.querySelector("#form-template-form"),
this.selectedFormTemplate
); );
if (formTemplateData) { if (formTemplateData) {
this.model.set("reply", formTemplateData); this.model.set("reply", formTemplateData);

View File

@ -229,14 +229,14 @@ describe "Composer Form Templates", type: :system do
composer.fill_title(topic_title) composer.fill_title(topic_title)
composer.fill_form_template_field("input", "Bruce Wayne") composer.fill_form_template_field("input", "Bruce Wayne")
composer.create composer.create
topic = Topic.where(user: user, title: topic_title)
topic_id = Topic.where(user: user, title: topic_title).pluck(:id)
post = Post.where(topic_id: topic_id).first
expect(topic_page).to have_topic_title(topic_title) expect(topic_page).to have_topic_title(topic_title)
expect(find("#{topic_page.post_by_number_selector(1)} .cooked p")).to have_content( expect(find("#{topic_page.post_by_number_selector(1)} .cooked p")).to have_content(
"Bruce Wayne", "Bruce Wayne",
) )
expect(find("#{topic_page.post_by_number_selector(1)} .cooked h3")).to have_content(
"What is your full name?",
)
end end
it "creates a post with an upload field" do it "creates a post with an upload field" do