Skip to content

Commit

Permalink
feat: add replace by unpusblished concept field in form (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
hegeaal authored Jan 26, 2024
1 parent 5173045 commit b6fe874
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/l10n/helptexts.nb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Se mer om ‘kontaktinformasjon’ i [veileder for begrepsbeskrivelser](https://
assignUserAbstract:
"Velg personen som skal ha ansvaret for å følge opp begrepet.",
replacedByAbstract: "Referanse til begrep som erstatter gjeldende begrep.",
replacedByUnpublishedAbstract: "Referanse til upubliserte begrep i egen katalog som erstatter gjeldende begrep.",
relationsTitle: "Relasjoner",
relationsTitleUnpublished: "Relasjoner - upublisert",
descriptionTitle: "Beskrivelse",
Expand Down
1 change: 1 addition & 0 deletions src/l10n/nb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const nb = {
internalInfo: "Interne opplysninger",
internalSubText: "Opplysningene under er til intern bruk og vil ikke publiseres ut i Felles datakatalog.",
replacedBy: 'Erstattes av',
replacedByNotPublished: 'Erstattes av - upublisert',
relation: 'Relasjon',
relationType: 'Nivå',
description: 'Beskrivelse',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export const schema = Yup.object().shape({
seOgså: Yup.array().of(Yup.string().nullable()).nullable(),
internSeOgså: Yup.array().of(Yup.string().nullable()).nullable(),
erstattesAv: Yup.array().of(Yup.string().nullable()).nullable(),
internErstattesAv: Yup.array().of(Yup.string().nullable()).nullable(),
begrepsRelasjon: Yup.array()
.of(
Yup.object().shape({
Expand Down
5 changes: 4 additions & 1 deletion src/pages/concept-registration-page/form-concept/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export type FormValues = Pick<
| 'seOgså'
| 'internSeOgså'
| 'erstattesAv'
| 'internErstattesAv'
| 'assignedUser'
| 'abbreviatedLabel'
| 'begrepsRelasjon'
Expand Down Expand Up @@ -269,7 +270,7 @@ export const FormConceptPure: FC<Props> = ({
title={localization.formValidity}
showInitially={expandAll}
>
<Validity />
<Validity catalogId={catalogId} />
</FormTemplate>
<FormTemplate
title={localization.formRelatedConcepts}
Expand Down Expand Up @@ -316,6 +317,7 @@ const formikConfig: WithFormikConfig<Props, FormValues> = {
seOgså = [],
internSeOgså = [],
erstattesAv = [],
internErstattesAv = [],
assignedUser = '',
abbreviatedLabel = null,
begrepsRelasjon = [],
Expand Down Expand Up @@ -344,6 +346,7 @@ const formikConfig: WithFormikConfig<Props, FormValues> = {
seOgså,
internSeOgså,
erstattesAv,
internErstattesAv,
assignedUser,
abbreviatedLabel,
begrepsRelasjon,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,32 @@ import { getTranslateText } from '../../../../lib/translateText';
import { useAppDispatch, useAppSelector } from '../../../../app/redux/hooks';
import {
fetchConceptSuggestions,
selectAllConceptSuggestions
fetchInternalConceptSuggestions,
selectAllConceptSuggestions,
selectAllInternalConceptSuggestions
} from '../../../../features/concept-suggestions';
import {
fetchConcepts,
selectAllConceptEntities
fetchInternalConcepts,
selectAllConceptEntities,
selectAllInternalConceptEntities
} from '../../../../features/concepts';

export const Validity: FC = () => {
interface Props {
catalogId: string;
}

export const Validity: FC<Props> = ({ catalogId }) => {
const [gyldigFomField] = useField('gyldigFom');
const [gyldigTomField] = useField('gyldigTom');
const [erstattesAv] = useField('erstattesAv');
const [internErstattesAv] = useField('internErstattesAv');
const dispatch = useAppDispatch();
const relatedConcepts = useAppSelector(selectAllConceptEntities);
const conceptSuggestions = useAppSelector(selectAllConceptSuggestions);
const relatedInternalConcepts = useAppSelector(
selectAllInternalConceptEntities
);

useEffect(() => {
if (erstattesAv.value?.length > 0) {
Expand All @@ -36,10 +48,29 @@ export const Validity: FC = () => {
dispatch(fetchConceptSuggestions({ q }));
};

const executeInternalConceptSuggestionSearch = (
query: string,
publisherId: string
) => {
dispatch(fetchInternalConceptSuggestions({ query, publisherId }));
};

useEffect(() => {
executeConceptSuggestionSearch('');
executeInternalConceptSuggestionSearch('', catalogId);
}, []);

useEffect(() => {
if (internErstattesAv.value?.length > 0) {
dispatch(
fetchInternalConcepts({
catalogId,
values: internErstattesAv.value
})
);
}
}, [internErstattesAv.value]);

const conceptSuggestionsMap = conceptSuggestions.map(
({ identifier, prefLabel, definition, publisher }) =>
({
Expand All @@ -59,6 +90,39 @@ export const Validity: FC = () => {
option?.value && dispatch(fetchConcepts([option.value]));
};

const extractListOfOriginaltBegrepIds = (values: OptionProps[]): string[] => {
const originaltBegreps: string[] = [];

values &&
values.forEach(value => {
const originaltBegrep = value.value;
originaltBegrep && originaltBegreps.push(originaltBegrep);
});
return originaltBegreps;
};

const addRelatedInternalConcept = (form, fieldName, option): void => {
form.setFieldValue(
fieldName,
option.map(item => item.value)
);
const values = extractListOfOriginaltBegrepIds(option);
option?.values && dispatch(fetchInternalConcepts({ catalogId, values }));
};

const internalConceptSuggestions = useAppSelector(
selectAllInternalConceptSuggestions
);

const internalConceptSuggestionsMap = internalConceptSuggestions.map(
({ anbefaltTerm, definisjon, originaltBegrep }) =>
({
value: originaltBegrep,
label: getTranslateText(anbefaltTerm?.navn),
description: getTranslateText(definisjon?.tekst)
} as any)
);

return (
<div>
<div className='form-group'>
Expand Down Expand Up @@ -115,6 +179,37 @@ export const Validity: FC = () => {
)}
/>
</div>
<div className='form-group'>
<HelpText
title={localization.replacedByNotPublished}
helpTextAbstract={localization.replacedByUnpublishedAbstract}
/>
<FieldArray
name='internErstattesAv'
render={({ form }) => (
<Field
name={internErstattesAv.name}
component={SelectField}
placeholder={localization.searchUnpublishedConcepts}
showCustomOption
options={internalConceptSuggestionsMap}
onClear={() => form.setFieldValue(internErstattesAv.name, '')}
onChange={addRelatedInternalConcept}
onInputChange={value =>
executeInternalConceptSuggestionSearch(value, catalogId)
}
defaultValue={form?.values?.internErstattesAv?.map(item => ({
value: item,
label:
getTranslateText(
relatedInternalConcepts[item]?.anbefaltTerm?.navn
) ?? 'default'
}))}
isMulti
/>
)}
/>
</div>
</div>
);
};
1 change: 1 addition & 0 deletions src/types/domain.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export interface Concept {
begrepsRelasjon?: Relasjon[];
internBegrepsRelasjon?: Relasjon[];
interneFelt?: Record<string, InternalField>;
internErstattesAv?: string[];
}

export interface SkosConcept {
Expand Down

0 comments on commit b6fe874

Please sign in to comment.