Skip to content

Commit

Permalink
feat: add mulitlanguage keyword support dataset form
Browse files Browse the repository at this point in the history
  • Loading branch information
hegeaal committed Jan 3, 2025
1 parent 67e3665 commit b50ef8e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
'use client';
import { Dataset } from '@catalog-frontend/types';
import { Textfield, Chip, Button, Combobox } from '@digdir/designsystemet-react';
import { Combobox } from '@digdir/designsystemet-react';
import { useFormikContext } from 'formik';
import { useState } from 'react';
import { capitalizeFirstLetter, getTranslateText, localization } from '@catalog-frontend/utils';
import styles from '../dataset-form.module.css';
import { useSearchConceptsByUri, useSearchConceptSuggestions } from '../../../hooks/useSearchService';
import { TitleWithTag } from '@catalog-frontend/ui';
import classNames from 'classnames';
import { FormikLanguageFieldset, TitleWithTag } from '@catalog-frontend/ui';

interface Props {
searchEnv: string; // Environment variable to search service
}

export const ConceptSection = ({ searchEnv }: Props) => {
const { setFieldValue, values } = useFormikContext<Dataset>();
const [inputValue, setInputValue] = useState<string>('');
const [searchQuery, setSearchQuery] = useState<string>('');

const { data: searchHits, isLoading: searching } = useSearchConceptSuggestions(searchEnv, searchQuery);
Expand All @@ -42,22 +40,9 @@ export const ConceptSection = ({ searchEnv }: Props) => {
).values(),
];

const addKeyword = () => {
if (inputValue && !values.keywordList?.nb?.includes(inputValue)) {
const updatedKeywords = [...(values.keywordList?.nb || []), inputValue];
setFieldValue('keywordList.nb', updatedKeywords);
setInputValue('');
}
};

const removeKeyword = (keyword: string) => {
const updatedKeywords = values.keywordList?.nb?.filter((item: string) => item !== keyword) || [];
setFieldValue('keywordList.nb', updatedKeywords);
};

return (
<>
<div className={styles.fieldContainer}>
<>
<TitleWithTag
title={localization.datasetForm.fieldLabel.concept}
tagTitle={localization.tag.recommended}
Expand Down Expand Up @@ -92,48 +77,19 @@ export const ConceptSection = ({ searchEnv }: Props) => {
</Combobox.Option>
))}
</Combobox>
</div>
</>

<div className={classNames(styles.set, styles.fullWidth)}>
<div className={styles.fullWidth}>
<Textfield
size='sm'
label={
<TitleWithTag
title={localization.datasetForm.fieldLabel.keyword}
tagTitle={localization.tag.recommended}
tagColor='info'
/>
}
type='text'
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
e.preventDefault();
addKeyword();
}
}}
<FormikLanguageFieldset
multiple
name={'keywordList'}
legend={
<TitleWithTag
title={localization.datasetForm.fieldLabel.keyword}
tagTitle={localization.tag.recommended}
tagColor='info'
/>
</div>
<div className={styles.tagsButton}>
<Button
size='sm'
onClick={addKeyword}
variant='secondary'
>{`${localization.add}...`}</Button>
</div>
</div>
<Chip.Group size='sm'>
{values.keywordList?.nb?.map((value: string) => (
<Chip.Removable
key={value}
onClick={() => removeKeyword(value)}
>
{value}
</Chip.Removable>
))}
</Chip.Group>
}
/>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const datasetTemplate = (dataset: Dataset): Dataset => {
: [],
type: dataset?.type,
keywordList: dataset.keyword ? groupByKeys(dataset.keyword) : { nb: [] },
keyword: dataset.keyword ?? [{ nb: '' }],
conceptList: dataset.concepts ? dataset.concepts.map((concept) => concept.uri) : [],
provenance: dataset?.provenance?.uri ? { uri: dataset?.provenance?.uri } : undefined,
accrualPeriodicity: dataset?.accrualPeriodicity?.uri ? { uri: dataset?.accrualPeriodicity?.uri } : undefined,
Expand Down Expand Up @@ -78,6 +79,7 @@ export const datasetToBeCreatedTemplate = (): DatasetToBeCreated => {
euThemeList: [],
type: undefined,
keywordList: { nb: [] },
keyword: [{ nb: '' }],
conceptList: [],
provenance: undefined,
accrualPeriodicity: undefined,
Expand Down
2 changes: 1 addition & 1 deletion libs/types/src/lib/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type DatasetToBeCreated = {
landingPage?: string[];
theme?: { uri: string }[];
type?: string;
keyword?: LocalizedStrings[];
keyword?: { [key: string]: string }[];
keywordList?: { nb?: string[]; nn?: string[]; en?: string[] };
concepts?: [{ uri: string }];
provenance?: ReferenceDataCode;
Expand Down

0 comments on commit b50ef8e

Please sign in to comment.