Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vuex_module_mappers.js « utils « lib « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 95a794dd268cc2a08f970a74944dee3616af5972 (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
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
import { mapValues, isString } from 'lodash';
import { mapState, mapActions } from 'vuex';

export const REQUIRE_STRING_ERROR_MESSAGE =
  '`vuex_module_mappers` can only be used with an array of strings, or an object with string values. Consider using the regular `vuex` map helpers instead.';

const normalizeFieldsToObject = (fields) => {
  return Array.isArray(fields)
    ? fields.reduce((acc, key) => Object.assign(acc, { [key]: key }), {})
    : fields;
};

const mapVuexModuleFields = ({ namespaceSelector, fields, vuexHelper, selector } = {}) => {
  // The `vuexHelper` needs an object which maps keys to field selector functions.
  const map = mapValues(normalizeFieldsToObject(fields), (value) => {
    if (!isString(value)) {
      throw new Error(REQUIRE_STRING_ERROR_MESSAGE);
    }

    // We need to use a good ol' function to capture the right "this".
    return function mappedFieldSelector(...args) {
      const namespace = namespaceSelector(this);

      return selector(namespace, value, ...args);
    };
  });

  return vuexHelper(map);
};

/**
 * Like `mapState`, but takes a function in the first param for selecting a namespace.
 *
 * ```
 * computed: {
 *   ...mapVuexModuleState(vm => vm.vuexModule, ['foo']),
 * }
 * ```
 *
 * @param {Function} namespaceSelector
 * @param {Array|Object} fields
 */
export const mapVuexModuleState = (namespaceSelector, fields) =>
  mapVuexModuleFields({
    namespaceSelector,
    fields,
    vuexHelper: mapState,
    selector: (namespace, value, state) => state[namespace][value],
  });

/**
 * Like `mapActions`, but takes a function in the first param for selecting a namespace.
 *
 * ```
 * methods: {
 *   ...mapVuexModuleActions(vm => vm.vuexModule, ['fetchFoos']),
 * }
 * ```
 *
 * @param {Function} namespaceSelector
 * @param {Array|Object} fields
 */
export const mapVuexModuleActions = (namespaceSelector, fields) =>
  mapVuexModuleFields({
    namespaceSelector,
    fields,
    vuexHelper: mapActions,
    selector: (namespace, value, dispatch, ...args) => dispatch(`${namespace}/${value}`, ...args),
  });

/**
 * Like `mapGetters`, but takes a function in the first param for selecting a namespace.
 *
 * ```
 * computed: {
 *   ...mapGetters(vm => vm.vuexModule, ['hasSearchInfo']),
 * }
 * ```
 *
 * @param {Function} namespaceSelector
 * @param {Array|Object} fields
 */
export const mapVuexModuleGetters = (namespaceSelector, fields) =>
  mapVuexModuleFields({
    namespaceSelector,
    fields,
    // `mapGetters` does not let us pass an object which maps to functions. Thankfully `mapState` does
    // and gives us access to the getters.
    vuexHelper: mapState,
    selector: (namespace, value, state, getters) => getters[`${namespace}/${value}`],
  });