<commit_before>import _ from 'lodash';

import './dashboard.scss';
import template from './dashboard.tpl.html';
import templateNewOrganizer from './new-organizer.tpl.html';
import {CAMPAIGN_STATUS_ACTIVE, CAMPAIGN_STATUS_FINALIZED} from "../../constants";

const Component = {
  controller,
  template,
};

function controller(
  $filter,
  $state,
  $mdDialog,
  adminService,
  alertService,
  dialogService,
  userService,
) {
  const vm = this;

  vm.isAdmin = false;
  vm.isJuror = false;
  vm.campaigns = [];
  vm.campaignsAdmin = null;
  vm.campaignsAdminInactive = null;

  vm.showInactiveCampaigns = false;
  vm.collapseClosedCampaigns = () => {
    vm.showInactiveCampaigns = !vm.showInactiveCampaigns;
  };

  vm.addOrganizer = addOrganizer;

  // functions

  vm.$onInit = () => {
    getAdminData();
    getJurorData();
  };

  function getAdminData() {
    userService
      .getAdmin()
      .then(data => {
        vm.isAdmin = data.data.length;
        const isActiveCampaign = campaign =>
            (campaign.active_round || !campaign.rounds.length) && !(campaign.status === CAMPAIGN_STATUS_FINALIZED);
        vm.campaignsAdmin = data.data.filter(
          campaign => isActiveCampaign(campaign),
        );
        vm.campaignsAdminInactive = data.data.filter(
          campaign => !isActiveCampaign(campaign),
        );
      })
      .catch(err => {
        vm.err = err;
      });
  }

  function getJurorData() {
    userService
      .getJuror()
      .then(data => {
        vm.isJuror = data.data.length;
        vm.user = data.user;
        if (!data.data.length) {
          return;
        }

        const roundsGroupedByCampaigns = _.groupBy(
          data.data.filter(round => round.status !== 'cancelled'),
          'campaign.id',
        );
        let campaignsJuror = _.values(roundsGroupedByCampaigns);

        // show finalized campaigns at the bottom; order campaigns by open date (more recent at the top)
        campaignsJuror.sort((campaign1, campaign2) => {
          const getStatus = campaign =>
              campaign.length > 0 && campaign[0].campaign
                  ? campaign[0].campaign.status
                  : null;

          const getOpenDate = campaign =>
              campaign.length > 0 && campaign[0].campaign
                  ? campaign[0].campaign.open_date
                  : null;

          const campaign1Status = getStatus(campaign1);
          const campaign2Status = getStatus(campaign2);
          const campaign1OpenDate = getOpenDate(campaign1);
          const campaign2OpenDate = getOpenDate(campaign2);

          if (
              campaign1Status === campaign2Status ||
              (
                campaign1Status === CAMPAIGN_STATUS_ACTIVE &&
                campaign2Status === CAMPAIGN_STATUS_ACTIVE
              )
          ) {
            if (campaign1OpenDate === campaign2OpenDate) {
              return 0;
            } else if (campaign1OpenDate < campaign2OpenDate) {
              return 1;
            } else { // if (campaign1OpenDate > campaign2OpenDate)
              return -1;
            }
          } else if (campaign1Status !== CAMPAIGN_STATUS_ACTIVE) {
            return 1;
          } else { // if (campaign2Status !== CAMPAIGN_STATUS_ACTIVE)
            return -1;
          }
        });

        vm.campaignsJuror = campaignsJuror;
      })
      .catch(err => {
        vm.err = err;
      });
  }

  function addOrganizer() {
    dialogService.show({
      template: templateNewOrganizer,
      scope: {
        organizers: [],
        add: (data, loading) => {
          if (!data[0]) {
            alertService.error({
              message: 'Error',
              detail: 'Provide organizer name',
            });
            return;
          }

          loading.window = true;
          const username = data[0].name;
          adminService.addOrganizer({ username }).then(response => {
            if (response.error) {
              loading.window = false;
              alertService.error(response.error);
              return;
            }

            alertService.success(`${username} added as an organizer`);
            $mdDialog.hide(true);
            $state.reload();
          });
        },
      },
    });
  }
}

