import _difference from 'lodash/difference' import _omit from 'lodash/omit' import { assert } from '../../utils' import { setLabels } from '../pipeline/labels.slice' import { PipelineState, updatePipeline } from '../pipeline/pipeline.slice' import { RelationState, updateRelation } from '../relations/relations.slice' import { Store } from '../store' /** * Update an action pipeline in the store * @param store * @returns */ export const update = (store: Store) => ( relId: string, pipelinePayload: Partial, relationPayload?: Partial ) => { assert(!!relId, 'relId is required') const { relations, pipeline, actions } = 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) assert(!!action, `action for relId=${relId} not found`) assert(!!pl, `pipeline with id=${relId} not found`) store.dispatch(updatePipeline({ id: pl.id, ...pipelinePayload })) // clean up labels const freshState = store.getState() const allProducedKeys = freshState.pipeline.flatMap(pl => Object.keys(pl.produce) ) const allProducedKeysLabels = Object.keys(freshState.labels) const lostLabels = _difference(allProducedKeysLabels, allProducedKeys) if (lostLabels.length) { store.dispatch(setLabels(_omit({ ...freshState.labels }, lostLabels))) } // update a relation if (relationPayload) { store.dispatch( updateRelation({ id: relId, ...relationPayload, }) ) } }