From 88a37a5d730cf4ad44421c805d3f7f84a206eed1 Mon Sep 17 00:00:00 2001 From: yelinz Date: Wed, 16 Mar 2022 09:08:28 +0100 Subject: [PATCH] refactor: use ember-resources for ember-caluma fetching --- ember/.eslintignore | 3 + ember/app/caluma-query/models/case.js | 21 + ember/app/cases/detail/controller.js | 34 +- ember/app/cases/detail/edit/controller.js | 7 + ember/app/cases/detail/edit/route.js | 6 +- ember/app/cases/detail/edit/template.hbs | 2 +- ember/app/cases/detail/index/controller.js | 11 +- ember/app/cases/detail/index/route.js | 19 +- ember/app/cases/detail/index/template.hbs | 38 +- ember/app/cases/detail/route.js | 12 +- ember/app/cases/detail/template.hbs | 8 +- .../detail/work-items/edit/form/controller.js | 30 +- .../detail/work-items/edit/form/template.hbs | 2 +- .../work-items/edit/index/controller.js | 31 +- .../detail/work-items/edit/index/route.js | 5 - .../detail/work-items/edit/index/template.hbs | 6 +- .../app/cases/detail/work-items/edit/route.js | 9 +- .../detail/work-items/index/controller.js | 50 ++- .../cases/detail/work-items/index/route.js | 25 +- ember/app/cases/index/template.hbs | 4 +- ember/app/cases/new/template.hbs | 4 +- .../components/case-state-label/template.hbs | 4 +- ember/app/components/cases-table/component.js | 67 +-- ember/app/components/cases-table/template.hbs | 25 +- .../app/components/dynamic-table/template.hbs | 2 +- ember/app/components/nav-bar/template.hbs | 2 +- ember/app/gql/queries/get-case.graphql | 21 - ember/app/gql/queries/get-cases.graphql | 33 -- .../register-caluma-query-model-work-item.js | 2 + ember/app/router.js | 2 +- ember/app/work-items/controller.js | 51 ++- ember/app/work-items/route.js | 7 +- ember/app/work-items/template.hbs | 2 +- ember/config/environment.js | 28 +- ember/mirage/config.js | 15 + ember/package.json | 19 +- ember/tests/acceptance/cases-test.js | 33 ++ ember/tests/acceptance/work-items-test.js | 24 ++ ember/translations/en.yaml | 10 +- ember/yarn.lock | 386 +++++++++++++++--- 40 files changed, 616 insertions(+), 444 deletions(-) create mode 100644 ember/app/caluma-query/models/case.js create mode 100644 ember/app/cases/detail/edit/controller.js delete mode 100644 ember/app/gql/queries/get-case.graphql delete mode 100644 ember/app/gql/queries/get-cases.graphql create mode 100644 ember/mirage/config.js create mode 100644 ember/tests/acceptance/cases-test.js create mode 100644 ember/tests/acceptance/work-items-test.js diff --git a/ember/.eslintignore b/ember/.eslintignore index 72df3730..e6ffd1d0 100644 --- a/ember/.eslintignore +++ b/ember/.eslintignore @@ -18,3 +18,6 @@ /.node_modules.ember-try/ /bower.json.ember-try /package.json.ember-try + +# mirage +/mirage/mirage diff --git a/ember/app/caluma-query/models/case.js b/ember/app/caluma-query/models/case.js new file mode 100644 index 00000000..50582536 --- /dev/null +++ b/ember/app/caluma-query/models/case.js @@ -0,0 +1,21 @@ +import CaseModel from "@projectcaluma/ember-core/caluma-query/models/work-item"; + +export default class CustomCaseModel extends CaseModel { + static fragment = `{ + createdAt + modifiedAt + createdByUser + createdByGroup + closedAt + closedByUser + closedByGroup + status + meta + document { + id + form { + name + } + } + }`; +} diff --git a/ember/app/cases/detail/controller.js b/ember/app/cases/detail/controller.js index e9a42db9..858d5424 100644 --- a/ember/app/cases/detail/controller.js +++ b/ember/app/cases/detail/controller.js @@ -1,35 +1,7 @@ import Controller from "@ember/controller"; -import getCaseQuery from "caluma-portal-demo/gql/queries/get-case"; -import { queryManager } from "ember-apollo-client"; -import { task, lastValue } from "ember-concurrency"; -import QueryParams from "ember-parachute"; -export default class CasesDetailController extends Controller.extend( - new QueryParams().Mixin -) { - @queryManager apollo; - - setup() { - this.fetchCase.perform(); - } - reset() { - this.fetchCase.cancelAll(); - } - - @lastValue("fetchCase") case; - @task - *fetchCase() { - if (typeof this.model === "object" && this.model) { - return this.model; - } - - const caseNode = yield this.apollo.query( - { - query: getCaseQuery, - variables: { caseId: this.model }, - }, - "allCases.edges" - ); - return caseNode.map(({ node }) => node)[0]; +export default class CasesDetailController extends Controller { + get case() { + return this.model.value[0]; } } diff --git a/ember/app/cases/detail/edit/controller.js b/ember/app/cases/detail/edit/controller.js new file mode 100644 index 00000000..07cdd77c --- /dev/null +++ b/ember/app/cases/detail/edit/controller.js @@ -0,0 +1,7 @@ +import Controller from "@ember/controller"; + +export default class CasesDetailEditController extends Controller { + get case() { + return this.model.value[0]; + } +} diff --git a/ember/app/cases/detail/edit/route.js b/ember/app/cases/detail/edit/route.js index a9e7b1e4..f0b4dcae 100644 --- a/ember/app/cases/detail/edit/route.js +++ b/ember/app/cases/detail/edit/route.js @@ -1,3 +1,7 @@ import CasesDetailRoute from "caluma-portal-demo/cases/detail/index/route"; -export default class CasesDetailEditRoute extends CasesDetailRoute {} +export default class CasesDetailEditRoute extends CasesDetailRoute { + model() { + return this.modelFor("cases.detail"); + } +} diff --git a/ember/app/cases/detail/edit/template.hbs b/ember/app/cases/detail/edit/template.hbs index 5cf0b7fe..f85b8b2a 100644 --- a/ember/app/cases/detail/edit/template.hbs +++ b/ember/app/cases/detail/edit/template.hbs @@ -1,3 +1,3 @@ {{#if @model}} - + {{/if}} diff --git a/ember/app/cases/detail/index/controller.js b/ember/app/cases/detail/index/controller.js index a8e586a2..baac1b9e 100644 --- a/ember/app/cases/detail/index/controller.js +++ b/ember/app/cases/detail/index/controller.js @@ -2,16 +2,21 @@ import Controller from "@ember/controller"; import { inject as service } from "@ember/service"; import cancelCaseMutation from "caluma-portal-demo/gql/mutations/cancel-case"; import { queryManager } from "ember-apollo-client"; -import { task } from "ember-concurrency"; +import { dropTask } from "ember-concurrency"; export default class CasesDetailIndexController extends Controller { @service router; @queryManager apollo; - @task *closeCase() { + get case() { + return this.model.value[0]; + } + + @dropTask + *closeCase() { yield this.apollo.mutate({ mutation: cancelCaseMutation, - variables: { case: this.model.id }, + variables: { case: this.case.id }, }); this.router.transitionTo("cases.index"); } diff --git a/ember/app/cases/detail/index/route.js b/ember/app/cases/detail/index/route.js index 11f00870..c2de39b1 100644 --- a/ember/app/cases/detail/index/route.js +++ b/ember/app/cases/detail/index/route.js @@ -1,22 +1,7 @@ import Route from "@ember/routing/route"; -import getCaseQuery from "caluma-portal-demo/gql/queries/get-case"; -import { queryManager } from "ember-apollo-client"; export default class CasesDetailRoute extends Route { - @queryManager apollo; - async model() { - const model = this.modelFor("cases.detail"); - if (typeof model === "object" && model) { - return model; - } - - const caseRecord = await this.apollo.query( - { - query: getCaseQuery, - variables: { caseId: model }, - }, - "allCases.edges" - ); - return caseRecord.map(({ node }) => node)[0]; + model() { + return this.modelFor("cases.detail"); } } diff --git a/ember/app/cases/detail/index/template.hbs b/ember/app/cases/detail/index/template.hbs index 0a77e036..ff31b515 100644 --- a/ember/app/cases/detail/index/template.hbs +++ b/ember/app/cases/detail/index/template.hbs @@ -1,7 +1,7 @@
- {{t "documents.general"}} + {{t "cases.general"}}
@@ -9,38 +9,38 @@ - {{t "documents.status"}} + {{t "cases.status"}} - + - {{t "documents.description"}} + {{t "cases.description"}} - {{or @model.form.description "-"}} + {{or this.case.form.description "-"}} - {{t "documents.createdAt"}} + {{t "cases.createdAt"}} {{ if - @model.createdAt - (moment-format @model.createdAt "DD.MM.YYYY") + this.case.createdAt + (moment-format this.case.createdAt "DD.MM.YYYY") "-" }} - {{t "documents.modifiedAt"}} + {{t "cases.modifiedAt"}} {{ if - @model.modifiedAt - (moment-format @model.modifiedAt "DD.MM.YYYY") + this.case.modifiedAt + (moment-format this.case.modifiedAt "DD.MM.YYYY") "-" }} @@ -55,32 +55,32 @@
- {{t "documents.actions"}} + {{t "cases.actions"}}

