Commit d170dd63 authored by David O'Regan's avatar David O'Regan

Merge branch 'alert_settings_apollo_Cache' into 'master'

Alert Settings Apollo

See merge request gitlab-org/gitlab!47437
parents c66cd2dc edd4710c
...@@ -13,6 +13,7 @@ import { ...@@ -13,6 +13,7 @@ import {
import { s__, __ } from '~/locale'; import { s__, __ } from '~/locale';
import Tracking from '~/tracking'; import Tracking from '~/tracking';
import { trackAlertIntegrationsViewsOptions, integrationToDeleteDefault } from '../constants'; import { trackAlertIntegrationsViewsOptions, integrationToDeleteDefault } from '../constants';
import getCurrentIntegrationQuery from '../graphql/queries/get_current_integration.query.graphql';
export const i18n = { export const i18n = {
title: s__('AlertsIntegrations|Current integrations'), title: s__('AlertsIntegrations|Current integrations'),
...@@ -58,11 +59,6 @@ export default { ...@@ -58,11 +59,6 @@ export default {
required: false, required: false,
default: false, default: false,
}, },
currentIntegration: {
type: Object,
required: false,
default: null,
},
}, },
fields: [ fields: [
{ {
...@@ -82,9 +78,15 @@ export default { ...@@ -82,9 +78,15 @@ export default {
label: __('Actions'), label: __('Actions'),
}, },
], ],
apollo: {
currentIntegration: {
query: getCurrentIntegrationQuery,
},
},
data() { data() {
return { return {
integrationToDelete: integrationToDeleteDefault, integrationToDelete: integrationToDeleteDefault,
currentIntegration: null,
}; };
}, },
mounted() { mounted() {
...@@ -101,11 +103,11 @@ export default { ...@@ -101,11 +103,11 @@ export default {
const { category, action } = trackAlertIntegrationsViewsOptions; const { category, action } = trackAlertIntegrationsViewsOptions;
Tracking.event(category, action); Tracking.event(category, action);
}, },
intergrationToDelete({ name, id }) { setIntegrationToDelete({ name, id }) {
this.integrationToDelete.id = id; this.integrationToDelete.id = id;
this.integrationToDelete.name = name; this.integrationToDelete.name = name;
}, },
deleteIntergration() { deleteIntegration() {
this.$emit('delete-integration', { id: this.integrationToDelete.id }); this.$emit('delete-integration', { id: this.integrationToDelete.id });
this.integrationToDelete = { ...integrationToDeleteDefault }; this.integrationToDelete = { ...integrationToDeleteDefault };
}, },
...@@ -153,7 +155,7 @@ export default { ...@@ -153,7 +155,7 @@ export default {
<gl-button <gl-button
v-gl-modal.deleteIntegration v-gl-modal.deleteIntegration
icon="remove" icon="remove"
@click="intergrationToDelete(item)" @click="setIntegrationToDelete(item)"
/> />
</gl-button-group> </gl-button-group>
</template> </template>
...@@ -175,7 +177,7 @@ export default { ...@@ -175,7 +177,7 @@ export default {
:title="__('Are you sure?')" :title="__('Are you sure?')"
:ok-title="s__('AlertSettings|Delete integration')" :ok-title="s__('AlertSettings|Delete integration')"
ok-variant="danger" ok-variant="danger"
@ok="deleteIntergration" @ok="deleteIntegration"
> >
<gl-sprintf <gl-sprintf
:message=" :message="
......
...@@ -12,11 +12,12 @@ import { ...@@ -12,11 +12,12 @@ import {
GlModalDirective, GlModalDirective,
GlToggle, GlToggle,
} from '@gitlab/ui'; } from '@gitlab/ui';
import MappingBuilder from './alert_mapping_builder.vue';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin'; import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { s__ } from '~/locale'; import { s__ } from '~/locale';
import ClipboardButton from '~/vue_shared/components/clipboard_button.vue'; import ClipboardButton from '~/vue_shared/components/clipboard_button.vue';
import MappingBuilder from './alert_mapping_builder.vue';
import AlertSettingsFormHelpBlock from './alert_settings_form_help_block.vue'; import AlertSettingsFormHelpBlock from './alert_settings_form_help_block.vue';
import getCurrentIntegrationQuery from '../graphql/queries/get_current_integration.query.graphql';
import service from '../services'; import service from '../services';
import { import {
integrationTypesNew, integrationTypesNew,
...@@ -117,16 +118,16 @@ export default { ...@@ -117,16 +118,16 @@ export default {
type: Boolean, type: Boolean,
required: true, required: true,
}, },
currentIntegration: {
type: Object,
required: false,
default: null,
},
canAddIntegration: { canAddIntegration: {
type: Boolean, type: Boolean,
required: true, required: true,
}, },
}, },
apollo: {
currentIntegration: {
query: getCurrentIntegrationQuery,
},
},
data() { data() {
return { return {
selectedIntegration: integrationTypesNew[0].value, selectedIntegration: integrationTypesNew[0].value,
...@@ -140,6 +141,7 @@ export default { ...@@ -140,6 +141,7 @@ export default {
resetSamplePayloadConfirmed: false, resetSamplePayloadConfirmed: false,
customMapping: null, customMapping: null,
parsingPayload: false, parsingPayload: false,
currentIntegration: null,
}; };
}, },
computed: { computed: {
......
...@@ -4,6 +4,7 @@ import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin'; ...@@ -4,6 +4,7 @@ import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { fetchPolicies } from '~/lib/graphql'; import { fetchPolicies } from '~/lib/graphql';
import createFlash, { FLASH_TYPES } from '~/flash'; import createFlash, { FLASH_TYPES } from '~/flash';
import getIntegrationsQuery from '../graphql/queries/get_integrations.query.graphql'; import getIntegrationsQuery from '../graphql/queries/get_integrations.query.graphql';
import getCurrentIntegrationQuery from '../graphql/queries/get_current_integration.query.graphql';
import createHttpIntegrationMutation from '../graphql/mutations/create_http_integration.mutation.graphql'; import createHttpIntegrationMutation from '../graphql/mutations/create_http_integration.mutation.graphql';
import createPrometheusIntegrationMutation from '../graphql/mutations/create_prometheus_integration.mutation.graphql'; import createPrometheusIntegrationMutation from '../graphql/mutations/create_prometheus_integration.mutation.graphql';
import updateHttpIntegrationMutation from '../graphql/mutations/update_http_integration.mutation.graphql'; import updateHttpIntegrationMutation from '../graphql/mutations/update_http_integration.mutation.graphql';
...@@ -11,6 +12,7 @@ import updatePrometheusIntegrationMutation from '../graphql/mutations/update_pro ...@@ -11,6 +12,7 @@ import updatePrometheusIntegrationMutation from '../graphql/mutations/update_pro
import destroyHttpIntegrationMutation from '../graphql/mutations/destroy_http_integration.mutation.graphql'; import destroyHttpIntegrationMutation from '../graphql/mutations/destroy_http_integration.mutation.graphql';
import resetHttpTokenMutation from '../graphql/mutations/reset_http_token.mutation.graphql'; import resetHttpTokenMutation from '../graphql/mutations/reset_http_token.mutation.graphql';
import resetPrometheusTokenMutation from '../graphql/mutations/reset_prometheus_token.mutation.graphql'; import resetPrometheusTokenMutation from '../graphql/mutations/reset_prometheus_token.mutation.graphql';
import updateCurrentIntergrationMutation from '../graphql/mutations/update_current_intergration.mutation.graphql';
import IntegrationsList from './alerts_integrations_list.vue'; import IntegrationsList from './alerts_integrations_list.vue';
import SettingsFormOld from './alerts_settings_form_old.vue'; import SettingsFormOld from './alerts_settings_form_old.vue';
import SettingsFormNew from './alerts_settings_form_new.vue'; import SettingsFormNew from './alerts_settings_form_new.vue';
...@@ -75,6 +77,9 @@ export default { ...@@ -75,6 +77,9 @@ export default {
createFlash({ message: err }); createFlash({ message: err });
}, },
}, },
currentIntegration: {
query: getCurrentIntegrationQuery,
},
}, },
data() { data() {
return { return {
...@@ -87,7 +92,7 @@ export default { ...@@ -87,7 +92,7 @@ export default {
loading() { loading() {
return this.$apollo.queries.integrations.loading; return this.$apollo.queries.integrations.loading;
}, },
intergrationsOptionsOld() { integrationsOptionsOld() {
return [ return [
{ {
name: s__('AlertSettings|HTTP endpoint'), name: s__('AlertSettings|HTTP endpoint'),
...@@ -208,7 +213,19 @@ export default { ...@@ -208,7 +213,19 @@ export default {
}); });
}, },
editIntegration({ id }) { editIntegration({ id }) {
this.currentIntegration = this.integrations.list.find(integration => integration.id === id); const currentIntegration = this.integrations.list.find(integration => integration.id === id);
this.$apollo.mutate({
mutation: updateCurrentIntergrationMutation,
variables: {
id: currentIntegration.id,
name: currentIntegration.name,
active: currentIntegration.active,
token: currentIntegration.token,
type: currentIntegration.type,
url: currentIntegration.url,
apiUrl: currentIntegration.apiUrl,
},
});
}, },
deleteIntegration({ id }) { deleteIntegration({ id }) {
const { projectPath } = this; const { projectPath } = this;
...@@ -229,7 +246,7 @@ export default { ...@@ -229,7 +246,7 @@ export default {
if (error) { if (error) {
return createFlash({ message: error }); return createFlash({ message: error });
} }
this.currentIntegration = null; this.clearCurrentIntegration();
return createFlash({ return createFlash({
message: this.$options.i18n.integrationRemoved, message: this.$options.i18n.integrationRemoved,
type: FLASH_TYPES.SUCCESS, type: FLASH_TYPES.SUCCESS,
...@@ -243,7 +260,10 @@ export default { ...@@ -243,7 +260,10 @@ export default {
}); });
}, },
clearCurrentIntegration() { clearCurrentIntegration() {
this.currentIntegration = null; this.$apollo.mutate({
mutation: updateCurrentIntergrationMutation,
variables: {},
});
}, },
testPayloadFailure() { testPayloadFailure() {
createFlash({ message: INTEGRATION_PAYLOAD_TEST_ERROR }); createFlash({ message: INTEGRATION_PAYLOAD_TEST_ERROR });
...@@ -255,16 +275,14 @@ export default { ...@@ -255,16 +275,14 @@ export default {
<template> <template>
<div> <div>
<integrations-list <integrations-list
:integrations="glFeatures.httpIntegrationsList ? integrations.list : intergrationsOptionsOld" :integrations="glFeatures.httpIntegrationsList ? integrations.list : integrationsOptionsOld"
:loading="loading" :loading="loading"
:current-integration="currentIntegration"
@edit-integration="editIntegration" @edit-integration="editIntegration"
@delete-integration="deleteIntegration" @delete-integration="deleteIntegration"
/> />
<settings-form-new <settings-form-new
v-if="glFeatures.httpIntegrationsList" v-if="glFeatures.httpIntegrationsList"
:loading="isUpdating" :loading="isUpdating"
:current-integration="currentIntegration"
:can-add-integration="canAddIntegration" :can-add-integration="canAddIntegration"
@create-new-integration="createNewIntegration" @create-new-integration="createNewIntegration"
@update-integration="updateIntegration" @update-integration="updateIntegration"
......
import Vue from 'vue';
import produce from 'immer';
import VueApollo from 'vue-apollo';
import createDefaultClient from '~/lib/graphql';
import getCurrentIntegrationQuery from './graphql/queries/get_current_integration.query.graphql';
Vue.use(VueApollo);
const resolvers = {
Mutation: {
updateCurrentIntegration: (
_,
{ id = null, name, active, token, type, url, apiUrl },
{ cache },
) => {
const sourceData = cache.readQuery({ query: getCurrentIntegrationQuery });
const data = produce(sourceData, draftData => {
if (id === null) {
// eslint-disable-next-line no-param-reassign
draftData.currentIntegration = null;
} else {
// eslint-disable-next-line no-param-reassign
draftData.currentIntegration = {
id,
name,
active,
token,
type,
url,
apiUrl,
};
}
});
cache.writeQuery({ query: getCurrentIntegrationQuery, data });
},
},
};
export default new VueApollo({
defaultClient: createDefaultClient(resolvers, {
cacheConfig: {},
assumeImmutableResults: true,
}),
});
mutation updateCurrentIntegration(
$id: String
$name: String
$active: Boolean
$token: String
$type: String
$url: String
$apiUrl: String
) {
updateCurrentIntegration(
id: $id
name: $name
active: $active
token: $token
type: $type
url: $url
apiUrl: $apiUrl
) @client
}
import Vue from 'vue'; import Vue from 'vue';
import VueApollo from 'vue-apollo';
import createDefaultClient from '~/lib/graphql';
import { parseBoolean } from '~/lib/utils/common_utils'; import { parseBoolean } from '~/lib/utils/common_utils';
import AlertSettingsWrapper from './components/alerts_settings_wrapper.vue'; import AlertSettingsWrapper from './components/alerts_settings_wrapper.vue';
import apolloProvider from './graphql';
Vue.use(VueApollo); apolloProvider.clients.defaultClient.cache.writeData({
data: {
currentIntegration: null,
},
});
export default el => { export default el => {
if (!el) { if (!el) {
...@@ -32,15 +35,6 @@ export default el => { ...@@ -32,15 +35,6 @@ export default el => {
multiIntegrations, multiIntegrations,
} = el.dataset; } = el.dataset;
const resolvers = {};
const apolloProvider = new VueApollo({
defaultClient: createDefaultClient(resolvers, {
cacheConfig: {},
assumeImmutableResults: true,
}),
});
return new Vue({ return new Vue({
el, el,
provide: { provide: {
......
...@@ -24,11 +24,14 @@ const mockIntegrations = [ ...@@ -24,11 +24,14 @@ const mockIntegrations = [
describe('AlertIntegrationsList', () => { describe('AlertIntegrationsList', () => {
let wrapper; let wrapper;
function mountComponent(propsData = {}) { function mountComponent({ data = {}, props = {} } = {}) {
wrapper = mount(AlertIntegrationsList, { wrapper = mount(AlertIntegrationsList, {
data() {
return { ...data };
},
propsData: { propsData: {
integrations: mockIntegrations, integrations: mockIntegrations,
...propsData, ...props,
}, },
stubs: { stubs: {
GlIcon: true, GlIcon: true,
...@@ -57,7 +60,7 @@ describe('AlertIntegrationsList', () => { ...@@ -57,7 +60,7 @@ describe('AlertIntegrationsList', () => {
}); });
it('renders an empty state when no integrations provided', () => { it('renders an empty state when no integrations provided', () => {
mountComponent({ integrations: [] }); mountComponent({ props: { integrations: [] } });
expect(findTableComponent().text()).toContain(i18n.emptyState); expect(findTableComponent().text()).toContain(i18n.emptyState);
}); });
...@@ -66,7 +69,7 @@ describe('AlertIntegrationsList', () => { ...@@ -66,7 +69,7 @@ describe('AlertIntegrationsList', () => {
}); });
it('renders an highlighted row when a current integration is selected to edit', () => { it('renders an highlighted row when a current integration is selected to edit', () => {
mountComponent({ currentIntegration: { id: '1' } }); mountComponent({ data: { currentIntegration: { id: '1' } } });
expect( expect(
findTableComponentRows() findTableComponentRows()
.at(0) .at(0)
......
...@@ -152,9 +152,9 @@ describe('AlertsSettingsFormNew', () => { ...@@ -152,9 +152,9 @@ describe('AlertsSettingsFormNew', () => {
createComponent({ createComponent({
data: { data: {
selectedIntegration: typeSet.http, selectedIntegration: typeSet.http,
currentIntegration: { id: '1', name: 'Test integration pre' },
}, },
props: { props: {
currentIntegration: { id: '1', name: 'Test integration pre' },
loading: false, loading: false,
}, },
}); });
...@@ -183,9 +183,9 @@ describe('AlertsSettingsFormNew', () => { ...@@ -183,9 +183,9 @@ describe('AlertsSettingsFormNew', () => {
createComponent({ createComponent({
data: { data: {
selectedIntegration: typeSet.prometheus, selectedIntegration: typeSet.prometheus,
currentIntegration: { id: '1', apiUrl: 'https://test-pre.com' },
}, },
props: { props: {
currentIntegration: { id: '1', apiUrl: 'https://test-pre.com' },
loading: false, loading: false,
}, },
}); });
...@@ -219,10 +219,10 @@ describe('AlertsSettingsFormNew', () => { ...@@ -219,10 +219,10 @@ describe('AlertsSettingsFormNew', () => {
createComponent({ createComponent({
data: { data: {
selectedIntegration: typeSet.http, selectedIntegration: typeSet.http,
currentIntegration: { id: '1', name: 'Test' },
active: true, active: true,
}, },
props: { props: {
currentIntegration: { id: '1', name: 'Test' },
loading: false, loading: false,
}, },
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment