Skip to content

Commit 0360718

Browse files
committed
fix(workflow): disable work item button for incorrect work item status
1 parent e7d1d1c commit 0360718

File tree

5 files changed

+80
-9
lines changed

5 files changed

+80
-9
lines changed

Diff for: packages/-ember-caluma/mirage/scenarios/default.js

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export default function (server) {
200200
workflowId: server.create("workflow").id,
201201
workItemIds: [
202202
server.create("work-item", {
203+
status: "READY",
203204
taskId: server.create("task", {
204205
type: "COMPLETE_WORKFLOW_FORM",
205206
}).id,

Diff for: packages/workflow/addon/components/work-item-button.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<UkButton
22
...attributes
33
@type={{@type}}
4-
@disabled={{or @disabled @loading this.mutate.isRunning}}
4+
@disabled={{this.disabled}}
55
@active={{@active}}
66
@loading={{or @loading this.mutate.isRunning}}
77
@color={{or @color "default"}}

Diff for: packages/workflow/addon/components/work-item-button.js

+32
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { inject as service } from "@ember/service";
22
import Component from "@glimmer/component";
33
import { queryManager } from "ember-apollo-client";
44
import { dropTask } from "ember-concurrency";
5+
import { trackedFunction } from "reactiveweb/function";
56

67
import cancelWorkItem from "@projectcaluma/ember-workflow/gql/mutations/cancel-work-item.graphql";
78
import completeWorkItem from "@projectcaluma/ember-workflow/gql/mutations/complete-work-item.graphql";
89
import skipWorkItem from "@projectcaluma/ember-workflow/gql/mutations/skip-work-item.graphql";
10+
import workItemStatusQuery from "@projectcaluma/ember-workflow/gql/queries/work-item-status.graphql";
911

1012
/**
1113
* Component to render a UkButton which mutates the given work item.
@@ -36,6 +38,36 @@ export default class WorkItemButtonComponent extends Component {
3638
completeWorkItemMutation = completeWorkItem;
3739
skipWorkItemMutation = skipWorkItem;
3840

41+
get disabled() {
42+
return (
43+
this.args.disabled ||
44+
this.args.loading ||
45+
this.mutate.isRunning ||
46+
!this.#workItemHasRequiredStatus.value
47+
);
48+
}
49+
50+
get requiredWorkItemStatus() {
51+
if (this.args.mutation === "cancel") {
52+
return ["READY", "SUSPENDED"];
53+
}
54+
55+
return ["READY"];
56+
}
57+
58+
#workItemHasRequiredStatus = trackedFunction(this, async () => {
59+
const status = await this.apollo.query(
60+
{
61+
query: workItemStatusQuery,
62+
variables: { id: this.args.workItemId },
63+
fetchPolicy: "network-only",
64+
},
65+
"allWorkItems.edges.0.node.status",
66+
);
67+
68+
return this.requiredWorkItemStatus.includes(status);
69+
});
70+
3971
@dropTask
4072
*mutate() {
4173
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
query WorkItemStatus($id: ID!) {
2+
allWorkItems(filter: [{ id: $id }], first: 1) {
3+
edges {
4+
node {
5+
id
6+
status
7+
}
8+
}
9+
}
10+
}

Diff for: packages/workflow/tests/integration/components/work-item-button-test.js

+36-8
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,34 @@ module("Integration | Component | work-item-button", function (hooks) {
99
setupRenderingTest(hooks);
1010
setupMirage(hooks);
1111

12+
hooks.beforeEach(function () {
13+
this.workItem = this.server.create("workItem", { status: "READY" });
14+
this.id = this.workItem.id;
15+
});
16+
1217
test("it renders default", async function (assert) {
1318
await render(
14-
hbs`<WorkItemButton @mutation="complete" @workItemId="test" />`,
19+
hbs`<WorkItemButton @mutation="complete" @workItemId={{this.id}} />`,
1520
);
1621

1722
assert.dom("button").hasText("Complete");
1823
});
1924

2025
test("it renders label", async function (assert) {
2126
await render(
22-
hbs`<WorkItemButton @mutation="complete" @workItemId="test" @label="Lorem Ipsum" />`,
27+
hbs`<WorkItemButton
28+
@mutation="complete"
29+
@workItemId={{this.id}}
30+
@label="Lorem Ipsum"
31+
/>`,
2332
);
2433

2534
assert.dom("button").hasText("Lorem Ipsum");
2635
});
2736

2837
test("it renders block", async function (assert) {
2938
await render(
30-
hbs`<WorkItemButton @mutation="complete" @workItemId="test">Lorem Ipsum</WorkItemButton>`,
39+
hbs`<WorkItemButton @mutation="complete" @workItemId={{this.id}}>Lorem Ipsum</WorkItemButton>`,
3140
);
3241

3342
assert.dom("button").hasText("Lorem Ipsum");
@@ -39,6 +48,10 @@ module("Integration | Component | work-item-button", function (hooks) {
3948
let mutation = "complete";
4049
this.set("mutation", mutation);
4150

51+
await render(
52+
hbs`<WorkItemButton @mutation={{this.mutation}} @workItemId={{this.id}} />`,
53+
);
54+
4255
this.server.post(
4356
"graphql",
4457
(_, request) => {
@@ -53,10 +66,6 @@ module("Integration | Component | work-item-button", function (hooks) {
5366
200,
5467
);
5568

56-
await render(
57-
hbs`<WorkItemButton @mutation={{this.mutation}} @workItemId="test" />`,
58-
);
59-
6069
await click("button");
6170

6271
mutation = "skip";
@@ -79,7 +88,7 @@ module("Integration | Component | work-item-button", function (hooks) {
7988
await render(
8089
hbs`<WorkItemButton
8190
@mutation="complete"
82-
@workItemId="test"
91+
@workItemId={{this.id}}
8392
@beforeMutate={{this.beforeMutate}}
8493
/>`,
8594
);
@@ -88,4 +97,23 @@ module("Integration | Component | work-item-button", function (hooks) {
8897

8998
assert.verifySteps(["beforeMutate"]);
9099
});
100+
101+
test("it renders as disabled if the required work item status is not given", async function (assert) {
102+
assert.expect(2);
103+
104+
await render(
105+
hbs`<WorkItemButton @mutation="complete" @workItemId={{this.id}} />`,
106+
);
107+
108+
assert.dom("button").isNotDisabled();
109+
110+
this.workItem.status = "COMPLETED";
111+
this.workItem.save();
112+
113+
await render(
114+
hbs`<WorkItemButton @mutation="complete" @workItemId={{this.id}} />`,
115+
);
116+
117+
assert.dom("button").isDisabled();
118+
});
91119
});

0 commit comments

Comments
 (0)