Skip to content

Commit

Permalink
feat: add main languages field to dataset form
Browse files Browse the repository at this point in the history
  • Loading branch information
hegeaal committed Oct 25, 2024
1 parent 823104c commit c13900a
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 8 deletions.
2 changes: 2 additions & 0 deletions apps/dataset-catalog/app/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export async function createDataset(values: DatasetToBeCreated, catalogId: strin
keyword: values?.keywordList ? transformToLocalizedStrings(values?.keywordList) : '',
concepts: values?.conceptList ? convertListToListOfObjects(values.conceptList, 'uri') : [],
spatial: values?.spatialList ? convertListToListOfObjects(values.spatialList, 'uri') : [],
language: values.languageList ? convertListToListOfObjects(values.languageList, 'uri') : [],
};
const datasetNoEmptyValues = removeEmptyValues(newDataset);

Expand Down Expand Up @@ -105,6 +106,7 @@ export async function updateDataset(catalogId: string, initialDataset: Dataset,
keyword: values?.keywordList ? transformToLocalizedStrings(values?.keywordList) : '',
concepts: values?.conceptList ? convertListToListOfObjects(values.conceptList, 'uri') : [],
spatial: values?.spatialList ? convertListToListOfObjects(values.spatialList, 'uri') : [],
language: values.languageList ? convertListToListOfObjects(values.languageList, 'uri') : [],
});

