This repository was archived by the owner on Apr 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathcontroller.ts
More file actions
170 lines (144 loc) · 4.93 KB
/
controller.ts
File metadata and controls
170 lines (144 loc) · 4.93 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import Store from '@ember-data/store';
import Controller from '@ember/controller';
import { action, computed } from '@ember/object';
import { alias, or } from '@ember/object/computed';
import { inject as service } from '@ember/service';
import { waitFor } from '@ember/test-waiters';
import { all, restartableTask, task, timeout } from 'ember-concurrency';
import { taskFor } from 'ember-concurrency-ts';
import config from 'ember-osf-web/config/environment';
import $ from 'jquery';
import Media from 'ember-responsive';
import Node from 'ember-osf-web/models/node';
import { QueryHasManyResult } from 'ember-osf-web/models/osf-model';
import User from 'ember-osf-web/models/user';
import Analytics from 'ember-osf-web/services/analytics';
import CurrentUser from 'ember-osf-web/services/current-user';
// TODO pull these from the database
const {
dashboard: {
noteworthyNode,
popularNode,
},
} = config;
export default class Dashboard extends Controller {
@service analytics!: Analytics;
@service currentUser!: CurrentUser;
@service store!: Store;
@service media!: Media;
page = 1;
loading = false;
loadingSearch = false;
loadingMore = false;
initialLoad = true;
// Initialized in setupController.
filter!: string | null;
sort = '-last_logged';
modalOpen = false;
newNode: Node | null = null;
showNewNodeNavigation = false;
'failedLoading-noteworthy' = false;
'failedLoading-popular' = false;
// institutions: Institution[] = A([]);
nodes?: QueryHasManyResult<Node>;
noteworthy!: QueryHasManyResult<Node>;
popular!: QueryHasManyResult<Node>;
@alias('currentUser.user') user!: User;
@or('nodes.length', 'filter', 'findNodes.isRunning') hasNodes!: boolean;
@computed('nodes.{length,meta.total}')
get hasMore(): boolean {
return !!this.nodes && this.nodes.length < this.nodes.meta.total;
}
@restartableTask
@waitFor
async setupTask() {
this.set('filter', null);
// const institutions = this.store.findAll('institution');
await all([
// institutions,
taskFor(this.findNodes).perform(),
taskFor(this.getPopularAndNoteworthy).perform(popularNode, 'popular'),
taskFor(this.getPopularAndNoteworthy).perform(noteworthyNode, 'noteworthy'),
]);
// this.set('institutions', institutions.toArray());
}
@restartableTask
@waitFor
async filterNodes(filter: string) {
await timeout(500);
this.setProperties({ filter });
this.analytics.track('list', 'filter', 'Dashboard - Search projects');
await taskFor(this.findNodes).perform();
}
@restartableTask
@waitFor
async findNodes(more?: boolean) {
const indicatorProperty = more ? 'loadingMore' : 'loading';
this.set(indicatorProperty, true);
const { user } = this.currentUser;
const nodes: QueryHasManyResult<Node> = await user!.queryHasMany('sparseNodes', {
embed: ['bibliographic_contributors', 'parent', 'root'],
filter: this.filter ? { title: $('<div>').text(this.filter).html() } : undefined,
page: more ? this.incrementProperty('page') : this.set('page', 1),
sort: this.sort || undefined,
});
if (more && this.nodes) {
this.nodes.pushObjects(nodes);
} else {
this.set('nodes', nodes);
}
this.set(indicatorProperty, false);
this.set('initialLoad', false);
}
@task
@waitFor
async getPopularAndNoteworthy(id: string, dest: 'noteworthy' | 'popular') {
try {
const node = await this.store.findRecord('node', id);
const linkedNodes: QueryHasManyResult<Node> = await node.queryHasMany('linkedNodes', {
embed: 'bibliographic_contributors',
page: { size: 5 },
});
this.set(dest, linkedNodes);
} catch (e) {
const failedProperty = `failedLoading-${dest}` as 'failedLoading-noteworthy' | 'failedLoading-popular';
this.set(failedProperty, true);
}
}
@action
more() {
taskFor(this.findNodes).perform(true);
}
@action
sortProjects(sort: string) {
this.setProperties({ sort });
taskFor(this.findNodes).perform();
}
@action
openModal() {
this.set('modalOpen', true);
}
@action
closeModal() {
this.setProperties({
modalOpen: false,
newNode: null,
showNewNodeNavigation: false,
});
}
@action
afterStay() {
taskFor(this.findNodes).perform();
}
@task
async projectCreated(newNode: Node) {
this.set('modalOpen', false);
await timeout(1);
this.set('newNode', newNode);
this.set('showNewNodeNavigation', true);
this.set('modalOpen', true);
}
get isMobile() {
return this.media.isMobile;
}
}