summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/registry/shared/utils.js
blob: bdf1ab9507d95ccbe9641dc806eb7fa990e920c2 (plain)
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
import { n__ } from '~/locale';
import { KEEP_N_OPTIONS, CADENCE_OPTIONS, OLDER_THAN_OPTIONS } from './constants';

export const findDefaultOption = options => {
  const item = options.find(o => o.default);
  return item ? item.key : null;
};

export const mapComputedToEvent = (list, root) => {
  const result = {};
  list.forEach(e => {
    result[e] = {
      get() {
        return this[root][e];
      },
      set(value) {
        this.$emit('input', { newValue: { ...this[root], [e]: value }, modified: e });
      },
    };
  });
  return result;
};

export const olderThanTranslationGenerator = variable =>
  n__(
    '%d day until tags are automatically removed',
    '%d days until tags are automatically removed',
    variable,
  );

export const keepNTranslationGenerator = variable =>
  n__('%d tag per image name', '%d tags per image name', variable);

export const optionLabelGenerator = (collection, translationFn) =>
  collection.map(option => ({
    ...option,
    label: translationFn(option.variable),
  }));

export const formOptionsGenerator = () => {
  return {
    olderThan: optionLabelGenerator(OLDER_THAN_OPTIONS, olderThanTranslationGenerator),
    cadence: CADENCE_OPTIONS,
    keepN: optionLabelGenerator(KEEP_N_OPTIONS, keepNTranslationGenerator),
  };
};