Skip to content

Commit

Permalink
refactor: use ember-resources for ember-caluma fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Yelinz committed Mar 25, 2022
1 parent 3577e08 commit 88a37a5
Show file tree
Hide file tree
Showing 40 changed files with 616 additions and 444 deletions.
3 changes: 3 additions & 0 deletions ember/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
/.node_modules.ember-try/
/bower.json.ember-try
/package.json.ember-try

# mirage
/mirage/mirage
21 changes: 21 additions & 0 deletions ember/app/caluma-query/models/case.js
Original file line number Diff line number Diff line change
@@ -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
}
}
}`;
}
34 changes: 3 additions & 31 deletions ember/app/cases/detail/controller.js
Original file line number Diff line number Diff line change
@@ -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];
}
}
7 changes: 7 additions & 0 deletions ember/app/cases/detail/edit/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Controller from "@ember/controller";

export default class CasesDetailEditController extends Controller {
get case() {
return this.model.value[0];
}
}
6 changes: 5 additions & 1 deletion ember/app/cases/detail/edit/route.js
Original file line number Diff line number Diff line change
@@ -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");
}
}
2 changes: 1 addition & 1 deletion ember/app/cases/detail/edit/template.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{#if @model}}
<CfContent @documentId={{@model.document.id}} />
<CfContent @documentId={{this.case.document.id}} />
{{/if}}
11 changes: 8 additions & 3 deletions ember/app/cases/detail/index/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
19 changes: 2 additions & 17 deletions ember/app/cases/detail/index/route.js
Original file line number Diff line number Diff line change
@@ -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");
}
}
38 changes: 19 additions & 19 deletions ember/app/cases/detail/index/template.hbs
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
<div class="uk-grid-collapse" uk-grid>
<div class="uk-width-1-3">
<span class="uk-text-large">
{{t "documents.general"}}
{{t "cases.general"}}
</span>
</div>
<div class="uk-width-2-3">
<table class="uk-table uk-table-justify uk-table-small">
<tbody>
<tr>
<th class="uk-text-bold uk-width-1-6">
{{t "documents.status"}}
{{t "cases.status"}}
</th>
<td class="uk-width-1-3">
<CaseStateLabel @state={{@model.status}} @iconPosition="right" />
<CaseStateLabel @state={{this.case.raw.status}} @iconPosition="right" />
</td>
<th class="uk-text-bold uk-width-1-6">
{{t "documents.description"}}
{{t "cases.description"}}
</th>
<td class="uk-width-1-3">
{{or @model.form.description "-"}}
{{or this.case.form.description "-"}}
</td>
</tr>
<tr>
<th class="uk-text-bold uk-width-1-6">
{{t "documents.createdAt"}}
{{t "cases.createdAt"}}
</th>
<td class="uk-width-1-3">
{{
if
@model.createdAt
(moment-format @model.createdAt "DD.MM.YYYY")
this.case.createdAt
(moment-format this.case.createdAt "DD.MM.YYYY")
"-"
}}
</td>
<th class="uk-text-bold uk-width-1-6">
{{t "documents.modifiedAt"}}
{{t "cases.modifiedAt"}}
</th>
<td class="uk-width-1-3">
{{
if
@model.modifiedAt
(moment-format @model.modifiedAt "DD.MM.YYYY")
this.case.modifiedAt
(moment-format this.case.modifiedAt "DD.MM.YYYY")
"-"
}}
</td>
Expand All @@ -55,32 +55,32 @@
<div class="uk-grid-collapse" uk-grid>
<div class="uk-width-1-3">
<span class="uk-text-large">
{{t "documents.actions"}}
{{t "cases.actions"}}
</span>
</div>
<div class="uk-width-2-3">
<h3>
{{t "documents.edit"}}
{{t "cases.edit"}}
</h3>
<p>
{{t "documents.editText"}}
{{t "cases.editText"}}
</p>
<LinkTo @route="cases.detail.edit" class="uk-button uk-button-default">
{{t "documents.edit"}}
{{t "cases.edit"}}
</LinkTo>

<h3>
{{t "documents.delete"}}
{{t "cases.delete"}}
</h3>
<p>
{{t "documents.deleteText"}}
{{t "cases.deleteText"}}
</p>
<UkButton
@onClick={{perform this.closeCase @model}}
@onClick={{perform this.closeCase this.case}}
@disabled={{this.closeCase.isRunning}}
@loading={{this.closeCase.isRunning}}
>
{{t "documents.delete"}}
{{t "cases.delete"}}
</UkButton>
</div>
</div>
Expand Down
12 changes: 9 additions & 3 deletions ember/app/cases/detail/route.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 4 additions & 4 deletions ember/app/cases/detail/template.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#if this.fetchCase.isRunning}}
{{#if @model.isLoading}}
<div class="uk-text-center uk-padding">
<UkSpinner @ratio={{2}} />
</div>
Expand All @@ -14,13 +14,13 @@

<UkTab as |tab|>
<tab.item @href="/{{this.case.id}}" @linkToIndex={{true}}>
{{t "documents.subnav.overview"}}
{{t "cases.subnav.overview"}}
</tab.item>
<tab.item @href="/{{this.case.id}}/work-items">
{{t "documents.subnav.workItems"}}
{{t "cases.subnav.workItems"}}
</tab.item>
<tab.item @href="/{{this.case.id}}/edit">
{{t "documents.subnav.document"}}
{{t "cases.subnav.document"}}
</tab.item>
</UkTab>

Expand Down
30 changes: 3 additions & 27 deletions ember/app/cases/detail/work-items/edit/form/controller.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
2 changes: 1 addition & 1 deletion ember/app/cases/detail/work-items/edit/form/template.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h3>{{this.workItem.name}}</h3>

{{#if this.fetchWorkItems.isRunning}}
{{#if @model.isLoading}}
<div class="uk-text-center uk-padding">
<UkSpinner @ratio={{2}} />
</div>
Expand Down
Loading

0 comments on commit 88a37a5

Please sign in to comment.