import {bindable, inject} from 'aurelia-framework';
import {Server} from 'backend/server';
import {Profile} from 'services/profile'
import {AUChannel} from 'services/channel'

@inject(Server, Profile, AUChannel)
export class ArticleMenu {
  tutorials = null;
  @bindable selectedProfile;

  constructor(server, profile, channel) {
    this.profile = profile;
    this.server = server;
    this.channel = channel;
    this.selectedProfile = this.profile.current.value;
  }

  activate() {
    this.profileChanged(this.profile.current);
  }

  profileChanged(profile) {
    this.server.getTutorialsForProfile(profile.value).then(tutorials => {
      this.tutorials = tutorials;
    });
  }

  selectedProfileChanged(profile) {
    profile = profile.value ? profile : this.profile.getValue(profile);
    this.profile.current = profile;
    this.profileChanged(profile);
  }
}

import {bindable, inject} from 'aurelia-framework';
import {Server} from 'backend/server';
import {Profile} from 'services/profile'
import {AUChannel} from 'services/channel'

@inject(Server, Profile, AUChannel)
export class ArticleMenu {
  tutorials = null;
  @bindable selectedProfile;

  constructor(server, profile, channel) {
    this.profile = profile;
    this.server = server;
    this.channel = channel;
    this.selectedProfile = this.profile.current.value;
  }

  activate() {
    this.profileChanged(this.profile.current);
  }

  profileChanged(profile) {
    this.server.getTutorialsForProfile(profile.value).then(tutorials => {
      this.tutorials = tutorials;
      let selectedTuroial = tutorials.find(x => x.isSelected);
      if (!selectedTuroial && tutorials.length) {
        this.tutorials[0].isSelected = true;
      }
    });
  }

  selectedProfileChanged(profile) {
    profile = profile.value ? profile : this.profile.getValue(profile);
    this.profile.current = profile;
    this.profileChanged(profile);
  }
}

Set Default selected tutorial if none are initially selected
fix(default-selected-tutorial): Set Default selected tutorial if none are initially selected
