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: copy forms #2489

Merged
merged 6 commits into from
Feb 16, 2024
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
@@ -0,0 +1,50 @@
<UkModal
@visible={{this.visible}}
@onHide={{fn (mut this.visible) false}}
data-test-copy-form-modal
as |modal|
>
{{#if this.visible}}
<ValidatedForm
@model={{this.changeset}}
@on-submit={{perform this.submit}}
as |f|
>
<modal.header>
<h2 class="uk-modal-title">
{{t "caluma.form-builder.copy-modal.title" form=@item.name}}
</h2>
</modal.header>
<modal.body>
<f.input
@name="name"
@label={{t "caluma.form-builder.copy-modal.name.label"}}
@hint={{t "caluma.form-builder.copy-modal.name.hint" name=@item.name}}
data-test-copy-modal-input-name
/>
<f.input
@name="slug"
@label={{t "caluma.form-builder.copy-modal.slug.label"}}
@hint={{t "caluma.form-builder.copy-modal.slug.hint" slug=@item.slug}}
@renderComponent={{component "cfb-slug-input" hidePrefix=false}}
data-test-copy-modal-input-slug
/>
</modal.body>
<modal.footer class="uk-text-right">
<f.submit
@loading={{this.submit.isRunning}}
@disabled={{or
this.submit.isRunning
(not this.changeset.isValid)
(eq @item.slug this.changeset.slug)
}}
data-test-copy-form-submit
>
{{t "caluma.form-builder.copy-modal.submit"}}
</f.submit>
</modal.footer>
</ValidatedForm>
{{/if}}
</UkModal>

{{yield (hash toggle=this.toggle)}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { action } from "@ember/object";
import { inject as service } from "@ember/service";
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { queryManager } from "ember-apollo-client";
import { Changeset } from "ember-changeset";
import lookupValidator from "ember-changeset-validations";
import { dropTask } from "ember-concurrency";

import copyFormMutation from "@projectcaluma/ember-form-builder/gql/mutations/copy-form.graphql";
import validations from "@projectcaluma/ember-form-builder/validations/form";

export default class CfbFormEditorCopyModal extends Component {
@queryManager apollo;
@service notification;
@service router;
@service intl;
@tracked visible = false;

constructor(owner, args) {
super(owner, args);

this.changeset = Changeset(
{ ...this.args.item, id: undefined },
lookupValidator(validations),
validations,
);
}

@action
toggle() {
this.visible = !this.visible;
}

@dropTask
*submit(changeset) {
try {
yield this.apollo.mutate(
{
mutation: copyFormMutation,
variables: {
input: {
source: this.args.item.slug,
name: changeset.name,
slug: changeset.slug,
},
},
},
"copyForm.form",
);

this.notification.success(
this.intl.t("caluma.form-builder.notification.form.create.success"),
);

this.router.transitionTo("edit", changeset.slug);
} catch (e) {
this.notification.danger(
this.intl.t("caluma.form-builder.notification.form.create.error"),
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@
/>

<div class="uk-text-right">
<CfbFormEditor::CopyModal @item={{this.data}} as |modal|>
<f.button
@disabled={{f.loading}}
@label={{t "caluma.form-builder.copy-modal.submit"}}
{{on "click" modal.toggle}}
data-test-copy-form-button={{@slug}}
/>
</CfbFormEditor::CopyModal>

<f.submit
@disabled={{f.loading}}
@label={{t "caluma.form-builder.global.save"}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
type="button"
class="uk-icon-button uk-animation-slide-top"
uk-icon="close"
title={{t "caluma.form-builder.global.ca ncel"}}
title={{t "caluma.form-builder.global.cancel"}}
{{on "click" (fn this.setMode "reorder")}}
>
</button>
Expand Down
1 change: 1 addition & 0 deletions packages/form-builder/addon/components/cfb-slug-input.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</span>
{{/if}}
<input
...attributes
anehx marked this conversation as resolved.
Show resolved Hide resolved
id={{@inputId}}
name={{or @inputName @name}}
type="text"
Expand Down
7 changes: 7 additions & 0 deletions packages/form-builder/addon/components/cfb-slug-input.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { action } from "@ember/object";
import { scheduleOnce } from "@ember/runloop";
import { inject as service } from "@ember/service";
import { htmlSafe } from "@ember/template";
import Component from "@glimmer/component";
Expand Down Expand Up @@ -30,6 +31,12 @@ export default class CfbSlugInputComponent extends Component {

@action
calculatePadding(element) {
// Ensures that the element is already visible in combined use
// with modal dialogs.
scheduleOnce("afterRender", this, "_calculatePadding", element);
}

_calculatePadding(element) {
anehx marked this conversation as resolved.
Show resolved Hide resolved
const prefixWidth = element.clientWidth;
const prefixMargin = window.getComputedStyle(element).marginLeft;

Expand Down
8 changes: 8 additions & 0 deletions packages/form-builder/addon/gql/mutations/copy-form.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mutation CopyForm($input: CopyFormInput!) {
copyForm(input: $input) {
form {
id
}
clientMutationId
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "@projectcaluma/ember-form-builder/components/cfb-form-editor/copy-modal";
39 changes: 39 additions & 0 deletions packages/form-builder/tests/acceptance/form-copy-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { visit, click, fillIn, currentURL } from "@ember/test-helpers";
import { setupMirage } from "ember-cli-mirage/test-support";
import { setupIntl } from "ember-intl/test-support";
import { module, test } from "qunit";

import { setupApplicationTest } from "dummy/tests/helpers";

module("Acceptance | form copy", function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
setupIntl(hooks);

test("can copy a form", async function (assert) {
assert.expect(5);

const form = this.server.create("form");

await visit("/");

await click(
`[data-test-form-list-item=${form.slug}] [data-test-edit-form]`,
);

assert.dom("[data-test-copy-form-modal] form").isNotVisible();

await click(`[data-test-copy-form-button=${form.slug}]`);

assert.dom("[data-test-copy-form-modal] form").isVisible();
assert.dom("[data-test-copy-modal-input-name]").hasValue(form.name);
assert.dom("[data-test-copy-modal-input-slug]").hasValue(form.slug);

await fillIn("[data-test-copy-modal-input-name]", `${form.name} copy`);
await fillIn("[data-test-copy-modal-input-slug]", `${form.slug}-copy`);

await click("[data-test-copy-form-submit]");

assert.strictEqual(currentURL(), `/${form.slug}-copy`);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render, click, fillIn, blur } from "@ember/test-helpers";
import { hbs } from "ember-cli-htmlbars";
import { setupMirage } from "ember-cli-mirage/test-support";
import { setupIntl } from "ember-intl/test-support";
import { setupIntl, t } from "ember-intl/test-support";
import { module, test } from "qunit";

import { setupRenderingTest } from "dummy/tests/helpers";
Expand All @@ -10,7 +10,7 @@ import graphqlError from "dummy/tests/helpers/graphql-error";
module("Integration | Component | cfb-form-editor/general", function (hooks) {
setupRenderingTest(hooks);
setupMirage(hooks);
setupIntl(hooks);
setupIntl(hooks, "en");

test("it renders", async function (assert) {
assert.expect(4);
Expand Down Expand Up @@ -181,7 +181,7 @@ module("Integration | Component | cfb-form-editor/general", function (hooks) {

assert
.dom("p")
.hasText('t:caluma.form-builder.form.not-found:("slug":"test-slug")');
.hasText(t("caluma.form-builder.form.not-found", { slug: "test-slug" }));
});

test("it validates the slug", async function (assert) {
Expand All @@ -197,7 +197,7 @@ module("Integration | Component | cfb-form-editor/general", function (hooks) {

assert
.dom("small.uk-text-danger")
.hasText("t:caluma.form-builder.validations.form.slug:()");
.hasText(t("caluma.form-builder.validations.form.slug"));

await fillIn("input[name=slug]", "valid-slug");

Expand All @@ -207,6 +207,6 @@ module("Integration | Component | cfb-form-editor/general", function (hooks) {

assert
.dom("small.uk-text-danger")
.hasText("t:caluma.form-builder.validations.form.slug:()");
.hasText(t("caluma.form-builder.validations.form.slug"));
});
});
10 changes: 10 additions & 0 deletions packages/form-builder/translations/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ caluma:
success: "Ihre Frage wurde erfolgreich gespeichert!"
error: "Hoppla, beim Speichern der Frage ist etwas schief gelaufen..."

copy-modal:
title: '"{form}" kopieren'
name:
label: "Name"
hint: "Original: {name}"
slug:
label: "Slug"
hint: "Original: {slug}"
submit: "Kopieren"

validations:
form:
slug: "Ein Formular mit diesem Slug existiert bereits"
Expand Down
10 changes: 10 additions & 0 deletions packages/form-builder/translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ caluma:
success: "Your question was successfully saved!"
error: "Ooops, something went wrong while saving the question..."

copy-modal:
title: 'Copy "{form}"'
name:
label: "Name"
hint: "Original: {name}"
slug:
label: "Slug"
hint: "Original: {slug}"
submit: "Copy"

anehx marked this conversation as resolved.
Show resolved Hide resolved
validations:
form:
slug: "A form with this slug already exists"
Expand Down
10 changes: 10 additions & 0 deletions packages/form-builder/translations/fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ caluma:
success: "Votre question a été enregistrée avec succès !"
error: "Oups, quelque chose s'est mal passé lors de l'enregistrement de la question..."

copy-modal:
title: 'Copier "{form}"'
name:
label: "Nom"
hint: "Original : {name}"
slug:
label: "Slug"
hint: "Original : {slug}"
submit: "Copier"

validations:
form:
slug: "Un formulaire avec ce slug existe déjà"
Expand Down
9 changes: 9 additions & 0 deletions packages/testing/addon/mirage-graphql/mocks/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ export default class FormMock extends BaseMock {
},
});
}

@register("CopyFormPayload")
handleCopyForm(_, { input }) {
return this.handleSavePayload.fn.call(this, _, {
input: {
slug: input.slug,
},
});
}
}
Loading