summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/crm/contacts/components/contact_form_wrapper.vue
blob: b29089519e22f049a9cd963d72dd9e99c3016380 (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
<script>
import { s__, __ } from '~/locale';
import { convertToGraphQLId } from '~/graphql_shared/utils';
import { TYPE_CRM_CONTACT, TYPE_GROUP } from '~/graphql_shared/constants';
import ContactForm from '../../components/form.vue';
import getGroupOrganizationsQuery from '../../organizations/components/graphql/get_group_organizations.query.graphql';
import getGroupContactsQuery from './graphql/get_group_contacts.query.graphql';
import createContactMutation from './graphql/create_contact.mutation.graphql';
import updateContactMutation from './graphql/update_contact.mutation.graphql';

export default {
  components: {
    ContactForm,
  },
  inject: ['groupFullPath', 'groupId'],
  props: {
    isEditMode: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      organizations: [],
    };
  },
  apollo: {
    organizations: {
      query() {
        return getGroupOrganizationsQuery;
      },
      variables() {
        return {
          groupFullPath: this.groupFullPath,
        };
      },
      update(data) {
        return this.extractOrganizations(data);
      },
    },
  },
  computed: {
    contactGraphQLId() {
      if (!this.isEditMode) return null;

      return convertToGraphQLId(TYPE_CRM_CONTACT, this.$route.params.id);
    },
    groupGraphQLId() {
      return convertToGraphQLId(TYPE_GROUP, this.groupId);
    },
    mutation() {
      if (this.isEditMode) return updateContactMutation;

      return createContactMutation;
    },
    getQuery() {
      return {
        query: getGroupContactsQuery,
        variables: { groupFullPath: this.groupFullPath, ids: [this.contactGraphQLId] },
      };
    },
    title() {
      if (this.isEditMode) return s__('Crm|Edit contact');

      return s__('Crm|New contact');
    },
    successMessage() {
      if (this.isEditMode) return s__('Crm|Contact has been updated.');

      return s__('Crm|Contact has been added.');
    },
    additionalCreateParams() {
      return { groupId: this.groupGraphQLId };
    },
    fields() {
      const fields = [
        { name: 'firstName', label: __('First name'), required: true },
        { name: 'lastName', label: __('Last name'), required: true },
        { name: 'email', label: __('Email'), required: true },
        { name: 'phone', label: __('Phone') },
        {
          name: 'organizationId',
          label: s__('Crm|Organization'),
          values: this.organizationSelectValues,
        },
        { name: 'description', label: __('Description') },
      ];

      if (this.isEditMode)
        fields.push({ name: 'active', label: s__('Crm|Active'), required: true, bool: true });

      return fields;
    },
    organizationSelectValues() {
      const values = this.organizations.map((o) => {
        return { value: o.id, text: o.name };
      });

      values.unshift({ value: null, text: s__('Crm|No organization') });
      return values;
    },
  },
  methods: {
    extractOrganizations(data) {
      const organizations = data?.group?.organizations?.nodes || [];
      return organizations.slice().sort((a, b) => a.name.localeCompare(b.name));
    },
  },
};
</script>

<template>
  <contact-form
    :drawer-open="true"
    :get-query="getQuery"
    get-query-node-path="group.contacts"
    :mutation="mutation"
    :additional-create-params="additionalCreateParams"
    :existing-id="contactGraphQLId"
    :fields="fields"
    :title="title"
    :success-message="successMessage"
  />
</template>