Skip to content

Commit

Permalink
fix: new concept
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreiffers committed Dec 13, 2024
1 parent 7dd519b commit 2fb79ad
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use client';

import { Breadcrumbs, PageBanner } from '@catalog-frontend/ui';
import { localization, getTranslateText } from '@catalog-frontend/utils';
import ConceptForm from '../../../../../components/concept-form';

export const NewPage = ({
breadcrumbList,
catalogId,
organization,
concept,
conceptStatuses,
codeListsResult,
fieldsResult,
usersResult,
catalogPortalBaseUri
}) => {


return (
<>
<Breadcrumbs
breadcrumbList={breadcrumbList}
catalogPortalUrl={`${catalogPortalBaseUri}/catalogs`}
/>
<PageBanner
title={localization.catalogType.concept}
subtitle={getTranslateText(organization.prefLabel).toString()}
/>
<ConceptForm
catalogId={catalogId}
concept={concept}
conceptStatuses={conceptStatuses}
codeListsResult={codeListsResult}
fieldsResult={fieldsResult}
usersResult={usersResult}
/>
</>
);
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,61 @@
import { RedirectType, redirect } from 'next/navigation';
import { getAllCodeLists, getConceptStatuses, getFields, getOrganization, getUsers } from '@catalog-frontend/data-access';
import { BreadcrumbType } from '@catalog-frontend/ui';
import { localization, prepareStatusList } from '@catalog-frontend/utils';
import { CodeListsResult, FieldsResult, Organization, UsersResult } from '@catalog-frontend/types';
import { withWriteProtectedPage } from '../../../../../utils/auth';

export const NewPage = withWriteProtectedPage(
({ catalogId }) => `/catalogs/${catalogId}/concepts/new`,
async ({ catalogId }) => {
//redirect(`${process.env.CONCEPT_CATALOG_GUI_BASE_URI}/${catalogId}/new`, RedirectType.push);
import { NewPage } from './new-page.client';

export default withWriteProtectedPage(
({ catalogId }) => `/catalogs//${catalogId}/concepts/new`,
async ({ catalogId, session }) => {
const concept = {
ansvarligVirksomhet: {
id: catalogId
}
};

const conceptStatuses = await getConceptStatuses()
.then((response) => response.json())
.then((body) => body?.conceptStatuses ?? [])
.then((statuses) => prepareStatusList(statuses));

const organization: Organization = await getOrganization(catalogId).then((response) => response.json());
const codeListsResult: CodeListsResult = await getAllCodeLists(catalogId, `${session?.accessToken}`).then(
(response) => response.json(),
);
const fieldsResult: FieldsResult = await getFields(catalogId, `${session?.accessToken}`).then((response) =>
response.json(),
);
const usersResult: UsersResult = await getUsers(catalogId, `${session?.accessToken}`).then((response) =>
response.json(),
);

const breadcrumbList = catalogId
? ([
{
href: `/catalogs/${catalogId}`,
text: localization.catalogType.concept,
},
{
href: `/catalogs/${catalogId}/concepts/new`,
text: localization.newConcept,
},
] as BreadcrumbType[])
: [];

return (
<NewPage
catalogId={catalogId}
breadcrumbList={breadcrumbList}
organization={organization}
concept={concept}
conceptStatuses={conceptStatuses}
codeListsResult={codeListsResult}
fieldsResult={fieldsResult}
usersResult={usersResult}
catalogPortalBaseUri={process.env.CATALOG_PORTAL_BASE_URI}
/>
);
},
);

export default NewPage;
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export const DefinitionSection = () => {
...def,
kildebeskrivelse: {
forholdTilKilde: def.kildebeskrivelse?.forholdTilKilde ?? 'egendefinert',
kilde: def.kildebeskrivelse?.kilde ?? []
}
}
kilde: def.kildebeskrivelse?.kilde ?? [],
},
};
};

