Skip to content

Commit

Permalink
feat: rename resource service
Browse files Browse the repository at this point in the history
  • Loading branch information
devcatalin committed Sep 10, 2021
1 parent 16af394 commit b7c7a78
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/redux/services/renameResource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {stringify} from 'yaml';
import {ResourceMapType} from '@models/appstate';
import {updateResource} from '@redux/reducers/main';
import {AppDispatch} from '@redux/store';
import {isIncomingRef} from './resourceRefs';

export const renameResource = (
resourceId: string,
newResourceName: string,
shouldUpdateRefs: boolean,
resourceMap: ResourceMapType,
dispatch: AppDispatch
) => {
const resource = resourceMap[resourceId];
if (!resource || !resource.content) {
return;
}
const newResourceContent = {
...resource.content,
metadata: {
...(resource.content.metadata || {}),
name: newResourceName,
},
};
const newResourceText = stringify(newResourceContent);
dispatch(updateResource({resourceId, content: newResourceText}));
if (shouldUpdateRefs && resource.refs) {
resource.refs.forEach(ref => {
if (!isIncomingRef(ref.type) || !ref.targetResourceId) {
return;
}
const dependentResource = resourceMap[ref.targetResourceId];
if (!dependentResource || !dependentResource.refs) {
return;
}
let newDependentResourceText = '';
dependentResource.text.split('\n').forEach((line, lineIndex) => {
const refAtCurrentLine = dependentResource.refs?.find(depRef => depRef.position?.line === lineIndex);
if (!refAtCurrentLine) {
newDependentResourceText += `${line}\n`;
return;
}
newDependentResourceText += `${line.replace(ref.name, newResourceName)}\n`;
});
dispatch(updateResource({resourceId: dependentResource.id, content: newDependentResourceText}));
});
}
};

0 comments on commit b7c7a78

Please sign in to comment.