Skip to content

Commit

Permalink
Merge pull request #7666 from stopfstedt/more_moment_with_luxon_repla…
Browse files Browse the repository at this point in the history
…cement

replaces Moment with Luxon in  components in ilios-common - part 2
  • Loading branch information
jrjohnson authored Feb 29, 2024
2 parents 02fcb43 + 60357d7 commit 61b2675
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ module('Acceptance | Session - Overview', function (hooks) {
assert.strictEqual(currentRouteName(), 'session.index');
assert.strictEqual(
page.details.overview.lastUpdated,
'Last Update Last Update: 07/09/2019 5:00 PM',
'Last Update Last Update: 7/9/2019 5:00 PM',
);
});

Expand Down
34 changes: 16 additions & 18 deletions packages/ilios-common/addon/components/daily-calendar-event.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import Component from '@glimmer/component';
import moment from 'moment';
import { DateTime } from 'luxon';
import colorChange from 'ilios-common/utils/color-change';
import { htmlSafe } from '@ember/template';
import calendarEventTooltip from 'ilios-common/utils/calendar-event-tooltip';
import { service } from '@ember/service';

export default class DailyCalendarEventComponent extends Component {
@service intl;
@service moment;

constructor() {
super(...arguments);
Expand Down Expand Up @@ -37,45 +36,44 @@ export default class DailyCalendarEventComponent extends Component {

get tooltipContent() {
//access the locale info here so the getter will recompute when it changes
this.moment.locale;
this.intl.locale;
return calendarEventTooltip(this.args.event, this.intl, 'h:mma');
}

get recentlyUpdated() {
const lastModifiedDate = moment(this.args.event.lastModified);
const today = moment();
const daysSinceLastUpdate = today.diff(lastModifiedDate, 'days');
const lastModifiedDate = DateTime.fromISO(this.args.event.lastModified);
const today = DateTime.now();
const daysSinceLastUpdate = today.diff(lastModifiedDate, 'days').days;

return daysSinceLastUpdate < 6 ? true : false;
return daysSinceLastUpdate < 6;
}

get startMoment() {
return moment(this.args.event.startDate);
get startLuxon() {
return DateTime.fromISO(this.args.event.startDate);
}

get endMoment() {
return moment(this.args.event.endDate);
get endLuxon() {
return DateTime.fromISO(this.args.event.endDate);
}

get startOfDay() {
return this.startMoment.startOf('day');
get startOfDayLuxon() {
return this.startLuxon.startOf('day');
}

get startMinuteRounded() {
const minute = this.startMoment.diff(this.startOfDay, 'minutes');
const minute = this.startLuxon.diff(this.startOfDayLuxon, 'minutes').minutes;
return Math.ceil(minute / 5);
}

get totalMinutesRounded() {
const minutes = this.endMoment.diff(this.startMoment, 'minutes');
const minutes = this.endLuxon.diff(this.startLuxon, 'minutes').minutes;
return Math.floor(minutes / 5);
}

getMinuteInTheDay(date) {
const m = moment(date);
const midnight = moment(date).startOf('day');
return m.diff(midnight, 'minutes');
const dt = DateTime.fromISO(date);
const midnight = dt.startOf('day');
return dt.diff(midnight, 'minutes').minutes;
}

get span() {
Expand Down
14 changes: 7 additions & 7 deletions packages/ilios-common/addon/components/dashboard/week.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import Component from '@glimmer/component';
import moment from 'moment';

import { DateTime } from 'luxon';
export default class DashboardWeekComponent extends Component {
get expanded() {
const lastSunday = moment().day(1).subtract(1, 'week').format('W');
const thisSunday = moment().day(1).format('W');
const nextSunday = moment().day(1).add(1, 'week').format('W');
const now = DateTime.now();
const lastSunday = now.set({ weekday: 1 }).minus({ week: 1 }).toFormat('W');
const thisSunday = now.set({ weekday: 1 }).toFormat('W');
const nextSunday = now.set({ weekday: 1 }).plus({ week: 1 }).toFormat('W');

return `${lastSunday}-${thisSunday}-${nextSunday}`;
}
get year() {
return moment().year();
return DateTime.now().year;
}
get week() {
//set day to Thursday to correctly calculate isoWeek
return moment().day(4).isoWeek();
return DateTime.now().set({ weekday: 4 }).weekNumber;
}
}
6 changes: 3 additions & 3 deletions packages/ilios-common/addon/components/session-copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { hash, all, filter } from 'rsvp';
import { dropTask, restartableTask, timeout } from 'ember-concurrency';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import moment from 'moment';
import { DateTime } from 'luxon';
import { findById, sortBy } from 'ilios-common/utils/array-helpers';

export default class SessionCopyComponent extends Component {
Expand All @@ -31,8 +31,8 @@ export default class SessionCopyComponent extends Component {
},
}),
});
const now = moment();
const thisYear = now.year();
const now = DateTime.now();
const thisYear = now.year;
this.years = years
.map((year) => Number(year.id))
.filter((year) => year >= thisYear - 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { validatable, NotBlank } from 'ilios-common/decorators/validation';
import { dropTask } from 'ember-concurrency';
import moment from 'moment';
import { DateTime } from 'luxon';

@validatable
export default class SessionOverviewIlmDuedateComponent extends Component {
Expand All @@ -16,22 +16,19 @@ export default class SessionOverviewIlmDuedateComponent extends Component {

@action
updateDate(date) {
const currentDueDate = moment(this.dueDate);
this.dueDate = moment(date)
.hour(currentDueDate.hour())
.minute(currentDueDate.minute())
.toDate();
const currentDueDate = DateTime.fromJSDate(this.dueDate);
this.dueDate = DateTime.fromJSDate(date)
.set({
hour: currentDueDate.hour,
minute: currentDueDate.minute,
})
.toJSDate();
}

@action
updateTime(value, type) {
const dueDate = moment(this.dueDate);
if (type === 'hour') {
dueDate.hour(value);
} else {
dueDate.minute(value);
}
this.dueDate = dueDate.toDate();
const update = 'hour' === type ? { hour: value } : { minute: value };
this.dueDate = DateTime.fromJSDate(this.dueDate).set(update).toJSDate();
}

save = dropTask(async () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/ilios-common/addon/components/session-overview.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { action } from '@ember/object';
import { service } from '@ember/service';
import { isEmpty } from '@ember/utils';
import { task, restartableTask, dropTask } from 'ember-concurrency';
import moment from 'moment';
import { DateTime } from 'luxon';
import { validatable, Length, Gte, NotBlank } from 'ilios-common/decorators/validation';
import { hash } from 'rsvp';
import { findById, sortBy } from 'ilios-common/utils/array-helpers';
Expand Down Expand Up @@ -123,7 +123,7 @@ export default class SessionOverview extends Component {
} else {
this.isIndependentLearning = false;
}
this.updatedAt = moment(session.updatedAt).format('L LT');
this.updatedAt = DateTime.fromJSDate(session.updatedAt).toFormat('D t');
this.sessionTypes = sessionTypes || [];
this.description = session.description;
});
Expand Down Expand Up @@ -172,7 +172,7 @@ export default class SessionOverview extends Component {
await ilmSession.save();
} else {
const hours = 1;
const dueDate = moment().add(6, 'weeks').hour('17').minute('00').toDate();
const dueDate = DateTime.now().plus({ week: 6 }).set({ hour: 17, minute: 0 }).toJSDate();
this.hours = hours;
const ilmSession = this.store.createRecord('ilm-session', {
session: this.args.session,
Expand Down
4 changes: 2 additions & 2 deletions packages/ilios-common/addon/components/weekly-events.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';
import moment from 'moment';
import { DateTime } from 'luxon';

export default class WeeklyEvents extends Component {
get weeksInYear() {
const weeksInTheYear = moment().year(this.args.year).isoWeeksInYear();
const weeksInTheYear = DateTime.now().set({ year: this.args.year }).weeksInWeekYear;
const weeks = [];
for (let i = 1; i <= weeksInTheYear; i++) {
weeks.push(`${i}`);
Expand Down
Loading

0 comments on commit 61b2675

Please sign in to comment.