Skip to content

Commit

Permalink
fix(workflow): disable work item button for incorrect work item status
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Jan 24, 2025
1 parent e7d1d1c commit d7e0fb1
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/-ember-caluma/mirage/scenarios/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export default function (server) {
workflowId: server.create("workflow").id,
workItemIds: [
server.create("work-item", {
status: "READY",
taskId: server.create("task", {
type: "COMPLETE_WORKFLOW_FORM",
}).id,
Expand Down
2 changes: 1 addition & 1 deletion packages/workflow/addon/components/work-item-button.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<UkButton
...attributes
@type={{@type}}
@disabled={{or @disabled @loading this.mutate.isRunning}}
@disabled={{this.disabled}}
@active={{@active}}
@loading={{or @loading this.mutate.isRunning}}
@color={{or @color "default"}}
Expand Down
32 changes: 32 additions & 0 deletions packages/workflow/addon/components/work-item-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { inject as service } from "@ember/service";
import Component from "@glimmer/component";
import { queryManager } from "ember-apollo-client";
import { dropTask } from "ember-concurrency";
import { trackedFunction } from "reactiveweb/function";

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

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

get disabled() {
return (
this.args.disabled ||
this.args.loading ||
this.mutate.isRunning ||
!this.#workItemHasRequiredStatus.value
);
}

get requiredWorkItemStatus() {
if (this.args.mutation === "cancel") {
return ["READY", "SUSPENDED"];
}

return ["READY"];
}

#workItemHasRequiredStatus = trackedFunction(this, async () => {
const status = await this.apollo.query(
{
query: workItemStatusQuery,
variables: { id: this.args.workItemId },
fetchPolicy: "network-only",
},
"allWorkItems.edges.0.node.status",
);

return this.requiredWorkItemStatus.includes(status);
});

@dropTask
*mutate() {
try {
Expand Down
10 changes: 10 additions & 0 deletions packages/workflow/addon/gql/queries/work-item-status.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
query WorkItemStatus($id: ID!) {
allWorkItems(filter: [{ id: $id }], first: 1) {
edges {
node {
id
status
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,30 @@ module("Integration | Component | work-item-button", function (hooks) {
setupRenderingTest(hooks);
setupMirage(hooks);

hooks.beforeEach(function () {
this.workItem = this.server.create("workItem", { status: "READY" });
this.id = this.workItem.id;
});

test("it renders default", async function (assert) {
await render(
hbs`<WorkItemButton @mutation="complete" @workItemId="test" />`,
hbs`<WorkItemButton @mutation="complete" @workItemId={{this.id}} />`,
);

assert.dom("button").hasText("Complete");
});

test("it renders label", async function (assert) {
await render(
hbs`<WorkItemButton @mutation="complete" @workItemId="test" @label="Lorem Ipsum" />`,
hbs`<WorkItemButton @mutation="complete" @workItemId={{this.id}} @label="Lorem Ipsum" />`,
);

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

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

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

await render(
hbs`<WorkItemButton @mutation={{this.mutation}} @workItemId={{this.id}} />`,
);

this.server.post(
"graphql",
(_, request) => {
Expand All @@ -53,10 +62,6 @@ module("Integration | Component | work-item-button", function (hooks) {
200,
);

await render(
hbs`<WorkItemButton @mutation={{this.mutation}} @workItemId="test" />`,
);

await click("button");

mutation = "skip";
Expand All @@ -79,7 +84,7 @@ module("Integration | Component | work-item-button", function (hooks) {
await render(
hbs`<WorkItemButton
@mutation="complete"
@workItemId="test"
@workItemId={{this.id}}
@beforeMutate={{this.beforeMutate}}
/>`,
);
Expand All @@ -88,4 +93,23 @@ module("Integration | Component | work-item-button", function (hooks) {

assert.verifySteps(["beforeMutate"]);
});

test("it renders as disabled if the required work item status is not given", async function (assert) {
assert.expect(2);

await render(
hbs`<WorkItemButton @mutation="complete" @workItemId={{this.id}} />`,
);

assert.dom("button").isNotDisabled();

this.workItem.status = "COMPLETED";
this.workItem.save();

await render(
hbs`<WorkItemButton @mutation="complete" @workItemId={{this.id}} />`,
);

assert.dom("button").isDisabled();
});
});

0 comments on commit d7e0fb1

Please sign in to comment.