-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds edit and delete functionality to userlist (#399)
* feat: only proceed confirmed deletions in EditForm * feat: add edit and delete actions to userlist * test: add coverage for confirm-task decorator * chore: add missing translations * test: fix tests where confirm-task decorator in passive use
- Loading branch information
1 parent
b513c73
commit 69d3c17
Showing
12 changed files
with
224 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { assert } from "@ember/debug"; | ||
import UIkit from "uikit"; | ||
|
||
import ENV from "ember-emeis/config/environment"; | ||
|
||
/** | ||
* This decorator makes heavy use of the UIKit modal. If anything breaks in future releases, it could be | ||
* caused by the use of non-public API in this decorator. | ||
* | ||
* For further details have a look at: https://github.com/uikit/uikit/blob/develop/src/js/core/modal.js | ||
*/ | ||
|
||
const LABELS = ["message", "ok", "cancel"]; | ||
|
||
async function confirm(text, options) { | ||
try { | ||
await UIkit.modal.confirm(text, options); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
function initOptions(args) { | ||
if (args.length === 1) { | ||
assert( | ||
"You should pass the confirm-task options as an object looking like this: { message: 'emeis.form.deleteMessage', cancel: 'emeis.form.back' } ", | ||
typeof args[0] === "object" | ||
); | ||
return args[0]; | ||
} | ||
return {}; | ||
} | ||
|
||
function validateIntl(context) { | ||
assert( | ||
"Inject the `intl` service into your component to properly translate the modal dialog.", | ||
context.intl | ||
); | ||
} | ||
|
||
function translateOptions(context, options) { | ||
const translated = {}; | ||
for (const key in options) { | ||
if (LABELS.includes(key) && context.intl.exists(options[key])) { | ||
translated[key] = context.intl.t(options[key]); | ||
} | ||
} | ||
return { | ||
labels: { | ||
// set some defaults | ||
message: context.intl.t("emeis.form.confirmText"), | ||
ok: context.intl.t("emeis.form.ok"), | ||
cancel: context.intl.t("emeis.form.cancel"), | ||
// add the override with the transmitted options | ||
...translated, | ||
}, | ||
}; | ||
} | ||
|
||
function filterOptions(options) { | ||
const filteredOptions = Object.fromEntries( | ||
Object.entries(options).filter(([key]) => !LABELS.includes(key)) | ||
); | ||
|
||
if (ENV.environment === "test") { | ||
filteredOptions.container = "#ember-testing"; | ||
} | ||
return filteredOptions; | ||
} | ||
|
||
// make sure that decorator can be called with or without arguments | ||
const makeFlexibleDecorator = (decorateFn, args) => { | ||
if (args.length === 3 && !args[0].message) { | ||
// We can assume that the decorator was called without options | ||
return decorateFn(...args); | ||
} | ||
|
||
return decorateFn; | ||
}; | ||
|
||
export function confirmTask(...decoratorArgs) { | ||
const options = initOptions(decoratorArgs); | ||
|
||
function decorate(target, property, desc) { | ||
const gen = desc.value; | ||
|
||
desc.value = function* (...args) { | ||
const event = args.find((arg) => arg instanceof Event); | ||
|
||
if (event) { | ||
event.preventDefault(); | ||
} | ||
|
||
validateIntl(this); | ||
const translatedOptions = translateOptions(this, options); | ||
// append other options which are not confirm-labels to the modal object | ||
// that way you can modify the modal with further options like "container" | ||
const filteredOptions = filterOptions(options); | ||
if ( | ||
!(yield confirm(translatedOptions.labels.message, { | ||
...translatedOptions, | ||
...filteredOptions, | ||
})) | ||
) { | ||
return; | ||
} | ||
return yield* gen.apply(this, args); | ||
}; | ||
} | ||
return makeFlexibleDecorator(decorate, decoratorArgs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { click, waitFor } from "@ember/test-helpers"; | ||
import { task } from "ember-concurrency"; | ||
import { setupTest } from "ember-qunit"; | ||
import { module, test } from "qunit"; | ||
|
||
import { confirmTask } from "ember-emeis/decorators/confirm-task"; | ||
|
||
const DUMMY_ARGUMENT = { test: "ok" }; | ||
|
||
module("Unit | decorators | confirm-task", function (hooks) { | ||
setupTest(hooks); | ||
|
||
hooks.beforeEach(function (assert) { | ||
this.TestStub = class TestStub { | ||
intl = { | ||
t: (s) => { | ||
return s; | ||
}, | ||
}; | ||
|
||
@task | ||
@confirmTask({ container: "#ember-testing" }) | ||
*deleteSomething(someArgument) { | ||
assert.step("delete"); | ||
return yield someArgument; | ||
} | ||
}; | ||
}); | ||
|
||
test("it triggers the action on confirm", async function (assert) { | ||
assert.expect(3); | ||
const instance = new this.TestStub(); | ||
|
||
const result = instance.deleteSomething.perform(DUMMY_ARGUMENT); | ||
|
||
await waitFor(".uk-modal.uk-open"); | ||
await click(".uk-button-primary"); | ||
|
||
assert.deepEqual(result.args[0], DUMMY_ARGUMENT); | ||
assert.verifySteps(["delete"]); | ||
}); | ||
|
||
test("it does not trigger the action on cancel", async function (assert) { | ||
assert.expect(1); | ||
const instance = new this.TestStub(); | ||
instance.deleteSomething.perform(); | ||
|
||
await waitFor(".uk-modal.uk-open"); | ||
await click(".uk-modal-close"); | ||
|
||
assert.verifySteps([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters