-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Check for circular references (#13783)
Co-authored-by: Konrad-Simso <konrad.simso@digdir.no>
- Loading branch information
1 parent
951d041
commit 3f0ff04
Showing
10 changed files
with
417 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 26 additions & 2 deletions
28
frontend/packages/schema-editor/src/components/SchemaEditor/hooks/useAddReference.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,49 @@ | ||
import type { HandleAdd, ItemPosition } from 'app-shared/types/dndTypes'; | ||
import { useCallback } from 'react'; | ||
import type { NodePosition } from '@altinn/schema-model'; | ||
import { SchemaModel } from '@altinn/schema-model'; | ||
import { createDefinitionPointer, SchemaModel } from '@altinn/schema-model'; | ||
|
||
import { calculatePositionInFullList } from '../utils'; | ||
import { useSavableSchemaModel } from '../../../hooks/useSavableSchemaModel'; | ||
import { useSchemaEditorAppContext } from '@altinn/schema-editor/hooks/useSchemaEditorAppContext'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
export const useAddReference = (): HandleAdd<string> => { | ||
const { setSelectedUniquePointer } = useSchemaEditorAppContext(); | ||
const savableModel = useSavableSchemaModel(); | ||
const circularReferenceError = useCircularReferenceError(); | ||
const circularReferenceAlert = useCircularReferenceAlert(); | ||
return useCallback( | ||
(reference: string, position: ItemPosition) => { | ||
const index = calculatePositionInFullList(savableModel, position); | ||
const parentPointer = savableModel.getSchemaPointerByUniquePointer(position.parentId); | ||
if (circularReferenceError(reference, parentPointer)) { | ||
circularReferenceAlert(); | ||
return; | ||
} | ||
const target: NodePosition = { parentPointer, index }; | ||
const { schemaPointer } = savableModel.getFinalNode(target.parentPointer); | ||
const refName = savableModel.generateUniqueChildName(schemaPointer, 'ref'); | ||
const ref = savableModel.addReference(refName, reference, target); | ||
const uniquePointer = SchemaModel.getUniquePointer(ref.schemaPointer); | ||
setSelectedUniquePointer(uniquePointer); | ||
}, | ||
[savableModel, setSelectedUniquePointer], | ||
[savableModel, setSelectedUniquePointer, circularReferenceAlert, circularReferenceError], | ||
); | ||
}; | ||
|
||
function useCircularReferenceError(): (reference: string, targetSchemaPointer: string) => boolean { | ||
const savableModel = useSavableSchemaModel(); | ||
return useCallback( | ||
(reference: string, targetSchemaPointer: string) => { | ||
const referencePointer = createDefinitionPointer(reference); | ||
return savableModel.willResultInCircularReferences(referencePointer, targetSchemaPointer); | ||
}, | ||
[savableModel], | ||
); | ||
} | ||
|
||
function useCircularReferenceAlert(): () => void { | ||
const { t } = useTranslation(); | ||
return useCallback(() => alert(t('schema_editor.error_circular_references')), [t]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.