All files / ee/app/assets/javascripts/group_member_contributions/store group_member_store.js

97.37% Statements 37/38
62.5% Branches 5/8
100% Functions 15/15
97.3% Lines 36/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89                13x   13x 13x 13x 13x 13x 13x       10x       3x       1x       3x 3x 27x           12x       1x 1x 1x   1x 1x 4x 4x 4x   4x   4x 2x     4x     1x         2x   1x   1x 1x 1x     1x 1x     1x         12x      
import createFlash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import { __ } from '~/locale';
import COLUMNS from '../constants';
 
export default class GroupMemberStore {
  constructor(memberContributionsPath) {
    this.memberContributionsPath = memberContributionsPath;
 
    this.state = {};
    this.state.isLoading = true;
    this.state.members = [];
    this.state.columns = [];
    this.state.sortOrders = {};
    this.state.currentSortedColumn = '';
  }
 
  get isLoading() {
    return this.state.isLoading;
  }
 
  get members() {
    return this.state.members;
  }
 
  get sortOrders() {
    return this.state.sortOrders;
  }
 
  setColumns(columns) {
    this.state.columns = columns;
    this.state.sortOrders = this.state.columns.reduce(
      (acc, column) => ({ ...acc, [column.name]: 1 }),
      {},
    );
  }
 
  setMembers(rawMembers) {
    this.state.members = rawMembers.map((rawMember) => GroupMemberStore.formatMember(rawMember));
  }
 
  sortMembers(sortByColumn) {
    Eif (sortByColumn) {
      this.state.currentSortedColumn = sortByColumn;
      this.state.sortOrders[sortByColumn] *= -1;
 
      const currentColumnOrder = this.state.sortOrders[sortByColumn] || 1;
      const members = this.state.members.slice().sort((a, b) => {
        let delta = -1;
        const columnOrderA = a[sortByColumn];
        const columnOrderB = b[sortByColumn];
 
        Iif (columnOrderA === columnOrderB) {
          delta = 0;
        } else if (columnOrderA > columnOrderB) {
          delta = 1;
        }
 
        return delta * currentColumnOrder;
      });
 
      this.state.members = members;
    }
  }
 
  fetchContributedMembers() {
    return axios
      .get(this.memberContributionsPath)
      .then((res) => res.data)
      .then((members) => {
        this.setColumns(COLUMNS);
        this.setMembers(members);
        this.state.isLoading = false;
      })
      .catch((e) => {
        this.state.isLoading = false;
        createFlash({
          message: __('Something went wrong while fetching group member contributions'),
        });
        throw e;
      });
  }
 
  static formatMember(rawMember) {
    return convertObjectPropsToCamelCase(rawMember);
  }
}