forked from ilios/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek-glance.gjs
More file actions
198 lines (181 loc) · 5.77 KB
/
Copy pathweek-glance.gjs
File metadata and controls
198 lines (181 loc) · 5.77 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { isNone } from '@ember/utils';
import { DateTime } from 'luxon';
import { TrackedAsyncData } from 'ember-async-data';
import { cached } from '@glimmer/tracking';
import scrollIntoView from 'ilios-common/modifiers/scroll-into-view';
import { gt, notEq } from 'ember-truth-helpers';
import { on } from '@ember/modifier';
import { fn } from '@ember/helper';
import optional from 'ilios-common/helpers/optional';
import t from 'ember-intl/helpers/t';
import FaIcon from 'ilios-common/components/fa-icon';
import WeekGlanceEvent from 'ilios-common/components/week-glance-event';
import formatDate from 'ember-intl/helpers/format-date';
export default class WeeklyGlance extends Component {
@service userEvents;
@service intl;
@service localeDays;
@cached
get weekEventsData() {
return new TrackedAsyncData(
this.userEvents.getEvents(
this.midnightAtTheStartOfTheWeekDateTime.toUnixInteger(),
this.midnightAtTheEndOfTheWeekDateTime.toUnixInteger(),
),
);
}
get weekEvents() {
return this.weekEventsData.isResolved ? this.weekEventsData.value : null;
}
get eventsLoaded() {
return !isNone(this.weekEvents);
}
get thursdayOfTheWeek() {
const thursday = DateTime.fromObject({
weekYear: this.args.year,
weekNumber: this.args.week,
weekday: 4,
hour: 0,
minute: 0,
second: 0,
});
if (!thursday.isValid) {
console.error('Invalid date', thursday.invalidReason, this.args.year, this.args.week);
return null;
}
return thursday.toJSDate();
}
get midnightAtTheStartOfTheWeekDateTime() {
if (!this.thursdayOfTheWeek) {
return null;
}
return DateTime.fromJSDate(this.localeDays.firstDayOfDateWeek(this.thursdayOfTheWeek));
}
get midnightAtTheEndOfTheWeekDateTime() {
if (!this.thursdayOfTheWeek) {
return null;
}
return DateTime.fromJSDate(this.localeDays.lastDayOfDateWeek(this.thursdayOfTheWeek));
}
get publishedWeekEvents() {
if (!this.weekEvents) {
return [];
}
return this.weekEvents.filter((ev) => {
return !ev.isBlanked && ev.isPublished && !ev.isScheduled;
});
}
get title() {
if (!this.midnightAtTheStartOfTheWeekDateTime || !this.midnightAtTheEndOfTheWeekDateTime) {
return '';
}
const from =
this.intl.formatDate(this.midnightAtTheStartOfTheWeekDateTime, { month: 'long' }) +
' ' +
this.midnightAtTheStartOfTheWeekDateTime.toFormat('d');
let to = this.midnightAtTheEndOfTheWeekDateTime.toFormat('d');
if (
!this.midnightAtTheStartOfTheWeekDateTime.hasSame(
this.midnightAtTheEndOfTheWeekDateTime,
'month',
)
) {
to =
this.intl.formatDate(this.midnightAtTheEndOfTheWeekDateTime, { month: 'long' }) +
' ' +
this.midnightAtTheEndOfTheWeekDateTime.toFormat('d');
return `${from} - ${to}`;
}
return `${from}-${to}`;
}
get nonIlmPreWorkEvents() {
if (!this.publishedWeekEvents) {
return [];
}
return this.publishedWeekEvents.filter((ev) => {
return ev.postrequisites.length === 0 || !ev.ilmSession;
});
}
get nonPreWorkEventsByDay() {
// This transforms the list of events
// into a map that groups events by the same start date.
const eventsByDate = new Map();
this.nonIlmPreWorkEvents.forEach((event) => {
const startDate = DateTime.fromISO(event.startDate);
const date = startDate.toISODate();
if (!eventsByDate.has(date)) {
eventsByDate.set(date, {
events: [],
startDate,
});
}
eventsByDate.get(date).events.push(event);
});
return eventsByDate.values();
}
<template>
<div
class="week-glance"
data-test-week-glance
{{scrollIntoView disabled=(notEq @week @weekInFocus)}}
>
{{#if @collapsible}}
<button
type="button"
class="title collapsible"
aria-expanded={{if @collapsed "false" "true"}}
data-test-week-title
{{on "click" (fn (optional @toggleCollapsed) @collapsed)}}
>
{{this.title}}
{{#if @showFullTitle}}
{{t "general.weekAtAGlance"}}
{{/if}}
<FaIcon @icon={{if @collapsed "caret-right" "caret-down"}} />
</button>
{{else}}
<h2 class="title" role={{if @collapsible "button"}} data-test-week-title>
{{this.title}}
{{#if @showFullTitle}}
{{t "general.weekAtAGlance"}}
{{/if}}
</h2>
{{/if}}
{{#unless @collapsed}}
{{#if this.eventsLoaded}}
{{#if (gt this.nonIlmPreWorkEvents.length 0)}}
{{#each this.nonPreWorkEventsByDay as |date|}}
<div class="events-by-date" data-test-events-by-date>
<h3 class="day long" data-test-day>{{formatDate date.startDate weekday="long"}}</h3>
<h3 class="day short" data-test-day>{{formatDate
date.startDate
weekday="short"
}}</h3>
<h3 class="day narrow" data-test-day>{{formatDate
date.startDate
weekday="narrow"
}}</h3>
<ul>
{{#each date.events as |event|}}
<WeekGlanceEvent @event={{event}} />
{{/each}}
</ul>
</div>
{{/each}}
{{else}}
<p class="no-events">
{{t "general.none"}}
</p>
{{/if}}
{{else}}
<p>
<FaIcon @icon="spinner" @spin={{true}} />
{{t "general.loadingEvents"}}
</p>
{{/if}}
{{/unless}}
</div>
</template>
}