From 821f34236530e5f4c8893c1e87ae22e808ab0599 Mon Sep 17 00:00:00 2001 From: Hege Aalvik Date: Wed, 17 Jan 2024 15:30:00 +0100 Subject: [PATCH] feat: add unpublished relation --- src/features/concept-suggestions/index.ts | 8 +- src/l10n/helptexts.nb.ts | 2 + .../form-concept/index.tsx | 3 + .../components/relation/index.tsx | 122 +++++++++++------- .../components/relations/index.tsx | 67 ++++++---- .../related-concepts.component.tsx | 21 ++- src/types/domain.d.ts | 1 + 7 files changed, 149 insertions(+), 75 deletions(-) diff --git a/src/features/concept-suggestions/index.ts b/src/features/concept-suggestions/index.ts index 3d97e86c..75dd078a 100644 --- a/src/features/concept-suggestions/index.ts +++ b/src/features/concept-suggestions/index.ts @@ -17,7 +17,7 @@ interface SuggestionsAttributes { publisherId?: string; } interface InternalSuggestionsAttributes { - q: string; + query: string; publisherId: string; } @@ -31,8 +31,10 @@ export const fetchConceptSuggestions = createAsyncThunk< export const fetchInternalConceptSuggestions = createAsyncThunk< Concept[], InternalSuggestionsAttributes ->('conceptForm/fetchInternalConceptSuggestions', async ({ q, publisherId }) => - getInternalConceptSuggestions(publisherId, q) +>( + 'conceptForm/fetchInternalConceptSuggestions', + async ({ query, publisherId }) => + getInternalConceptSuggestions(publisherId, query) ); const conceptSuggestionsAdapter = createEntityAdapter({ diff --git a/src/l10n/helptexts.nb.ts b/src/l10n/helptexts.nb.ts index 2df86a59..1ec89bd8 100644 --- a/src/l10n/helptexts.nb.ts +++ b/src/l10n/helptexts.nb.ts @@ -138,11 +138,13 @@ Se mer om ‘kontaktinformasjon’ i [veileder for begrepsbeskrivelser](https:// seOgsaaTitle: "Se også", seOgsaaAbstract: "Referanse til et beslektet begrep.", seOgsaaUpublisertAbstract: "Referanse til et beslektet upublisert bergrep i egen katalog.", + internalRelationAbstract: 'Relasjon til et upublisert begrep i egen katalog.', assignUserTitle: "Hvem skal begrepet tildeles?", assignUserAbstract: "Velg personen som skal ha ansvaret for å følge opp begrepet.", replacedByAbstract: "Referanse til begrep som erstatter gjeldende begrep.", relationsTitle: "Relasjoner", + relationsTitleUnpublished: "Relasjoner - upublisert", descriptionTitle: "Beskrivelse", inndelingskriteriumTitle: "Inndelingskriterium", versionTitle: "Versjonsnummer", diff --git a/src/pages/concept-registration-page/form-concept/index.tsx b/src/pages/concept-registration-page/form-concept/index.tsx index 8fe17c8c..7704da7b 100644 --- a/src/pages/concept-registration-page/form-concept/index.tsx +++ b/src/pages/concept-registration-page/form-concept/index.tsx @@ -82,6 +82,7 @@ export type FormValues = Pick< | 'assignedUser' | 'abbreviatedLabel' | 'begrepsRelasjon' + | 'internBegrepsRelasjon' | 'versjonsnr' | 'interneFelt' >; @@ -318,6 +319,7 @@ const formikConfig: WithFormikConfig = { assignedUser = '', abbreviatedLabel = null, begrepsRelasjon = [], + internBegrepsRelasjon = [], versjonsnr = { major: 0, minor: 0, patch: 0 }, interneFelt = {} } @@ -345,6 +347,7 @@ const formikConfig: WithFormikConfig = { assignedUser, abbreviatedLabel, begrepsRelasjon, + internBegrepsRelasjon, versjonsnr, interneFelt }), diff --git a/src/pages/concept-registration-page/form-concept/related-concepts/components/relation/index.tsx b/src/pages/concept-registration-page/form-concept/related-concepts/components/relation/index.tsx index 1e637661..0c4008d7 100644 --- a/src/pages/concept-registration-page/form-concept/related-concepts/components/relation/index.tsx +++ b/src/pages/concept-registration-page/form-concept/related-concepts/components/relation/index.tsx @@ -12,7 +12,9 @@ import { } from '../../../../../../app/redux/hooks'; import { fetchConcepts, - selectAllConceptEntities + fetchInternalConcepts, + selectAllConceptEntities, + selectAllInternalConceptEntities } from '../../../../../../features/concepts'; import { SelectField, @@ -59,22 +61,35 @@ interface Props { languages: Language[]; isReadOnly: boolean; conceptSuggestionsMap?: OptionProps[]; - executeConceptSuggestionSearch: (query: string, publisherId?: string) => void; + executeConceptSuggestionSearch?: ( + query: string, + publisherId?: string + ) => void; + executeInternalConceptSuggestionSearch?: ( + query: string, + publisherId: string + ) => void; + fieldName: 'begrepsRelasjon' | 'internBegrepsRelasjon'; } const RelationItem: FC = ({ index, + fieldName, relation, catalogId, languages, isReadOnly, conceptSuggestionsMap, - executeConceptSuggestionSearch + executeConceptSuggestionSearch, + executeInternalConceptSuggestionSearch }) => { const [filterSuggestionsByCatalogId, setFilterSuggestionsByCatalogId] = useState(false); - const [field] = useField('begrepsRelasjon'); + const [fieldValue] = useField(fieldName); const relatedConcepts = useAppSelector(selectAllConceptEntities); + const relatedInternalConcepts = useAppSelector( + selectAllInternalConceptEntities + ); const dispatch = useAppDispatch(); @@ -87,68 +102,77 @@ const RelationItem: FC = ({ } }, []); - const addRelatedConcept = (form, fieldName, option): void => { - form.setFieldValue(fieldName, option?.value ?? null); - option?.value && dispatch(fetchConcepts([option.value])); + const addRelatedConcept = (form, field, option): void => { + form.setFieldValue(field, option?.value ?? null); + const values = [option.value]; + option?.value && fieldName === 'begrepsRelasjon' + ? dispatch(fetchConcepts(values)) + : dispatch(fetchInternalConcepts({ catalogId, values })); }; const clearTypeRelatedFields = form => { - form.setFieldValue(`begrepsRelasjon[${index}].relasjonsType`); + form.setFieldValue(`${fieldName}[${index}].relasjonsType`); form.setFieldValue( - `begrepsRelasjon[${index}].beskrivelse`, + `${fieldName}[${index}].beskrivelse`, { nb: '', nn: '', en: '' }, false ); form.setFieldValue( - `begrepsRelasjon[${index}].inndelingskriterium`, + `${fieldName}[${index}].inndelingskriterium`, { nb: '', nn: '', en: '' }, false ); }; - const handleChangeOptionValue = (form, fieldName, option) => { + const handleChangeOptionValue = (form, field, option) => { if ( option?.value === RelationType.OVERORDNET || option?.value === RelationType.OMFATTER ) { setFilterSuggestionsByCatalogId(true); - form.setFieldValue( - `begrepsRelasjon[${index}].relatertBegrep`, - null, - false - ); + form.setFieldValue(`${fieldName}[${index}].relatertBegrep`, null, false); } else { setFilterSuggestionsByCatalogId(false); } - form.setFieldValue(fieldName, option?.value ?? null); + form.setFieldValue(field, option?.value ?? null); }; + const getLabel = () => + fieldName === 'begrepsRelasjon' + ? getTranslateText( + relatedConcepts[fieldValue.value[index].relatertBegrep]?.prefLabel + ) || 'default' + : getTranslateText( + relatedInternalConcepts[fieldValue.value[index].relatertBegrep] + ?.anbefaltTerm?.navn + ) || 'default'; + return ( <> { - form.setFieldValue(`begrepsRelasjon[${index}].relasjon`, null); - form.setFieldValue(`begrepsRelasjon[${index}].relasjonsType`, null); + form.setFieldValue(`${fieldName}[${index}].relasjon`, null); + form.setFieldValue(`${fieldName}[${index}].relasjonsType`, null); }} - onChange={(form, fieldName, option) => { + onChange={(form, field, option) => { clearTypeRelatedFields(form); - handleChangeOptionValue(form, fieldName, option); + handleChangeOptionValue(form, field, option); }} defaultValue={relationOptions.find( - option => option.value === field.value[index].relasjon + option => option.value === fieldValue.value[index].relasjon )} /> {(relation.relasjon === Relation.PARTITIV || relation.relasjon === Relation.GENERISK) && ( = ({ option => option.filterByParent === relation.relasjon )} onClear={form => - form.setFieldValue( - `begrepsRelasjon[${index}].relasjonsType`, - null - ) + form.setFieldValue(`${fieldName}[${index}].relasjonsType`, null) } - onChange={(form, fieldName, option) => { - handleChangeOptionValue(form, fieldName, option); + onChange={(form, field, option) => { + handleChangeOptionValue(form, field, option); }} defaultValue={relationTypeOptions.find( - option => option.value === field.value[index].relasjonsType + option => option.value === fieldValue.value[index].relasjonsType )} /> )} @@ -178,9 +199,9 @@ const RelationItem: FC = ({ showRequired={!isReadOnly} /> @@ -190,16 +211,16 @@ const RelationItem: FC = ({
)}
= ({ showCustomOption options={conceptSuggestionsMap} onClear={form => - form.setFieldValue(`begrepsRelasjon[${index}].relatertBegrep`, '') + form.setFieldValue(`${fieldName}[${index}].relatertBegrep`, '') } onChange={addRelatedConcept} - onInputChange={query => - filterSuggestionsByCatalogId - ? executeConceptSuggestionSearch(query, catalogId) - : executeConceptSuggestionSearch(query) - } + onInputChange={query => { + if (fieldName === 'begrepsRelasjon') { + if (filterSuggestionsByCatalogId) { + executeConceptSuggestionSearch && + executeConceptSuggestionSearch(query, catalogId); + } else { + executeConceptSuggestionSearch && + executeConceptSuggestionSearch(query); + } + } else { + executeInternalConceptSuggestionSearch && + executeInternalConceptSuggestionSearch(query, catalogId); + } + }} defaultValue={ - field.value[index].relatertBegrep && { - value: field.value[index].relatertBegrep, - label: getTranslateText( - relatedConcepts[field.value[index].relatertBegrep]?.prefLabel - ) + fieldValue.value[index].relatertBegrep && { + value: fieldValue.value[index].relatertBegrep, + label: getLabel() } } /> diff --git a/src/pages/concept-registration-page/form-concept/related-concepts/components/relations/index.tsx b/src/pages/concept-registration-page/form-concept/related-concepts/components/relations/index.tsx index 2099e431..d56b3e50 100644 --- a/src/pages/concept-registration-page/form-concept/related-concepts/components/relations/index.tsx +++ b/src/pages/concept-registration-page/form-concept/related-concepts/components/relations/index.tsx @@ -9,53 +9,72 @@ import { useAppSelector, useAppDispatch } from '../../../../../../app/redux/hooks'; -import { fetchConcepts } from '../../../../../../features/concepts'; +import { + fetchConcepts, + fetchInternalConcepts +} from '../../../../../../features/concepts'; import { ButtonSource } from '../../../../../../components/button-source/button-source.component'; import { OptionProps } from '../../../../../../components/fields/field-select/field-select.component'; import RelationItem from '../relation'; import SC from './styled'; -const getRelations = form => form?.values?.begrepsRelasjon ?? []; - -const handleAddRelation = (push: any) => { - push({} as Relasjon); -}; - -const handleRemoveRelation = (form, index) => { - const { begrepsRelasjon } = form.values; - if (Array.isArray(begrepsRelasjon)) { - const removeElement = begrepsRelasjon?.[index]; - const updated = begrepsRelasjon?.filter(v => v !== removeElement); - form.setFieldValue('begrepsRelasjon', updated); - } -}; +const getPublishedRelations = form => form?.values?.begrepsRelasjon ?? []; +const getInternalRelations = form => form?.values?.internBegrepsRelasjon ?? []; interface Props { catalogId: string; + fieldName: 'begrepsRelasjon' | 'internBegrepsRelasjon'; languages: Language[]; isReadOnly: boolean; conceptSuggestionsMap?: OptionProps[]; - executeConceptSuggestionSearch: (query: string, publisherId?: string) => void; + executeConceptSuggestionSearch?: ( + query: string, + publisherId: string | undefined + ) => void; + executeInternalConceptSuggestionSearch?: ( + query: string, + publisherId: string + ) => void; } const Relations: FC = ({ catalogId, + fieldName, languages, isReadOnly, conceptSuggestionsMap, - executeConceptSuggestionSearch + executeConceptSuggestionSearch, + executeInternalConceptSuggestionSearch }) => { - const [field] = useField('begrepsRelasjon'); + const [field] = useField(fieldName); const conceptForm = useAppSelector(state => state.conceptForm); const relations = field.value ?? []; const dispatch = useAppDispatch(); - const idS = relations.map(s => s.relatertBegrep).filter(Boolean); + const values = relations.map(s => s.relatertBegrep).filter(Boolean); + const getRelations = + fieldName === 'begrepsRelasjon' + ? getPublishedRelations + : getInternalRelations; + const handleAddRelation = (push: any) => { + push({} as Relasjon); + }; + + const handleRemoveRelation = (form, index) => { + const { begrepsRelasjon } = form.values; + if (Array.isArray(begrepsRelasjon)) { + const removeElement = begrepsRelasjon?.[index]; + const updated = begrepsRelasjon?.filter(v => v !== removeElement); + form.setFieldValue(fieldName, updated); + } + }; useEffect(() => { - if (idS?.length > 0) { - dispatch(fetchConcepts(idS)); + if (values?.length > 0) { + fieldName === 'begrepsRelasjon' + ? dispatch(fetchConcepts(values)) + : dispatch(fetchInternalConcepts({ catalogId, values })); } }, []); @@ -63,13 +82,14 @@ const Relations: FC = ({ {relations && ( (
{getRelations(form).map((relation, index) => ( = ({ executeConceptSuggestionSearch={ executeConceptSuggestionSearch } + executeInternalConceptSuggestionSearch={ + executeInternalConceptSuggestionSearch + } />
= ({ }) ); } - }, [seOgsaaField]); + }, [internSeOgsaaField]); const executeConceptSuggestionSearch = (q: string, publisherId?: string) => { dispatch(fetchConceptSuggestions({ q, publisherId })); }; const executeInternalConceptSuggestionSearch = ( - q: string, + query: string, publisherId: string ) => { - dispatch(fetchInternalConceptSuggestions({ q, publisherId })); + dispatch(fetchInternalConceptSuggestions({ query, publisherId })); }; useEffect(() => { @@ -152,12 +152,27 @@ const RelatedConceptsPure: FC = ({ + + ; }