<commit_before>import Ember from 'ember';
import { task, timeout } from 'ember-concurrency';

const { Component, RSVP, computed, inject, isEmpty, isPresent } = Ember;
const { map, all } = RSVP;
const { service } = inject;

export default Component.extend({
  store: service(),
  programYear: null,
  isManaging: false,
  bufferStewards: [],
  classNameBindings: [':detail-stewards', ':stewards-manager', 'showCollapsible:collapsible'],
  editable: true,

  showCollapsible: computed('isManaging', 'programYear.stewards.[]', function () {
    const isManaging = this.get('isManaging');
    const programYear = this.get('programYear');
    const stewardIds = programYear.hasMany('stewards').ids();
    return !isManaging && stewardIds.get('length');
  }),

  stewardsBySchool: computed('programYear.stewards.[]', async function(){
    const programYear = this.get('programYear');
    if (isEmpty(programYear)) {
      return [];
    }

    const stewards = await programYear.get('stewards');
    const stewardObjects = await map(stewards.toArray(), async steward => {
      const school = await steward.get('school');
      const schoolId = isPresent(school)?school.get('id'):0;
      const schoolTitle = isPresent(school)?school.get('title'):null;
      const department = await steward.get('department');
      const departmentId = isPresent(department)?department.get('id'):0;
      const departmentTitle = isPresent(department)?department.get('title'):null;
      return {
        schoolId,
        schoolTitle,
        departmentId,
        departmentTitle,
      };
    });
    const schools = stewardObjects.uniqBy('schoolId');
    const schoolData = schools.map(obj => {
      const departments = stewardObjects.filterBy('schoolId', obj.schoolId);
      const rhett = {
        schoolId: obj.schoolId,
        schoolTitle: obj.schoolTitle,
        departments
      };

      return rhett;
    });

    return schoolData;
  }),

  save: task( function * (){
    yield timeout(10);
    const programYear = this.get('programYear');
    const bufferStewards = this.get('bufferStewards');
    let stewards = yield programYear.get('stewards');
    let stewardsToRemove = stewards.filter(steward => !bufferStewards.includes(steward));
    let stewardsToAdd = bufferStewards.filter(steward => !stewards.includes(steward));
    stewardsToAdd.setEach('programYear', programYear);
    yield all(stewardsToRemove.invoke('destroyRecord'));
    yield all(stewardsToAdd.invoke('save'));
    this.set('isManaging', false);
    this.set('bufferStewards', []);
  }),

  manage: task( function * (){
    yield timeout(10);
    this.get('expand')();
    const stewards = yield this.get('programYear.stewards');
    this.set('bufferStewards', stewards.toArray());
    this.set('isManaging', true);
  }),
  actions: {
    collapse(){
      const programYear = this.get('programYear');
      const stewardIds = programYear.hasMany('stewards').ids();
      if (stewardIds.get('length')) {
        this.get('collapse')();
      }
    },
    cancel: function(){
      this.set('isManaging', false);
      this.set('bufferStewards', []);<commit_msg>Swap add / remove order for program year stewards<commit_after>import Ember from 'ember';
import { task, timeout } from 'ember-concurrency';

const { Component, RSVP, computed, inject, isEmpty, isPresent } = Ember;
const { map, all } = RSVP;
const { service } = inject;

export default Component.extend({
  store: service(),
  programYear: null,
  isManaging: false,
  bufferStewards: [],
  classNameBindings: [':detail-stewards', ':stewards-manager', 'showCollapsible:collapsible'],
  editable: true,

  showCollapsible: computed('isManaging', 'programYear.stewards.[]', function () {
    const isManaging = this.get('isManaging');
    const programYear = this.get('programYear');
    const stewardIds = programYear.hasMany('stewards').ids();
    return !isManaging && stewardIds.get('length');
  }),

  stewardsBySchool: computed('programYear.stewards.[]', async function(){
    const programYear = this.get('programYear');
    if (isEmpty(programYear)) {
      return [];
    }

    const stewards = await programYear.get('stewards');
    const stewardObjects = await map(stewards.toArray(), async steward => {
      const school = await steward.get('school');
      const schoolId = isPresent(school)?school.get('id'):0;
      const schoolTitle = isPresent(school)?school.get('title'):null;
      const department = await steward.get('department');
      const departmentId = isPresent(department)?department.get('id'):0;
      const departmentTitle = isPresent(department)?department.get('title'):null;
      return {
        schoolId,
        schoolTitle,
        departmentId,
        departmentTitle,
      };
    });
    const schools = stewardObjects.uniqBy('schoolId');
    const schoolData = schools.map(obj => {
      const departments = stewardObjects.filterBy('schoolId', obj.schoolId);
      const rhett = {
        schoolId: obj.schoolId,
        schoolTitle: obj.schoolTitle,
        departments
      };

      return rhett;
    });

    return schoolData;
  }),

  save: task( function * (){
    yield timeout(10);
    const programYear = this.get('programYear');
    const bufferStewards = this.get('bufferStewards');
    let stewards = yield programYear.get('stewards');
    let stewardsToRemove = stewards.filter(steward => !bufferStewards.includes(steward));
    let stewardsToAdd = bufferStewards.filter(steward => !stewards.includes(steward));
    stewardsToAdd.setEach('programYear', programYear);
    yield all(stewardsToAdd.invoke('save'));
    yield all(stewardsToRemove.invoke('destroyRecord'));
    this.set('isManaging', false);
    this.set('bufferStewards', []);
  }),

  manage: task( function * (){
    yield timeout(10);
    this.get('expand')();
    const stewards = yield this.get('programYear.stewards');
    this.set('bufferStewards', stewards.toArray());
    this.set('isManaging', true);
  }),
  actions: {
    collapse(){
      const programYear = this.get('programYear');
      const stewardIds = programYear.hasMany('stewards').ids();
      if (stewardIds.get('length')) {
        this.get('collapse')();
      }
    },
    cancel: function(){
      this.set('isManaging', false);
      this.set('bufferStewards', []);
{'fraction_non_alphanumeric': 0.14887194153161742, 'fraction_numerical': 0.0019065776930409914, 'mean_word_length': 3.783434650455927, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1862166', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1862, 'n_words': 375}