- {{t "documents.edit"}} + {{t "cases.edit"}}

- {{t "documents.editText"}} + {{t "cases.editText"}}

- {{t "documents.edit"}} + {{t "cases.edit"}}

- {{t "documents.delete"}} + {{t "cases.delete"}}

- {{t "documents.deleteText"}} + {{t "cases.deleteText"}}

- {{t "documents.delete"}} + {{t "cases.delete"}}
diff --git a/ember/app/cases/detail/route.js b/ember/app/cases/detail/route.js index 79c234aa..dd47b3d0 100644 --- a/ember/app/cases/detail/route.js +++ b/ember/app/cases/detail/route.js @@ -1,7 +1,13 @@ import Route from "@ember/routing/route"; - +import { useCalumaQuery } from "@projectcaluma/ember-core/caluma-query"; +import { allCases } from "@projectcaluma/ember-core/caluma-query/queries"; export default class CasesDetailRoute extends Route { - model(params) { - return params.id; + model({ case_id }) { + const caseQuery = useCalumaQuery(this, allCases, () => ({ + options: { pageSize: 1 }, + filter: [{ id: case_id }], + })); + + return caseQuery; } } diff --git a/ember/app/cases/detail/template.hbs b/ember/app/cases/detail/template.hbs index 1ab6d387..b8d30d2a 100644 --- a/ember/app/cases/detail/template.hbs +++ b/ember/app/cases/detail/template.hbs @@ -1,4 +1,4 @@ -{{#if this.fetchCase.isRunning}} +{{#if @model.isLoading}}
@@ -14,13 +14,13 @@ - {{t "documents.subnav.overview"}} + {{t "cases.subnav.overview"}} - {{t "documents.subnav.workItems"}} + {{t "cases.subnav.workItems"}} - {{t "documents.subnav.document"}} + {{t "cases.subnav.document"}} diff --git a/ember/app/cases/detail/work-items/edit/form/controller.js b/ember/app/cases/detail/work-items/edit/form/controller.js index c3737d9d..101be8ca 100644 --- a/ember/app/cases/detail/work-items/edit/form/controller.js +++ b/ember/app/cases/detail/work-items/edit/form/controller.js @@ -1,40 +1,16 @@ import Controller from "@ember/controller"; import { inject as service } from "@ember/service"; -import calumaQuery from "@projectcaluma/ember-core/caluma-query"; -import { allWorkItems } from "@projectcaluma/ember-core/caluma-query/queries"; import completeWorkItem from "caluma-portal-demo/gql/mutations/complete-work-item"; import { queryManager } from "ember-apollo-client"; -import { - dropTask, - restartableTask, - lastValue, -} from "ember-concurrency-decorators"; +import { dropTask } from "ember-concurrency-decorators"; export default class CasesDetailWorkItemsEditFormController extends Controller { @queryManager apollo; - @service store; - @service notification; - @service intl; @service router; - @calumaQuery({ query: allWorkItems }) - workItemsQuery; - - @lastValue("fetchWorkItems") workItem; - @restartableTask() - *fetchWorkItems(id) { - if (typeof id === "object") { - return id; - } - try { - yield this.workItemsQuery.fetch({ filter: [{ id }] }); - - return this.workItemsQuery.value[0]; - } catch (error) { - console.error(error); - this.notification.danger(this.intl.t("workItems.fetchError")); - } + get workItem() { + return this.model.value[0]; } @dropTask() diff --git a/ember/app/cases/detail/work-items/edit/form/template.hbs b/ember/app/cases/detail/work-items/edit/form/template.hbs index 5a860083..617b1840 100644 --- a/ember/app/cases/detail/work-items/edit/form/template.hbs +++ b/ember/app/cases/detail/work-items/edit/form/template.hbs @@ -1,6 +1,6 @@

{{this.workItem.name}}

-{{#if this.fetchWorkItems.isRunning}} +{{#if @model.isLoading}}
diff --git a/ember/app/cases/detail/work-items/edit/index/controller.js b/ember/app/cases/detail/work-items/edit/index/controller.js index 98fe7b57..1e8dd545 100644 --- a/ember/app/cases/detail/work-items/edit/index/controller.js +++ b/ember/app/cases/detail/work-items/edit/index/controller.js @@ -1,43 +1,25 @@ import Controller from "@ember/controller"; import { action } from "@ember/object"; import { inject as service } from "@ember/service"; -import calumaQuery from "@projectcaluma/ember-core/caluma-query"; -import { allWorkItems } from "@projectcaluma/ember-core/caluma-query/queries"; +import { tracked } from "@glimmer/tracking"; import completeWorkItem from "caluma-portal-demo/gql/mutations/complete-work-item"; import saveWorkItem from "caluma-portal-demo/gql/mutations/save-work-item"; import { queryManager } from "ember-apollo-client"; -import { lastValue } from "ember-concurrency"; import { dropTask } from "ember-concurrency-decorators"; import moment from "moment"; export default class CasesDetailWorkItemsEditController extends Controller { @queryManager apollo; - @service store; @service notification; @service intl; @service router; - @calumaQuery({ query: allWorkItems, options: "options" }) - workItemsQuery; - - get options() { - return { - pageSize: 1, - }; + get workItem() { + return this.model.value[0]; } - @lastValue("fetchWorkItem") workItem; - @dropTask() - *fetchWorkItem(id) { - try { - yield this.workItemsQuery.fetch({ filter: [{ id }] }); - - return this.workItemsQuery.value[0]; - } catch (error) { - this.notification.danger(this.intl.t("workItems.fetchError")); - } - } + @tracked description = this.workItem.description; @dropTask *finishWorkItem(event) { @@ -70,7 +52,6 @@ export default class CasesDetailWorkItemsEditController extends Controller { @dropTask *saveManualWorkItem(event) { event.preventDefault(); - try { yield this.apollo.mutate({ mutation: saveWorkItem, @@ -79,15 +60,15 @@ export default class CasesDetailWorkItemsEditController extends Controller { workItem: this.workItem.id, description: this.workItem.description, deadline: this.workItem.deadline, - meta: JSON.stringify(this.workItem?.meta), }, }, }); this.notification.success(this.intl.t("workItems.saveSuccess")); - this.router.transitionTo("cases.detail.work-items.index"); + // this.router.transitionTo("cases.detail.work-items.index"); } catch (error) { + console.error(error); this.notification.danger(this.intl.t("workItems.saveError")); } } diff --git a/ember/app/cases/detail/work-items/edit/index/route.js b/ember/app/cases/detail/work-items/edit/index/route.js index 46d9d991..1cfcf4f9 100644 --- a/ember/app/cases/detail/work-items/edit/index/route.js +++ b/ember/app/cases/detail/work-items/edit/index/route.js @@ -4,9 +4,4 @@ export default class CasesDetailWorkItemsEditRoute extends Route { model() { return this.modelFor("cases.detail.work-items.edit"); } - - setupController(controller, model) { - super.setupController(controller, model); - controller.fetchWorkItem.perform(model); - } } diff --git a/ember/app/cases/detail/work-items/edit/index/template.hbs b/ember/app/cases/detail/work-items/edit/index/template.hbs index ae140bbf..8a09497e 100644 --- a/ember/app/cases/detail/work-items/edit/index/template.hbs +++ b/ember/app/cases/detail/work-items/edit/index/template.hbs @@ -49,7 +49,7 @@ type="text" value={{ format-date - this.workItem.createdAt + this.workItem.raw.createdAt day="2-digit" month="2-digit" year="numeric" @@ -70,7 +70,7 @@