Skip to content

Commit

Permalink
feat: add unpublished relation
Browse files Browse the repository at this point in the history
  • Loading branch information
hegeaal committed Jan 18, 2024
1 parent 5c02411 commit 821f342
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 75 deletions.
8 changes: 5 additions & 3 deletions src/features/concept-suggestions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface SuggestionsAttributes {
publisherId?: string;
}
interface InternalSuggestionsAttributes {
q: string;
query: string;
publisherId: string;
}

Expand All @@ -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<SkosConcept>({
Expand Down
2 changes: 2 additions & 0 deletions src/l10n/helptexts.nb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/pages/concept-registration-page/form-concept/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export type FormValues = Pick<
| 'assignedUser'
| 'abbreviatedLabel'
| 'begrepsRelasjon'
| 'internBegrepsRelasjon'
| 'versjonsnr'
| 'interneFelt'
>;
Expand Down Expand Up @@ -318,6 +319,7 @@ const formikConfig: WithFormikConfig<Props, FormValues> = {
assignedUser = '',
abbreviatedLabel = null,
begrepsRelasjon = [],
internBegrepsRelasjon = [],
versjonsnr = { major: 0, minor: 0, patch: 0 },
interneFelt = {}
}
Expand Down Expand Up @@ -345,6 +347,7 @@ const formikConfig: WithFormikConfig<Props, FormValues> = {
assignedUser,
abbreviatedLabel,
begrepsRelasjon,
internBegrepsRelasjon,
versjonsnr,
interneFelt
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {
} from '../../../../../../app/redux/hooks';
import {
fetchConcepts,
selectAllConceptEntities
fetchInternalConcepts,
selectAllConceptEntities,
selectAllInternalConceptEntities
} from '../../../../../../features/concepts';
import {
SelectField,
Expand Down Expand Up @@ -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<Props> = ({
index,
fieldName,
relation,
catalogId,
languages,
isReadOnly,
conceptSuggestionsMap,
executeConceptSuggestionSearch
executeConceptSuggestionSearch,
executeInternalConceptSuggestionSearch
}) => {
const [filterSuggestionsByCatalogId, setFilterSuggestionsByCatalogId] =
useState<boolean>(false);
const [field] = useField('begrepsRelasjon');
const [fieldValue] = useField(fieldName);
const relatedConcepts = useAppSelector(selectAllConceptEntities);
const relatedInternalConcepts = useAppSelector(
selectAllInternalConceptEntities
);

const dispatch = useAppDispatch();

Expand All @@ -87,68 +102,77 @@ const RelationItem: FC<Props> = ({
}
}, []);

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 (
<>
<SC.SelectButtons>
<Field
name={`begrepsRelasjon[${index}].relasjon`}
name={`${fieldName}[${index}].relasjon`}
component={SelectField}
label={localization.relation}
showLabel
showRequired
options={relationOptions}
onClear={form => {
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) && (
<Field
name={`begrepsRelasjon[${index}].relasjonsType`}
name={`${fieldName}[${index}].relasjonsType`}
component={SelectField}
label={localization.relationType}
showLabel
Expand All @@ -157,16 +181,13 @@ const RelationItem: FC<Props> = ({
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
)}
/>
)}
Expand All @@ -178,9 +199,9 @@ const RelationItem: FC<Props> = ({
showRequired={!isReadOnly}
/>
<MultilingualField
name={`begrepsRelasjon[${index}].beskrivelse`}
name={`${fieldName}[${index}].beskrivelse`}
component={InputField}
label={`begrepsRelasjon[${index}].beskrivelse`}
label={`${fieldName}[${index}].beskrivelse`}
languages={languages}
/>
</div>
Expand All @@ -190,16 +211,16 @@ const RelationItem: FC<Props> = ({
<div className='form-group'>
<HelpText title={localization.inndelingskriteriumTitle} />
<MultilingualField
name={`begrepsRelasjon[${index}].inndelingskriterium`}
name={`${fieldName}[${index}].inndelingskriterium`}
component={InputField}
label={`begrepsRelasjon[${index}].inndelingskriterium`}
label={`${fieldName}[${index}].inndelingskriterium`}
languages={languages}
/>
</div>
)}
<div className='form-group'>
<Field
name={`begrepsRelasjon[${index}].relatertBegrep`}
name={`${fieldName}[${index}].relatertBegrep`}
component={SelectField}
placeholder={localization.searchConcepts}
label={localization.relatedConcept}
Expand All @@ -208,20 +229,27 @@ const RelationItem: FC<Props> = ({
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()
}
}
/>
Expand Down
Loading

0 comments on commit 821f342

Please sign in to comment.