-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtable-controller.js
92 lines (80 loc) · 2.38 KB
/
table-controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Controller from "@ember/controller";
import { action } from "@ember/object";
import { inject as service } from "@ember/service";
import { tracked } from "@glimmer/tracking";
import {
lastValue,
enqueueTask,
restartableTask,
timeout,
} from "ember-concurrency";
import { TrackedObject } from "tracked-built-ins";
import { stringFromArray } from "mysagw/utils/query-params";
export default class TableController extends Controller {
@service filteredForms;
queryParams = ["order", "filter"];
// Filters
_filters = {
documentNumber: "",
identities: "",
answerSearch: "",
forms: "",
expertAssociations: "",
distributionPlan: "",
sections: "",
};
@tracked filters = new TrackedObject(this._filters);
@tracked invertedFilters = new TrackedObject(this._filters);
@tracked order = "-CREATED_AT";
@tracked filter;
serializeFilter() {
this.filter = btoa(
JSON.stringify({ filters: this.filters, inverts: this.invertedFilters }),
);
}
@action
deserializeFilter() {
if (!this.filter) {
return;
}
const { filters, inverts } = JSON.parse(atob(this.filter));
this.filters = new TrackedObject(filters);
this.invertedFilters = new TrackedObject(inverts);
}
@restartableTask
*updateFilter(type, eventOrValue) {
if (["documentNumber", "answerSearch"].includes(type)) {
// debounce only input filters by 500ms to prevent too many requests when
// typing into a search field
yield timeout(500);
}
// Update the filter with the passed value. This can either be an array of
// objects (multiple choice filters), and event or a plain value
if (Array.isArray(eventOrValue)) {
this.filters[type] = stringFromArray(
eventOrValue,
type === "identities" ? "id" : "value",
);
} else {
this.filters[type] = eventOrValue.target?.value ?? eventOrValue;
}
this.serializeFilter();
}
@enqueueTask
*invertFilter(type) {
this.invertedFilters[type] = !this.invertedFilters[type];
this.serializeFilter();
yield timeout(300);
}
@action
resetFilters() {
this.filters = new TrackedObject(this._filters);
this.invertedFilters = new TrackedObject(this._filters);
this.serializeFilter();
}
@lastValue("fetchForms") forms;
@restartableTask
*fetchForms() {
return (yield this.filteredForms.fetch()).map((form) => form.slug);
}
}