From 89d1cf22bb04ae329f249fd6d2409c0b02bafcd3 Mon Sep 17 00:00:00 2001 From: hegeaal Date: Wed, 23 Oct 2024 13:05:42 +0200 Subject: [PATCH] feat: add main languages field to dataset form --- apps/dataset-catalog/app/actions/actions.ts | 2 + .../datasets/[datasetId]/edit/page.tsx | 4 ++ .../[catalogId]/datasets/new/page.tsx | 4 ++ .../dataset-form-geography-section.tsx | 48 +++++++++++++++++-- .../dataset-form/dataset-initial-values.tsx | 5 +- .../components/dataset-form/index.tsx | 7 ++- .../src/lib/reference-data/api/index.ts | 11 +++++ libs/types/src/lib/dataset.ts | 3 ++ libs/types/src/lib/reference-data.ts | 1 + .../utils/src/lib/language/dataset.form.nb.ts | 2 +- 10 files changed, 79 insertions(+), 8 deletions(-) diff --git a/apps/dataset-catalog/app/actions/actions.ts b/apps/dataset-catalog/app/actions/actions.ts index 3ab653657..5b41418b1 100644 --- a/apps/dataset-catalog/app/actions/actions.ts +++ b/apps/dataset-catalog/app/actions/actions.ts @@ -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); @@ -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); diff --git a/apps/dataset-catalog/app/catalogs/[catalogId]/datasets/[datasetId]/edit/page.tsx b/apps/dataset-catalog/app/catalogs/[catalogId]/datasets/[datasetId]/edit/page.tsx index 92ac947f8..ca0c8aa3b 100644 --- a/apps/dataset-catalog/app/catalogs/[catalogId]/datasets/[datasetId]/edit/page.tsx +++ b/apps/dataset-catalog/app/catalogs/[catalogId]/datasets/[datasetId]/edit/page.tsx @@ -9,6 +9,7 @@ import { getDatasetTypes, getDataThemes, getFrequencies, + getLanguages, getLosThemes, getOrganization, getProvenanceStatements, @@ -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 = { @@ -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 = [ diff --git a/apps/dataset-catalog/app/catalogs/[catalogId]/datasets/new/page.tsx b/apps/dataset-catalog/app/catalogs/[catalogId]/datasets/new/page.tsx index 3c1416218..5c5784874 100644 --- a/apps/dataset-catalog/app/catalogs/[catalogId]/datasets/new/page.tsx +++ b/apps/dataset-catalog/app/catalogs/[catalogId]/datasets/new/page.tsx @@ -8,6 +8,7 @@ import { getDatasetTypes, getDataThemes, getFrequencies, + getLanguages, getLosThemes, getOrganization, getProvenanceStatements, @@ -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 = { @@ -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 = [ diff --git a/apps/dataset-catalog/components/dataset-form/dataset-form-geography-section.tsx b/apps/dataset-catalog/components/dataset-form/dataset-form-geography-section.tsx index f062f4cee..654a74067 100644 --- a/apps/dataset-catalog/components/dataset-form/dataset-form-geography-section.tsx +++ b/apps/dataset-catalog/components/dataset-form/dataset-form-geography-section.tsx @@ -1,21 +1,23 @@ 'use client'; import { AddButton, DeleteButton, FormContainer, TitleWithTag } from '@catalog-frontend/ui'; import { getTranslateText, localization } from '@catalog-frontend/utils'; -import { Button, Combobox, Heading, Textfield } from '@digdir/designsystemet-react'; +import { Combobox, Heading, Textfield, Checkbox, Tag } from '@digdir/designsystemet-react'; import { useMemo, 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(''); const { values, setFieldValue } = useFormikContext(); + const langNOR = languages.filter(lang => lang.code === 'NOR')[0] const { data: searchHits, isLoading: isSearching } = useSearchAdministrativeUnits(searchTerm, envVariable); const { data: selectedValues, isLoading: isLoadingselectedValues } = useSearchAdministrativeUnitsByUri( @@ -35,6 +37,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 @@ -145,10 +157,38 @@ export const GeographySection = ({ envVariable }: Props) => { title={localization.datasetForm.heading.releaseDate} subtitle={localization.datasetForm.helptext.releaseDate} /> + + setFieldValue('languageList', values)} + value={values.languageList} + > + {values.languageList && values.languageList.includes('http://publications.europa.eu/resource/authority/language/NOR') && + + {getTranslateText(langNOR.label)} + + } + {sortedLanguages.filter(lang => lang.code !== 'NOR').map((lang) => ( + + {getTranslateText(lang.label)} + + ))} + ); diff --git a/apps/dataset-catalog/components/dataset-form/dataset-initial-values.tsx b/apps/dataset-catalog/components/dataset-form/dataset-initial-values.tsx index a1d8ed928..6b25e376b 100644 --- a/apps/dataset-catalog/components/dataset-form/dataset-initial-values.tsx +++ b/apps/dataset-catalog/components/dataset-form/dataset-initial-values.tsx @@ -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 ?? '', @@ -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) : [], }; }; @@ -81,5 +82,7 @@ export const datasetToBeCreatedTemplate = (): DatasetToBeCreated => { hasAvailabilityAnnotation: { hasBody: { nb: '' } }, spatialList: [], temporal: [{ startDate: '', endDate: '' }], + issued: '', + languageList: [] }; }; diff --git a/apps/dataset-catalog/components/dataset-form/index.tsx b/apps/dataset-catalog/components/dataset-form/index.tsx index 33ff20135..526b68ec8 100644 --- a/apps/dataset-catalog/components/dataset-form/index.tsx +++ b/apps/dataset-catalog/components/dataset-form/index.tsx @@ -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 }); @@ -108,7 +108,10 @@ export const DatasetForm = ({ initialValues, submitType, referenceData, searchEn /> - + diff --git a/libs/data-access/src/lib/reference-data/api/index.ts b/libs/data-access/src/lib/reference-data/api/index.ts index 349e3f922..883cb1901 100644 --- a/libs/data-access/src/lib/reference-data/api/index.ts +++ b/libs/data-access/src/lib/reference-data/api/index.ts @@ -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); +}; diff --git a/libs/types/src/lib/dataset.ts b/libs/types/src/lib/dataset.ts index 4c7fb15a0..da5802d58 100644 --- a/libs/types/src/lib/dataset.ts +++ b/libs/types/src/lib/dataset.ts @@ -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 { diff --git a/libs/types/src/lib/reference-data.ts b/libs/types/src/lib/reference-data.ts index 75f7e3e92..65bddb100 100644 --- a/libs/types/src/lib/reference-data.ts +++ b/libs/types/src/lib/reference-data.ts @@ -52,4 +52,5 @@ export interface ReferenceData { provenanceStatements: ReferenceDataCode[]; datasetTypes: ReferenceDataCode[]; frequencies: ReferenceDataCode[]; + languages: ReferenceDataCode[]; } diff --git a/libs/utils/src/lib/language/dataset.form.nb.ts b/libs/utils/src/lib/language/dataset.form.nb.ts index 0f51503ce..3233357eb 100644 --- a/libs/utils/src/lib/language/dataset.form.nb.ts +++ b/libs/utils/src/lib/language/dataset.form.nb.ts @@ -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',