diff --git a/packages/form-builder/addon/components/cfb-form-editor/question-usage.hbs b/packages/form-builder/addon/components/cfb-form-editor/question-usage.hbs new file mode 100644 index 000000000..f28cf3281 --- /dev/null +++ b/packages/form-builder/addon/components/cfb-form-editor/question-usage.hbs @@ -0,0 +1,50 @@ +{{#if this.otherForms}} + + {{this.title}} + +{{/if}} + + + + {{t "caluma.form-builder.question.usage.references-heading"}} + + + + + \ No newline at end of file diff --git a/packages/form-builder/addon/components/cfb-form-editor/question-usage.js b/packages/form-builder/addon/components/cfb-form-editor/question-usage.js new file mode 100644 index 000000000..55491978e --- /dev/null +++ b/packages/form-builder/addon/components/cfb-form-editor/question-usage.js @@ -0,0 +1,46 @@ +import { inject as service } from "@ember/service"; +import Component from "@glimmer/component"; +import { tracked } from "@glimmer/tracking"; +import { queryManager } from "ember-apollo-client"; +import { trackedFunction } from "ember-resources/util/function"; + +import allFormsForQuestionQuery from "@projectcaluma/ember-form-builder/gql/queries/all-forms-for-question.graphql"; + +export default class CfbFormEditorQuestionUsage extends Component { + @service intl; + @queryManager apollo; + + @tracked modalVisible = false; + + get title() { + return this.intl.t("caluma.form-builder.question.usage.title", { + n: this.otherForms, + // for highlighting the number we use the tag + htmlSafe: true, + }); + } + + get otherForms() { + return this.forms.value?.length - 1 ?? 0; + } + + forms = trackedFunction(this, async () => { + try { + const forms = await this.apollo.query( + { + query: allFormsForQuestionQuery, + variables: { + slug: this.args.model.slug, + }, + fetchPolicy: "no-cache", + }, + "allForms.edges", + ); + + return forms; + } catch (error) { + console.error(error); + return { value: [] }; + } + }); +} diff --git a/packages/form-builder/addon/components/cfb-form-editor/question.hbs b/packages/form-builder/addon/components/cfb-form-editor/question.hbs index 600b7a7a2..a46b6f96f 100644 --- a/packages/form-builder/addon/components/cfb-form-editor/question.hbs +++ b/packages/form-builder/addon/components/cfb-form-editor/question.hbs @@ -440,7 +440,9 @@ /> {{/if}} -
+
+ + `, + ); + + await waitFor("[data-test-show-question-usage-modal-link]"); + await click("[data-test-show-question-usage-modal-link]"); + + assert.dom("[data-test-question-usage-modal]").isVisible(); + assert.dom("[data-test-question-form-item]").exists({ count: 3 }); + }); + + test("it is hidden when no other reference to this question exists", async function (assert) { + this.set( + "question", + this.server.create("question", { + label: "Test Label", + slug: "test-slug", + type: "TEXT", + }), + ); + + this.server.create("form", { questions: [this.question] }); + + await render( + hbs``, + ); + + assert.dom("[data-test-show-question-usage-modal-link]").isNotVisible(); + }); + }, +); diff --git a/packages/form-builder/translations/de.yaml b/packages/form-builder/translations/de.yaml index 27fab506c..11456f0dd 100644 --- a/packages/form-builder/translations/de.yaml +++ b/packages/form-builder/translations/de.yaml @@ -134,6 +134,11 @@ caluma: hideLabel: "Label verstecken" + usage: + title: "Diese Frage wird in {n,plural, =1 {einem Formular} other {# Formularen}} verwendet." + references-heading: "Alle Verweise auf diese Frage" + not-published: "nicht publiziert" + options: delete: "Option löschen" archive: "Option archivieren (verstecken)" diff --git a/packages/form-builder/translations/en.yaml b/packages/form-builder/translations/en.yaml index c589ad23a..802220bc1 100644 --- a/packages/form-builder/translations/en.yaml +++ b/packages/form-builder/translations/en.yaml @@ -134,6 +134,11 @@ caluma: hideLabel: "Hide label" + usage: + title: "This question is used in {n,plural, =1 {another form} other {# other forms}}" + references-heading: "All references of this question" + not-published: "not published" + options: delete: "Delete option" archive: "Archive (hide) option" diff --git a/packages/form-builder/translations/fr.yaml b/packages/form-builder/translations/fr.yaml index 81fa3630f..4376e4288 100644 --- a/packages/form-builder/translations/fr.yaml +++ b/packages/form-builder/translations/fr.yaml @@ -134,6 +134,11 @@ caluma: hideLabel: "Cacher l'étiquette" + usage: + title: "Cette question est {n,plural, =1 {utilisé sous une forme} other {utilisé sous # formes}}." + references-heading: "Toutes les références à cette question" + not-published: "non publié" + options: delete: "Supprimer l'option" archive: "Archiver (masquer) l'option"