packages/ilios-common/addon/components/course/objectives.js
import Component from '@glimmer/component';
import { service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { dropTask, restartableTask } from 'ember-concurrency';
import { action } from '@ember/object';
import { mapBy } from 'ilios-common/utils/array-helpers';
export default class CourseObjectivesComponent extends Component {
@service store;
@service flashMessages;
@tracked newObjectiveEditorOn = false;
@tracked newObjectiveTitle;
@tracked objectivesRelationship;
get showCollapsible() {
return this.hasObjectives && !this.isManaging;
}
get hasObjectives() {
return this.objectiveCount > 0;
}
get objectiveCount() {
return this.objectivesRelationship ? this.objectivesRelationship.length : 0;
}
load = restartableTask(async () => {
this.objectivesRelationship = await this.args.course.courseObjectives;
});
saveNewObjective = dropTask(async (title) => {
const newCourseObjective = this.store.createRecord('course-objective');
let position = 0;
const courseObjectives = await this.args.course.courseObjectives;
if (courseObjectives.length) {
const positions = mapBy(courseObjectives, 'position');
position = Math.max(...positions) + 1;
}
newCourseObjective.set('title', title);
newCourseObjective.set('position', position);
newCourseObjective.set('course', this.args.course);
await newCourseObjective.save();
this.newObjectiveEditorOn = false;
this.flashMessages.success('general.newObjectiveSaved');
});
@action
toggleNewObjectiveEditor() {
//force expand the objective component
//otherwise adding the first new objective will cause it to close
this.args.expand();
this.newObjectiveEditorOn = !this.newObjectiveEditorOn;
}
@action
collapse() {
if (this.hasObjectives) {
this.args.collapse();
}
}
}