File size: 3,793 Bytes
da96562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*!
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

import {
  reactive,
  computed,
  readonly,
  DeepReadonly,
} from 'vue';
import { AjaxHelper } from 'CoreHome';
import type { CustomDimension, AvailableScope, ExtractionDimension } from './types';

interface CustomDimensionsStoreState {
  isLoading: boolean;
  isUpdating: boolean;
  customDimensions: CustomDimension[];
  availableScopes: AvailableScope[];
  extractionDimensions: ExtractionDimension[];
}

class CustomDimensionsStore {
  private privateState = reactive<CustomDimensionsStoreState>({
    customDimensions: [],
    availableScopes: [],
    extractionDimensions: [],
    isLoading: false,
    isUpdating: false,
  });

  private state = computed(() => readonly(this.privateState));

  readonly isLoading = computed(() => this.state.value.isLoading);

  readonly isUpdating = computed(() => this.state.value.isUpdating);

  readonly extractionDimensions = computed(() => this.state.value.extractionDimensions);

  readonly extractionDimensionsOptions = computed(
    () => this.extractionDimensions.value.map((e) => ({ key: e.value, value: e.name })),
  );

  readonly availableScopes = computed(() => this.state.value.availableScopes);

  readonly customDimensions = computed(() => this.state.value.customDimensions);

  readonly customDimensionsById = computed(() => {
    const dimensionsById: Record<string, DeepReadonly<CustomDimension>> = {};
    this.customDimensions.value.forEach((c) => {
      dimensionsById[`${c.idcustomdimension}`] = c;
    });
    return dimensionsById;
  });

  private reloadPromise: Promise<void>|null = null;

  reload() {
    this.privateState.customDimensions = [];
    this.privateState.availableScopes = [];
    this.privateState.extractionDimensions = [];
    this.reloadPromise = null;
    return this.fetch();
  }

  fetch() {
    if (this.reloadPromise) {
      return this.reloadPromise;
    }

    this.privateState.isLoading = true;
    this.reloadPromise = Promise.all([
      this.fetchConfiguredCustomDimensions(),
      this.fetchAvailableExtractionDimensions(),
      this.fetchAvailableScopes(),
    ]).finally(() => {
      this.privateState.isLoading = false;
    }) as unknown as Promise<void>;

    return this.reloadPromise!;
  }

  fetchConfiguredCustomDimensions() {
    return AjaxHelper.fetch<CustomDimension[]>({
      method: 'CustomDimensions.getConfiguredCustomDimensions',
      filter_limit: '-1',
    }).then((r) => {
      this.privateState.customDimensions = r;
    });
  }

  fetchAvailableExtractionDimensions() {
    return AjaxHelper.fetch<ExtractionDimension[]>({
      method: 'CustomDimensions.getAvailableExtractionDimensions',
      filter_limit: '-1',
    }).then((r) => {
      this.privateState.extractionDimensions = r;
    });
  }

  fetchAvailableScopes() {
    return AjaxHelper.fetch<AvailableScope[]>({
      method: 'CustomDimensions.getAvailableScopes',
      filter_limit: '-1',
    }).then((r) => {
      this.privateState.availableScopes = r;
    });
  }

  createOrUpdateDimension(dimension: CustomDimension, method: string): Promise<void> {
    this.privateState.isUpdating = true;
    return AjaxHelper.post(
      {
        method,
        scope: dimension.scope,
        idDimension: dimension.idcustomdimension,
        idSite: dimension.idsite,
        name: dimension.name,
        description: dimension.description,
        active: dimension.active ? '1' : '0',
        caseSensitive: dimension.case_sensitive ? '1' : '0',
      },
      {
        extractions: dimension.extractions,
      },
    ).finally(() => {
      this.privateState.isUpdating = false;
    });
  }
}

export default new CustomDimensionsStore();