summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/filtered_search_bar/tokens/crm_contact_token.vue
blob: fcabeab1d8a213f3c70c22b8a4e72e6d4b278642 (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
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
<script>
import { GlFilteredSearchSuggestion } from '@gitlab/ui';

import { TYPENAME_CRM_CONTACT } from '~/graphql_shared/constants';
import { getIdFromGraphQLId, convertToGraphQLId } from '~/graphql_shared/utils';
import { createAlert } from '~/flash';
import { WORKSPACE_GROUP, WORKSPACE_PROJECT } from '~/issues/constants';
import { isPositiveInteger } from '~/lib/utils/number_utils';
import { __ } from '~/locale';
import searchCrmContactsQuery from '../queries/search_crm_contacts.query.graphql';

import { OPTIONS_NONE_ANY } from '../constants';

import BaseToken from './base_token.vue';

export default {
  components: {
    BaseToken,
    GlFilteredSearchSuggestion,
  },
  props: {
    config: {
      type: Object,
      required: true,
    },
    value: {
      type: Object,
      required: true,
    },
    active: {
      type: Boolean,
      required: true,
    },
  },
  data() {
    return {
      contacts: this.config.initialContacts || [],
      loading: false,
    };
  },
  computed: {
    defaultContacts() {
      return this.config.defaultContacts || OPTIONS_NONE_ANY;
    },
    namespace() {
      return this.config.isProject ? WORKSPACE_PROJECT : WORKSPACE_GROUP;
    },
  },
  methods: {
    getActiveContact(contacts, data) {
      return contacts.find((contact) => {
        return `${this.formatContactId(contact)}` === data;
      });
    },
    getContactName(contact) {
      return `${contact.firstName} ${contact.lastName}`;
    },
    fetchContacts(searchTerm) {
      let searchString = null;
      let searchId = null;
      if (isPositiveInteger(searchTerm)) {
        searchId = this.formatContactGraphQLId(searchTerm);
      } else {
        searchString = searchTerm;
      }

      this.loading = true;

      this.$apollo
        .query({
          query: searchCrmContactsQuery,
          variables: {
            fullPath: this.config.fullPath,
            searchString,
            searchIds: searchId ? [searchId] : null,
            isProject: this.config.isProject,
          },
        })
        .then(({ data }) => {
          this.contacts = this.config.isProject
            ? data[this.namespace]?.group.contacts.nodes
            : data[this.namespace]?.contacts.nodes;
        })
        .catch(() =>
          createAlert({
            message: __('There was a problem fetching CRM contacts.'),
          }),
        )
        .finally(() => {
          this.loading = false;
        });
    },
    formatContactId(contact) {
      return `${getIdFromGraphQLId(contact.id)}`;
    },
    formatContactGraphQLId(id) {
      return convertToGraphQLId(TYPENAME_CRM_CONTACT, id);
    },
  },
};
</script>

<template>
  <base-token
    :config="config"
    :value="value"
    :active="active"
    :suggestions-loading="loading"
    :suggestions="contacts"
    :get-active-token-value="getActiveContact"
    :default-suggestions="defaultContacts"
    v-bind="$attrs"
    @fetch-suggestions="fetchContacts"
    v-on="$listeners"
  >
    <template #view="{ viewTokenProps: { inputValue, activeTokenValue } }">
      {{ activeTokenValue ? getContactName(activeTokenValue) : inputValue }}
    </template>
    <template #suggestions-list="{ suggestions }">
      <gl-filtered-search-suggestion
        v-for="contact in suggestions"
        :key="formatContactId(contact)"
        :value="formatContactId(contact)"
      >
        <div>
          <div>{{ getContactName(contact) }}</div>
          <div class="gl-font-sm">{{ contact.email }}</div>
        </div>
      </gl-filtered-search-suggestion>
    </template>
  </base-token>
</template>