2020-11-06 18:09:07 +00:00
|
|
|
import produce from 'immer';
|
2022-09-28 00:08:50 +00:00
|
|
|
import { createAlert } from '~/flash';
|
2020-11-06 18:09:07 +00:00
|
|
|
|
|
|
|
import { DELETE_INTEGRATION_ERROR, ADD_INTEGRATION_ERROR } from './error_messages';
|
|
|
|
|
|
|
|
const deleteIntegrationFromStore = (store, query, { httpIntegrationDestroy }, variables) => {
|
|
|
|
const integration = httpIntegrationDestroy?.integration;
|
|
|
|
if (!integration) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sourceData = store.readQuery({
|
|
|
|
query,
|
|
|
|
variables,
|
|
|
|
});
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
const data = produce(sourceData, (draftData) => {
|
2020-11-06 18:09:07 +00:00
|
|
|
draftData.project.alertManagementIntegrations.nodes = draftData.project.alertManagementIntegrations.nodes.filter(
|
|
|
|
({ id }) => id !== integration.id,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
store.writeQuery({
|
|
|
|
query,
|
|
|
|
variables,
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const addIntegrationToStore = (
|
|
|
|
store,
|
|
|
|
query,
|
|
|
|
{ httpIntegrationCreate, prometheusIntegrationCreate },
|
|
|
|
variables,
|
|
|
|
) => {
|
|
|
|
const integration =
|
|
|
|
httpIntegrationCreate?.integration || prometheusIntegrationCreate?.integration;
|
|
|
|
if (!integration) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sourceData = store.readQuery({
|
|
|
|
query,
|
|
|
|
variables,
|
|
|
|
});
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
const data = produce(sourceData, (draftData) => {
|
2020-11-06 18:09:07 +00:00
|
|
|
draftData.project.alertManagementIntegrations.nodes = [
|
|
|
|
integration,
|
|
|
|
...draftData.project.alertManagementIntegrations.nodes,
|
|
|
|
];
|
|
|
|
});
|
|
|
|
|
|
|
|
store.writeQuery({
|
|
|
|
query,
|
|
|
|
variables,
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onError = (data, message) => {
|
2022-09-28 00:08:50 +00:00
|
|
|
createAlert({ message });
|
2020-11-06 18:09:07 +00:00
|
|
|
throw new Error(data.errors);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const hasErrors = ({ errors = [] }) => errors?.length;
|
|
|
|
|
|
|
|
export const updateStoreAfterIntegrationDelete = (store, query, data, variables) => {
|
|
|
|
if (hasErrors(data)) {
|
|
|
|
onError(data, DELETE_INTEGRATION_ERROR);
|
|
|
|
} else {
|
|
|
|
deleteIntegrationFromStore(store, query, data, variables);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const updateStoreAfterIntegrationAdd = (store, query, data, variables) => {
|
|
|
|
if (hasErrors(data)) {
|
|
|
|
onError(data, ADD_INTEGRATION_ERROR);
|
|
|
|
} else {
|
|
|
|
addIntegrationToStore(store, query, data, variables);
|
|
|
|
}
|
|
|
|
};
|