Skip to content

Commit

Permalink
feat: add form for service
Browse files Browse the repository at this point in the history
  • Loading branch information
hegeaal committed Nov 24, 2023
1 parent aeb75a1 commit b91d075
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 16 deletions.
35 changes: 34 additions & 1 deletion apps/service-catalog/app/actions/public-services/actions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use server';

import { getAllPublicServices } from '@catalog-frontend/data-access';
import { createPublicService, getAllPublicServices } from '@catalog-frontend/data-access';
import { ServiceToBeCreated } from '@catalog-frontend/types';
import { authOptions, validateSession } from '@catalog-frontend/utils';
import { getServerSession } from 'next-auth';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';

export async function getPublicServices(catalogId: string) {
const session = await getServerSession(authOptions);
Expand All @@ -18,3 +21,33 @@ export async function getPublicServices(catalogId: string) {
return;
}
}

export async function createNewPublicService(catalogId: string, formData: FormData) {
const newPublicService: ServiceToBeCreated = {
catalogId: catalogId,
title: {
nb: formData.get('title.nb'),
},
description: {
nb: formData.get('description.nb'),
},
};

const session = await getServerSession(authOptions);
await validateSession(session);
let success = false;
try {
const response = await createPublicService(newPublicService, catalogId, `${session?.accessToken}`);
if (response.status !== 201) {
throw new Error();
}
success = true;
} catch (error) {
return;
} finally {
if (success) {
revalidatePath(`/catalogs/${catalogId}/public-services`);
redirect(`/catalogs/${catalogId}/public-services`);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { FormFieldCard } from '@catalog-frontend/ui';
import { Params } from 'next/dist/shared/lib/router/utils/route-matcher';
import ServiceForm from '../../../../components/service-form';
import { Heading } from '@digdir/design-system-react';

export default async function NewPublicServicePage() {
export default async function NewPublicServicePage({ params }: Params) {
const { catalogId } = params;
return (
<div>
<h1>Opprett ny offentlig tjeneste</h1>
<FormFieldCard
title='test'
subtitle='tyeye'
></FormFieldCard>
<div className='container'>
<Heading size='medium'>Informasjon om tjenesten</Heading>
<ServiceForm catalogId={catalogId} />
</div>
);
}
87 changes: 87 additions & 0 deletions apps/service-catalog/app/components/service-form/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'use client';

import { Tag, Textarea, Textfield } from '@digdir/design-system-react';
import { Button, FormFieldCard } from '@catalog-frontend/ui';

import { localization } from '@catalog-frontend/utils';

import { Service } from '@catalog-frontend/types';

import styles from './service-form.module.css';

import { TrashIcon } from '@navikt/aksel-icons';
import { createNewPublicService } from '../../actions/public-services/actions';

type ServiceFormProps = {
catalogId: string;
service?: Service;
};

export const ServiceForm = ({ catalogId, service }: ServiceFormProps) => {
const createPublicService = createNewPublicService.bind(null, catalogId);

return (
<>
<div className='container'>
<div className={styles.pageContainer}>
<form action={createPublicService}>
<div className={styles.formContainer}>
<FormFieldCard
title={localization.title}
subtitle='Egenskapen brukes å til oppgi navn til en tjeneste'
>
<div>
<Textfield
defaultValue={service?.title.nb}
label={
<div>
<p>Tekst på bokmål</p>
<Tag
variant='secondary'
color='first'
size='small'
>
Må fylles ut
</Tag>
</div>
}
type='text'
name='title.nb'
/>
</div>
</FormFieldCard>
<FormFieldCard
title={localization.description}
subtitle='Egenskapen brukes til å oppgi en tekstlig beskrivelse av tjenesten'
>
<Textarea name='description.nb' />
</FormFieldCard>
</div>

<div>
<Button type='submit'>Lagre tjeneste</Button>
{service ? (
<Button
color='danger'
variant='secondary'
>
<TrashIcon fontSize='1.5rem' />
Slett tjeneste
</Button>
) : (
<Button
color='danger'
variant='secondary'
>
Avbryt
</Button>
)}
</div>
</form>
</div>
</div>
</>
);
};

export default ServiceForm;
Empty file.
15 changes: 15 additions & 0 deletions libs/data-access/src/lib/public-services/api/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Service } from '@catalog-frontend/types';

const path = `${process.env.SERVICE_CATALOG_BASE_URI}`;

export const getAllPublicServices = async (catalogId: string, accessToken: string) => {
Expand All @@ -10,3 +12,16 @@ export const getAllPublicServices = async (catalogId: string, accessToken: strin
};
return await fetch(resource, options);
};

export const createPublicService = async (publicService: Partial<Service>, catalogId: string, accessToken: string) => {
const resource = `${path}/catalogs/${catalogId}/public-services`;
const options = {
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify(publicService),
};
return await fetch(resource, options);
};
1 change: 0 additions & 1 deletion libs/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ export * from './lib/editor';
export * from './lib/json-patch';
export * from './lib/change-request';
export * from './lib/service';
export * from './lib/params';
5 changes: 0 additions & 5 deletions libs/types/src/lib/params.ts

This file was deleted.

5 changes: 4 additions & 1 deletion libs/types/src/lib/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { MultiLanguageText } from './language';

export interface Service {
export interface Service extends ServiceToBeCreated {
id: string;
}

export interface ServiceToBeCreated {
catalogId: string;
title: MultiLanguageText;
description?: MultiLanguageText;
Expand Down

0 comments on commit b91d075

Please sign in to comment.