export default Component;
<commit_msg>Clean up order campaigns code<commit_after>import _ from 'lodash';

import './dashboard.scss';
import template from './dashboard.tpl.html';
import templateNewOrganizer from './new-organizer.tpl.html';
import {CAMPAIGN_STATUS_ACTIVE, CAMPAIGN_STATUS_FINALIZED} from "../../constants";

const Component = {
  controller,
  template,
};

function controller(
  $filter,
  $state,
  $mdDialog,
  adminService,
  alertService,
  dialogService,
  userService,
) {
  const vm = this;

  vm.isAdmin = false;
  vm.isJuror = false;
  vm.campaigns = [];
  vm.campaignsAdmin = null;
  vm.campaignsAdminInactive = null;

  vm.showInactiveCampaigns = false;
  vm.collapseClosedCampaigns = () => {
    vm.showInactiveCampaigns = !vm.showInactiveCampaigns;
  };

  vm.addOrganizer = addOrganizer;

  // functions

  vm.$onInit = () => {
    getAdminData();
    getJurorData();
  };

  function getAdminData() {
    userService
      .getAdmin()
      .then(data => {
        vm.isAdmin = data.data.length;
        const isActiveCampaign = campaign =>
            (campaign.active_round || !campaign.rounds.length) && !(campaign.status === CAMPAIGN_STATUS_FINALIZED);
        vm.campaignsAdmin = data.data.filter(
          campaign => isActiveCampaign(campaign),
        );
        vm.campaignsAdminInactive = data.data.filter(
          campaign => !isActiveCampaign(campaign),
        );
      })
      .catch(err => {
        vm.err = err;
      });
  }

  function getJurorData() {
    userService
      .getJuror()
      .then(data => {
        vm.isJuror = data.data.length;
        vm.user = data.user;
        if (!data.data.length) {
          return;
        }

        const roundsGroupedByCampaigns = _.groupBy(
          data.data.filter(round => round.status !== 'cancelled'),
          'campaign.id',
        );
        let campaignsJuror = _.values(roundsGroupedByCampaigns);

        // order campaigns by open date (more recent at the top)
        campaignsJuror.sort((campaign1, campaign2) => {
          const getOpenDate = campaign =>
              campaign.length > 0 && campaign[0].campaign
                  ? campaign[0].campaign.open_date
                  : null;

          const campaign1OpenDate = getOpenDate(campaign1);
          const campaign2OpenDate = getOpenDate(campaign2);

          if (campaign1OpenDate === campaign2OpenDate) {
            return 0;
          } else if (campaign1OpenDate < campaign2OpenDate) {
            return 1;
          } else { // if (campaign1OpenDate > campaign2OpenDate)
            return -1;
          }
        });

        vm.campaignsJuror = campaignsJuror;
      })
      .catch(err => {
        vm.err = err;
      });
  }

  function addOrganizer() {
    dialogService.show({
      template: templateNewOrganizer,
      scope: {
        organizers: [],
        add: (data, loading) => {
          if (!data[0]) {
            alertService.error({
              message: 'Error',
              detail: 'Provide organizer name',
            });
            return;
          }

          loading.window = true;
          const username = data[0].name;
          adminService.addOrganizer({ username }).then(response => {
            if (response.error) {
              loading.window = false;
              alertService.error(response.error);
              return;
            }

            alertService.success(`${username} added as an organizer`);
            $mdDialog.hide(true);
            $state.reload();
          });
        },
      },
    });
  }
}

export default Component;

{'fraction_non_alphanumeric': 0.11850152905198777, 'fraction_numerical': 0.00700815494393476, 'mean_word_length': 1.9005912786400592, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '7310570', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2220, 'n_words': 429}