import _mapValues from 'lodash/mapValues' import _omit from 'lodash/omit' import { assert } from '../../utils' import { removeAction } from '../actions/actions.slice' import { setLabels } from '../pipeline/labels.slice' import { removePipeline, updatePipeline } from '../pipeline/pipeline.slice' import { removeRelation, updateRelation } from '../relations/relations.slice' import { Store } from '../store' import { EMPTY_ATTR_GLOBAL_ID } from '../utils/system' import { updateView } from '../views/views.slice' /** * Remove an action from the store * @param store * @returns */ export const remove = (store: Store) => (relId: string) => { assert(!!relId, 'relId is required') const { relations, pipeline, actions, labels, views } = store.getState() const [relation] = relations.filter(({ id }) => id === relId) assert(!!relation, `relation with id=${relId} not found`) const [action] = actions.filter(({ name }) => name === relation.actName) const [pl] = pipeline.filter(({ id }) => id === relId) const [prevRel] = relations.filter(({ rel }) => rel.includes(relId)) const otherActionRelationsCount = relations.filter( ({ actName }) => actName === action.name ).length assert(!!action, `action for relId=${relId} not found`) if (pl) { const attrGlobalIds = Object.keys(pl.produce) // Remove from labels store.dispatch(setLabels(_omit(labels, attrGlobalIds))) // Remove from consume in other blocks for (const ppl of pipeline) { const consume = _mapValues(ppl.consume, (val: string) => attrGlobalIds.includes(val) ? EMPTY_ATTR_GLOBAL_ID : val ) store.dispatch(updatePipeline({ ...ppl, consume })) } // Remove from output blocks for (const view of views) { const elements = { ...view.elements } for (const globalId of attrGlobalIds) { delete elements[globalId] } store.dispatch(updateView({ ...view, elements })) } store.dispatch(removePipeline({ id: relId })) } if (otherActionRelationsCount <= 1) { store.dispatch(removeAction(action)) } if (prevRel) { store.dispatch( updateRelation({ id: prevRel.id, rel: [], }) ) } store.dispatch(removeRelation({ id: relId })) }