All files / app/assets/javascripts/header_search/store getters.js

100% Statements 46/46
85.71% Branches 12/14
100% Functions 13/13
100% Lines 46/46

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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199                            1x 5x                     5x     1x 4x                 4x     1x 3x             1x 3x             1x 1x   1x                                                         1x 5x                     5x     1x 5x                   5x     1x 5x                 5x     1x 1x   1x 1x               1x 1x               1x           1x     1x 1x 1x   1x 4x   4x 1x   3x         3x       1x     1x 4x 1x     3x   6x         3x    
import { omitBy, isNil } from 'lodash';
import { objectToQuery } from '~/lib/utils/url_utility';
 
import {
  MSG_ISSUES_ASSIGNED_TO_ME,
  MSG_ISSUES_IVE_CREATED,
  MSG_MR_ASSIGNED_TO_ME,
  MSG_MR_IM_REVIEWER,
  MSG_MR_IVE_CREATED,
  MSG_IN_PROJECT,
  MSG_IN_GROUP,
  MSG_IN_ALL_GITLAB,
} from '../constants';
 
export const searchQuery = (state) => {
  const query = omitBy(
    {
      search: state.search,
      nav_source: 'navbar',
      project_id: state.searchContext.project?.id,
      group_id: state.searchContext.group?.id,
      scope: state.searchContext?.scope,
    },
    isNil,
  );
 
  return `${state.searchPath}?${objectToQuery(query)}`;
};
 
export const autocompleteQuery = (state) => {
  const query = omitBy(
    {
      term: state.search,
      project_id: state.searchContext.project?.id,
      project_ref: state.searchContext?.ref,
    },
    isNil,
  );
 
  return `${state.autocompletePath}?${objectToQuery(query)}`;
};
 
export const scopedIssuesPath = (state) => {
  return (
    state.searchContext.project_metadata?.issues_path ||
    state.searchContext.group_metadata?.issues_path ||
    state.issuesPath
  );
};
 
export const scopedMRPath = (state) => {
  return (
    state.searchContext.project_metadata?.mr_path ||
    state.searchContext.group_metadata?.mr_path ||
    state.mrPath
  );
};
 
export const defaultSearchOptions = (state, getters) => {
  const userName = gon.current_username;
 
  return [
    {
      html_id: 'default-issues-assigned',
      title: MSG_ISSUES_ASSIGNED_TO_ME,
      url: `${getters.scopedIssuesPath}/?assignee_username=${userName}`,
    },
    {
      html_id: 'default-issues-created',
      title: MSG_ISSUES_IVE_CREATED,
      url: `${getters.scopedIssuesPath}/?author_username=${userName}`,
    },
    {
      html_id: 'default-mrs-assigned',
      title: MSG_MR_ASSIGNED_TO_ME,
      url: `${getters.scopedMRPath}/?assignee_username=${userName}`,
    },
    {
      html_id: 'default-mrs-reviewer',
      title: MSG_MR_IM_REVIEWER,
      url: `${getters.scopedMRPath}/?reviewer_username=${userName}`,
    },
    {
      html_id: 'default-mrs-created',
      title: MSG_MR_IVE_CREATED,
      url: `${getters.scopedMRPath}/?author_username=${userName}`,
    },
  ];
};
 
export const projectUrl = (state) => {
  const query = omitBy(
    {
      search: state.search,
      nav_source: 'navbar',
      project_id: state.searchContext?.project?.id,
      group_id: state.searchContext?.group?.id,
      scope: state.searchContext?.scope,
    },
    isNil,
  );
 
  return `${state.searchPath}?${objectToQuery(query)}`;
};
 
export const groupUrl = (state) => {
  const query = omitBy(
    {
      search: state.search,
      nav_source: 'navbar',
      group_id: state.searchContext?.group?.id,
      scope: state.searchContext?.scope,
    },
    isNil,
  );
 
  return `${state.searchPath}?${objectToQuery(query)}`;
};
 
export const allUrl = (state) => {
  const query = omitBy(
    {
      search: state.search,
      nav_source: 'navbar',
      scope: state.searchContext?.scope,
    },
    isNil,
  );
 
  return `${state.searchPath}?${objectToQuery(query)}`;
};
 
export const scopedSearchOptions = (state, getters) => {
  const options = [];
 
  Eif (state.searchContext.project) {
    options.push({
      html_id: 'scoped-in-project',
      scope: state.searchContext.project.name,
      description: MSG_IN_PROJECT,
      url: getters.projectUrl,
    });
  }
 
  Eif (state.searchContext.group) {
    options.push({
      html_id: 'scoped-in-group',
      scope: state.searchContext.group.name,
      description: MSG_IN_GROUP,
      url: getters.groupUrl,
    });
  }
 
  options.push({
    html_id: 'scoped-in-all',
    description: MSG_IN_ALL_GITLAB,
    url: getters.allUrl,
  });
 
  return options;
};
 
export const autocompleteGroupedSearchOptions = (state) => {
  const groupedOptions = {};
  const results = [];
 
  state.autocompleteOptions.forEach((option) => {
    const category = groupedOptions[option.category];
 
    if (category) {
      category.data.push(option);
    } else {
      groupedOptions[option.category] = {
        category: option.category,
        data: [option],
      };
 
      results.push(groupedOptions[option.category]);
    }
  });
 
  return results;
};
 
export const searchOptions = (state, getters) => {
  if (!state.search) {
    return getters.defaultSearchOptions;
  }
 
  const sortedAutocompleteOptions = Object.values(getters.autocompleteGroupedSearchOptions).reduce(
    (options, group) => {
      return [...options, ...group.data];
    },
    [],
  );
 
  return getters.scopedSearchOptions.concat(sortedAutocompleteOptions);
};