debian-mirror-gitlab/app/assets/javascripts/vue_shared/alert_details/index.js

85 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-01-03 14:25:43 +05:30
import { defaultDataIdFromObject } from 'apollo-cache-inmemory';
import produce from 'immer';
2020-05-24 23:13:21 +05:30
import Vue from 'vue';
import VueApollo from 'vue-apollo';
2020-10-24 23:57:45 +05:30
import createDefaultClient from '~/lib/graphql';
2020-05-24 23:13:21 +05:30
import AlertDetails from './components/alert_details.vue';
2021-03-11 19:13:27 +05:30
import { PAGE_CONFIG } from './constants';
import sidebarStatusQuery from './graphql/queries/alert_sidebar_status.query.graphql';
2021-01-03 14:25:43 +05:30
import createRouter from './router';
2020-05-24 23:13:21 +05:30
Vue.use(VueApollo);
2021-03-08 18:12:59 +05:30
export default (selector) => {
2020-05-24 23:13:21 +05:30
const domEl = document.querySelector(selector);
2021-03-11 19:13:27 +05:30
const { alertId, projectPath, projectIssuesPath, projectId, page } = domEl.dataset;
2020-10-24 23:57:45 +05:30
const router = createRouter();
2020-07-28 23:09:34 +05:30
const resolvers = {
Mutation: {
toggleSidebarStatus: (_, __, { cache }) => {
2020-11-24 15:15:51 +05:30
const sourceData = cache.readQuery({ query: sidebarStatusQuery });
2021-03-08 18:12:59 +05:30
const data = produce(sourceData, (draftData) => {
2020-11-24 15:15:51 +05:30
draftData.sidebarStatus = !draftData.sidebarStatus;
});
2020-07-28 23:09:34 +05:30
cache.writeQuery({ query: sidebarStatusQuery, data });
},
},
};
2020-05-24 23:13:21 +05:30
const apolloProvider = new VueApollo({
2020-07-28 23:09:34 +05:30
defaultClient: createDefaultClient(resolvers, {
cacheConfig: {
2021-03-08 18:12:59 +05:30
dataIdFromObject: (object) => {
2020-07-28 23:09:34 +05:30
// eslint-disable-next-line no-underscore-dangle
if (object.__typename === 'AlertManagementAlert') {
return object.iid;
}
return defaultDataIdFromObject(object);
2020-05-24 23:13:21 +05:30
},
},
2020-11-24 15:15:51 +05:30
assumeImmutableResults: true,
2020-07-28 23:09:34 +05:30
}),
});
2021-04-29 21:17:54 +05:30
apolloProvider.clients.defaultClient.cache.writeQuery({
query: sidebarStatusQuery,
2020-07-28 23:09:34 +05:30
data: {
sidebarStatus: false,
},
2020-05-24 23:13:21 +05:30
});
2021-03-11 19:13:27 +05:30
const provide = {
projectPath,
alertId,
page,
projectIssuesPath,
projectId,
2021-04-29 21:17:54 +05:30
statuses: PAGE_CONFIG[page].STATUSES,
2021-03-11 19:13:27 +05:30
};
if (page === PAGE_CONFIG.OPERATIONS.TITLE) {
const { TRACK_ALERTS_DETAILS_VIEWS_OPTIONS, TRACK_ALERT_STATUS_UPDATE_OPTIONS } = PAGE_CONFIG[
page
];
provide.trackAlertsDetailsViewsOptions = TRACK_ALERTS_DETAILS_VIEWS_OPTIONS;
provide.trackAlertStatusUpdateOptions = TRACK_ALERT_STATUS_UPDATE_OPTIONS;
} else if (page === PAGE_CONFIG.THREAT_MONITORING.TITLE) {
provide.isThreatMonitoringPage = true;
}
2020-05-24 23:13:21 +05:30
// eslint-disable-next-line no-new
new Vue({
el: selector,
2021-03-08 18:12:59 +05:30
components: {
AlertDetails,
},
2021-03-11 19:13:27 +05:30
provide,
2020-05-24 23:13:21 +05:30
apolloProvider,
2020-10-24 23:57:45 +05:30
router,
2020-05-24 23:13:21 +05:30
render(createElement) {
2020-07-28 23:09:34 +05:30
return createElement('alert-details', {});
2020-05-24 23:13:21 +05:30
},
});
};