summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/crm/components/new_organization_form.vue
blob: 3b11edc69359be152c25aa8aef6dd1324b2f5eae (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<script>
import { GlAlert, GlButton, GlDrawer, GlFormGroup, GlFormInput } from '@gitlab/ui';
import { produce } from 'immer';
import { __, s__ } from '~/locale';
import { convertToGraphQLId } from '~/graphql_shared/utils';
import { TYPE_GROUP } from '~/graphql_shared/constants';
import createOrganization from './queries/create_organization.mutation.graphql';
import getGroupOrganizationsQuery from './queries/get_group_organizations.query.graphql';

export default {
  components: {
    GlAlert,
    GlButton,
    GlDrawer,
    GlFormGroup,
    GlFormInput,
  },
  inject: ['groupFullPath', 'groupId'],
  props: {
    drawerOpen: {
      type: Boolean,
      required: true,
    },
  },
  data() {
    return {
      name: '',
      defaultRate: null,
      description: '',
      submitting: false,
      errorMessages: [],
    };
  },
  computed: {
    invalid() {
      return this.name.trim() === '';
    },
  },
  methods: {
    save() {
      this.submitting = true;
      return this.$apollo
        .mutate({
          mutation: createOrganization,
          variables: {
            input: {
              groupId: convertToGraphQLId(TYPE_GROUP, this.groupId),
              name: this.name,
              defaultRate: this.defaultRate ? parseFloat(this.defaultRate) : null,
              description: this.description,
            },
          },
          update: this.updateCache,
        })
        .then(({ data }) => {
          if (data.customerRelationsOrganizationCreate.errors.length === 0) this.close(true);

          this.submitting = false;
        })
        .catch(() => {
          this.errorMessages = [this.$options.i18n.somethingWentWrong];
          this.submitting = false;
        });
    },
    close(success) {
      this.$emit('close', success);
    },
    updateCache(store, { data: { customerRelationsOrganizationCreate } }) {
      if (customerRelationsOrganizationCreate.errors.length > 0) {
        this.errorMessages = customerRelationsOrganizationCreate.errors;
        return;
      }

      const variables = {
        groupFullPath: this.groupFullPath,
      };
      const sourceData = store.readQuery({
        query: getGroupOrganizationsQuery,
        variables,
      });

      const data = produce(sourceData, (draftState) => {
        draftState.group.organizations.nodes = [
          ...sourceData.group.organizations.nodes,
          customerRelationsOrganizationCreate.organization,
        ];
      });

      store.writeQuery({
        query: getGroupOrganizationsQuery,
        variables,
        data,
      });
    },
    getDrawerHeaderHeight() {
      const wrapperEl = document.querySelector('.content-wrapper');

      if (wrapperEl) {
        return `${wrapperEl.offsetTop}px`;
      }

      return '';
    },
  },
  i18n: {
    buttonLabel: s__('Crm|Create organization'),
    cancel: __('Cancel'),
    name: __('Name'),
    defaultRate: s__('Crm|Default rate (optional)'),
    description: __('Description (optional)'),
    title: s__('Crm|New Organization'),
    somethingWentWrong: __('Something went wrong. Please try again.'),
  },
};
</script>

<template>
  <gl-drawer
    class="gl-drawer-responsive"
    :open="drawerOpen"
    :header-height="getDrawerHeaderHeight()"
    @close="close(false)"
  >
    <template #title>
      <h4>{{ $options.i18n.title }}</h4>
    </template>
    <gl-alert v-if="errorMessages.length" variant="danger" @dismiss="errorMessages = []">
      <ul class="gl-mb-0! gl-ml-5">
        <li v-for="error in errorMessages" :key="error">
          {{ error }}
        </li>
      </ul>
    </gl-alert>
    <form @submit.prevent="save">
      <gl-form-group :label="$options.i18n.name" label-for="organization-name">
        <gl-form-input id="organization-name" v-model="name" />
      </gl-form-group>
      <gl-form-group :label="$options.i18n.defaultRate" label-for="organization-default-rate">
        <gl-form-input
          id="organization-default-rate"
          v-model="defaultRate"
          type="number"
          step="0.01"
        />
      </gl-form-group>
      <gl-form-group :label="$options.i18n.description" label-for="organization-description">
        <gl-form-input id="organization-description" v-model="description" />
      </gl-form-group>
      <span class="gl-float-right">
        <gl-button data-testid="cancel-button" @click="close(false)">
          {{ $options.i18n.cancel }}
        </gl-button>
        <gl-button
          variant="confirm"
          :disabled="invalid"
          :loading="submitting"
          data-testid="create-new-organization-button"
          type="submit"
          >{{ $options.i18n.buttonLabel }}</gl-button
        >
      </span>
    </form>
  </gl-drawer>
</template>