const diff = compare(initialDataset, updatedDataset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getDatasetTypes,
getDataThemes,
getFrequencies,
getLanguages,
getLosThemes,
getOrganization,
getProvenanceStatements,
Expand All @@ -27,12 +28,14 @@ export default async function EditDatasetPage({ params }: Params) {
datasetTypesResponse,
provenanceStatementsResponse,
frequenciesResponse,
languagesResponse,
] = await Promise.all([
getLosThemes().then((res) => res.json()),
getDataThemes().then((res) => res.json()),
getDatasetTypes().then((res) => res.json()),
getProvenanceStatements().then((res) => res.json()),
getFrequencies().then((res) => res.json()),
getLanguages().then((res) => res.json()),
]);

const referenceData = {
Expand All @@ -41,6 +44,7 @@ export default async function EditDatasetPage({ params }: Params) {
datasetTypes: datasetTypesResponse.datasetTypes,
provenanceStatements: provenanceStatementsResponse.provenanceStatements,
frequencies: frequenciesResponse.frequencies,
languages: languagesResponse.linguisticSystems,
};

const breadcrumbList = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getDatasetTypes,
getDataThemes,
getFrequencies,
getLanguages,
getLosThemes,
getOrganization,
getProvenanceStatements,
Expand All @@ -26,12 +27,14 @@ export default async function NewDatasetPage({ params }: Params) {
datasetTypesResponse,
provenanceStatementsResponse,
frequenciesResponse,
languageResponse
] = await Promise.all([
getLosThemes().then((res) => res.json()),
getDataThemes().then((res) => res.json()),
getDatasetTypes().then((res) => res.json()),
getProvenanceStatements().then((res) => res.json()),
getFrequencies().then((res) => res.json()),
getLanguages().then((res) => res.json())
]);

const referenceData = {
Expand All @@ -40,6 +43,7 @@ export default async function NewDatasetPage({ params }: Params) {
datasetTypes: datasetTypesResponse.datasetTypes,
provenanceStatements: provenanceStatementsResponse.provenanceStatements,
frequencies: frequenciesResponse.frequencies,
languages: languageResponse.linguisticSystems
};

const breadcrumbList = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
'use client';
import { AddButton, DeleteButton, FormContainer, TitleWithTag } from '@catalog-frontend/ui';
import { getTranslateText, localization } from '@catalog-frontend/utils';
import { Combobox, Heading, Textfield } from '@digdir/designsystemet-react';
import { Checkbox, Combobox, Heading, Textfield } from '@digdir/designsystemet-react';
import { useCallback, useState } from 'react';
import { useSearchAdministrativeUnits, useSearchAdministrativeUnitsByUri } from '../../hooks/useReferenceDataSearch';
import { Field, FieldArray, useFormikContext } from 'formik';
import { Dataset } from '@catalog-frontend/types';
import { debounce } from 'lodash';
import { Dataset, ReferenceDataCode } from '@catalog-frontend/types';
import { debounce, sortBy } from 'lodash';
import styles from './dataset-form.module.css';

interface Props {
envVariable: string;
languages: ReferenceDataCode[];
}

export const GeographySection = ({ envVariable }: Props) => {
export const GeographySection = ({ envVariable, languages }: Props) => {
const [searchTerm, setSearchTerm] = useState<string>('');
const { values, setFieldValue } = useFormikContext<Dataset>();
const langNOR = languages.filter((lang) => lang.code === 'NOR')[0];

const { data: searchHits, isLoading: isSearching } = useSearchAdministrativeUnits(searchTerm, envVariable);
const { data: selectedValues, isLoading: isLoadingselectedValues } = useSearchAdministrativeUnitsByUri(
Expand All @@ -37,6 +39,16 @@ export const GeographySection = ({ envVariable }: Props) => {
if (uri.includes('nasjon')) return localization.spatial.country;
return '';
};
const customLanguageOrder = [
'http://publications.europa.eu/resource/authority/language/NOB',
'http://publications.europa.eu/resource/authority/language/NNO',
'http://publications.europa.eu/resource/authority/language/ENG',
'http://publications.europa.eu/resource/authority/language/SMI',
];

const sortedLanguages = sortBy(languages, (item) => {
return customLanguageOrder.indexOf(item.uri);
});

const comboboxOptions = [
// Combines selectedValues and searchHits, and add uri's for values not found in selectedValues
Expand Down Expand Up @@ -147,10 +159,40 @@ export const GeographySection = ({ envVariable }: Props) => {
title={localization.datasetForm.heading.releaseDate}
subtitle={localization.datasetForm.helptext.releaseDate}
/>
<Field
className={styles.date}
as={Textfield}
type='date'
name='issued'
/>
<FormContainer.Header
title={localization.datasetForm.heading.language}
subtitle={localization.datasetForm.helptext.language}
/>
<Checkbox.Group
legend={`${localization.choose}...`}
onChange={(values) => setFieldValue('languageList', values)}
value={values.languageList}
>
{values.languageList && values.languageList.includes('NOR') && (
<Checkbox
key={langNOR.uri}
value={langNOR.uri}
>
{getTranslateText(langNOR.label)}
</Checkbox>
)}
{sortedLanguages
.filter((lang) => lang.code !== 'NOR')
.map((lang) => (
<Checkbox
key={lang.uri}
value={lang.uri}
>
{getTranslateText(lang.label)}
</Checkbox>
))}
</Checkbox.Group>
</FormContainer>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AccessRights, Dataset, DatasetToBeCreated, PublicationStatus } from '@c
import { groupByKeys } from '@catalog-frontend/utils';

export const datasetTemplate = (dataset: Dataset): Dataset => {
console.log(dataset.theme);
return {
id: dataset?.id ?? '',
catalogId: dataset?.catalogId ?? '',
Expand Down Expand Up @@ -44,6 +43,8 @@ export const datasetTemplate = (dataset: Dataset): Dataset => {
hasAvailabilityAnnotation: { hasBody: { nb: dataset.hasAvailabilityAnnotation?.hasBody?.nb ?? '' } },
spatialList: dataset.spatial ? dataset.spatial.map((spatial) => spatial.uri) : [],
temporal: dataset.temporal ?? [{ startDate: '', endDate: '' }],
issued: dataset.issued ?? '',
languageList: dataset.language ? dataset.language.map((lang) => lang.uri) : [],
};
};

Expand Down Expand Up @@ -81,5 +82,7 @@ export const datasetToBeCreatedTemplate = (): DatasetToBeCreated => {
hasAvailabilityAnnotation: { hasBody: { nb: '' } },
spatialList: [],
temporal: [{ startDate: '', endDate: '' }],
issued: '',
languageList: []
};
};
7 changes: 5 additions & 2 deletions apps/dataset-catalog/components/dataset-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Props = {
export const DatasetForm = ({ initialValues, submitType, referenceData, searchEnv, referenceDataEnv }: Props) => {
const { catalogId, datasetId } = useParams();
const [isDirty, setIsDirty] = useState(false);
const { losThemes, dataThemes, provenanceStatements, datasetTypes, frequencies } = referenceData;
const { losThemes, dataThemes, provenanceStatements, datasetTypes, frequencies, languages } = referenceData;

useWarnIfUnsavedChanges({ unsavedChanges: isDirty });

Expand Down Expand Up @@ -108,7 +108,10 @@ export const DatasetForm = ({ initialValues, submitType, referenceData, searchEn
/>
<TypeSection datasetTypes={datasetTypes} />
<ConceptSection searchEnv={searchEnv} />
<GeographySection envVariable={referenceDataEnv} />
<GeographySection
envVariable={referenceDataEnv}
languages={languages}
/>
<ProvenanceSection data={{ provenanceStatements, frequencies }} />
<ContentSection />
</div>
Expand Down
11 changes: 11 additions & 0 deletions libs/data-access/src/lib/reference-data/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,14 @@ export const getAdministrativeUnitsByUri = async (uriList: string[], envVariable

return data;
};

export const getLanguages = async () => {
const resource = `${process.env.FDK_BASE_URI}/reference-data/linguistic-systems`;
const options = {
headers: {
'Content-Type': 'application/json',
},
method: 'GET',
};
return await fetch(resource, options);
};
3 changes: 3 additions & 0 deletions libs/types/src/lib/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ export interface DatasetToBeCreated {
conformsTo?: UriWithLabel[];
spatial?: ReferenceDataCode[];
temporal?: DateRange[];
issued?: string;
language?: ReferenceDataCode[]
// Arrays of uris used as helper values for Formik. These properties is not part of the db object.
losThemeList?: string[];
euThemeList?: string[];
conceptList?: string[];
spatialList?: string[];
languageList?: string[]
}

export interface UriWithLabel {
Expand Down
1 change: 1 addition & 0 deletions libs/types/src/lib/reference-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ export interface ReferenceData {
provenanceStatements: ReferenceDataCode[];
datasetTypes: ReferenceDataCode[];
frequencies: ReferenceDataCode[];
languages: ReferenceDataCode[];
}
2 changes: 1 addition & 1 deletion libs/utils/src/lib/language/dataset.form.nb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const datasetFormNb = {
spatial: 'Søk etter geografisk område fra Administrative enheter (Kartverket) og velg fra nedtrekksliste.',
temporal: 'Tidsrommet datasettet dekker dersom det kun har innhold fra visse perioder',
releaseDate: 'Når innholdet i datasettet ble/blir tilgjengeliggjort',
language: 'Hovedspråket innholdet i datasettet er skrevet på.',
language: 'Språkene innholdet i datasettet er skrevet på.',
},
heading: {
description: 'Beskrivelse av datasettet',
Expand Down

0 comments on commit c13900a

Please sign in to comment.