return (
Expand All @@ -57,13 +57,7 @@ export const DefinitionSection = () => {
tagTitle={localization.tag.required}
/>
}
>
{Object.keys(errors).some((value) =>
['definisjon', 'definisjonForAllmennheten', 'definisjonForSpesialister'].includes(value),
) && (
<ErrorMessage>Minst en definisjon må være definert!</ErrorMessage>
)}
</Fieldset>
/>

{definitions
.filter((name) => values[name])
Expand All @@ -85,7 +79,7 @@ export const DefinitionSection = () => {
</Heading>
<Popover
open={open[index]}
onClose={() => setOpen({...open, [index]: false})}
onClose={() => setOpen({ ...open, [index]: false })}
placement='top'
size='md'
variant='default'
Expand All @@ -94,8 +88,10 @@ export const DefinitionSection = () => {
<Tag
size='sm'
color='second'
onMouseEnter={() => def.kildebeskrivelse?.kilde?.length && setOpen({...open, [index]: true})}
onMouseOut={() => setOpen({...open, [index]: false})}
onMouseEnter={() =>
def.kildebeskrivelse?.kilde?.length && setOpen({ ...open, [index]: true })
}
onMouseOut={() => setOpen({ ...open, [index]: false })}
>
{`${def.kildebeskrivelse?.kilde?.length ? def.kildebeskrivelse?.kilde.length : 'Ingen'} ${localization.conceptForm.fieldLabel.sources.toLowerCase()}`}
</Tag>
Expand Down Expand Up @@ -187,6 +183,11 @@ export const DefinitionSection = () => {
/>
))}
</Box>

{Object.keys(errors).some((value) =>
['definisjon', 'definisjonForAllmennheten', 'definisjonForSpesialister'].includes(value),
) && <ErrorMessage>Minst en definisjon må være definert!</ErrorMessage>}

</Box>
);
};
21 changes: 11 additions & 10 deletions apps/concept-catalog/components/concept-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import classNames from 'classnames';
import { Formik, Form } from 'formik';
import { useRouter } from 'next/navigation';
import { Alert, Spinner } from '@digdir/designsystemet-react';
import { localization, trimObjectWhitespace } from '@catalog-frontend/utils';
import { localization } from '@catalog-frontend/utils';
import { CodeListsResult, Concept, FieldsResult, ReferenceDataCode, UsersResult } from '@catalog-frontend/types';
import { Button, FormLayout, NotificationCarousel } from '@catalog-frontend/ui';
import { createConcept, updateConcept } from '../../app/actions/concept/actions';
Expand Down Expand Up @@ -60,25 +60,25 @@ const getNotifications = ({ isValid, hasUnsavedChanges }) => [
];

const ConceptForm = ({ catalogId, concept, conceptStatuses, codeListsResult, fieldsResult, usersResult }: Props) => {
const [isSubmitted, setIsSubmitted] = useState(false);
const [isSaveButtonClicked, setIsSaveButtonClicked] = useState(false);
const [isCanceled, setIsCanceled] = useState(false);

const router = useRouter();

const mapPropsToValues = ({
id,
anbefaltTerm = { navn: {} },
definisjon = { tekst: {}, kildebeskrivelse: undefined },
definisjonForAllmennheten = { tekst: {}, kildebeskrivelse: undefined },
definisjonForSpesialister = { tekst: {}, kildebeskrivelse: undefined },
definisjon = undefined,
definisjonForAllmennheten = undefined,
definisjonForSpesialister = undefined,
merknad = {},
merkelapp = [],
tillattTerm = {},
frarådetTerm = {},
eksempel = {},
fagområde = {},
fagområdeKoder = [],
statusURI = undefined,
statusURI = 'http://publications.europa.eu/resource/authority/concept-status/DRAFT',
omfang = undefined,
kontaktpunkt = undefined,
gyldigFom = undefined,
Expand All @@ -91,7 +91,7 @@ const ConceptForm = ({ catalogId, concept, conceptStatuses, codeListsResult, fie
abbreviatedLabel = undefined,
begrepsRelasjon = [],
internBegrepsRelasjon = [],
versjonsnr = { major: 0, minor: 0, patch: 0 },
versjonsnr = { major: 0, minor: 1, patch: 0 },
interneFelt = {},
...rest
}: Concept) => {
Expand Down Expand Up @@ -156,19 +156,19 @@ const ConceptForm = ({ catalogId, concept, conceptStatuses, codeListsResult, fie
<Formik
initialValues={mapPropsToValues(concept)}
validationSchema={conceptSchema}
validateOnChange={isSubmitted}
validateOnBlur={isSubmitted}
validateOnChange={isSaveButtonClicked}
validateOnBlur={isSaveButtonClicked}
onSubmit={async (values, { setSubmitting }) => {
concept.id === null ? await handleCreate(values as Concept) : await handleUpdate(values as Concept);
setSubmitting(false);
setIsSubmitted(true);
}}
>
{({ errors, values, dirty, isValid, isSubmitting, isValidating, submitForm }) => {
const notifications = getNotifications({ isValid, hasUnsavedChanges: false });
return (
<>
<div className='container'>
{isSaveButtonClicked ? 'save button clicked' : 'save not clicked'}
<Form>
<FormLayout>
<FormLayout.Section
Expand Down Expand Up @@ -274,6 +274,7 @@ const ConceptForm = ({ catalogId, concept, conceptStatuses, codeListsResult, fie
type='button'
disabled={isSubmitting || isValidating || isCanceled || !dirty}
onClick={() => {
setIsSaveButtonClicked(true);
submitForm();
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ export const definitionSchema = Yup.object()
return true;
},
})
.nullable();
.nullable()
.default(null);

export const relationSchema = Yup.object().shape({
relasjon: Yup.string().required('Feltet må fylles ut'),
Expand Down
1 change: 1 addition & 0 deletions libs/utils/src/lib/language/nb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const nb = {
choose: 'Velg',
serviceMessageError: 'Kunne ikke laste inn tjenestemeldinger. Vennligst prøv igjen senere.',
serviceMessageSeeMore: 'Se detaljert driftsmelding for mer informasjon.',
newConcept: 'Nytt begrep',

catalogType: {
admin: 'Administrasjonsgrensesnitt',
Expand Down

0 comments on commit 2fb79ad

Please sign in to comment.