apps/web: move getDocs function to server

rename getDocs -> combineDocuments
This commit is contained in:
vchikalkin 2023-11-30 15:53:49 +03:00
parent 74d5c11f8e
commit 5f8a9c5320
6 changed files with 45 additions and 32 deletions

23
apps/web/api/ius/tools.ts Normal file
View File

@ -0,0 +1,23 @@
import type { Document, DocumentType } from './types';
import { unique } from 'radash';
export function combineDocuments({
documentTypes,
documents,
}: {
documentTypes: DocumentType[];
documents: Document[];
}) {
if (!documents.length) {
return documentTypes.map((x) => ({ ...(x as Document), canUpload: true }));
}
return unique(documents, ({ documentTypeId }) => documentTypeId).map((document) => ({
...document,
canUpload: documentTypes.some(
(documentType) => documentType.documentTypeId === document.documentTypeId
),
}));
}
export type CombinedDocuments = ReturnType<typeof combineDocuments>;

View File

@ -1,5 +1,7 @@
import * as apiIUS from '@/api/ius/query';
import { combineDocuments } from '@/api/ius/tools';
import { Form } from '@/components/Form';
import type { FormComponentProps } from '@/components/Form/types';
import type { PageProps } from '@/types/page';
import { withError } from '@/utils/error';
import { getPageUrlParams } from '@/utils/url';
@ -32,7 +34,14 @@ export default async function Page(pageProps: PageProps) {
apiIUS.getDocumentTypes({ pageUrlParams }),
apiIUS.getDocuments({ pageUrlParams }),
]).then(([data, metaData, { title }, documentTypes, documents]) => {
const props = { data, documentTypes, documents, metaData, pageUrlParams, title };
const combinedDocuments = combineDocuments({ documentTypes, documents });
const props: FormComponentProps = {
combinedDocuments,
data,
metaData,
pageUrlParams,
title,
};
return <Form {...props} />;
});

View File

@ -1,4 +1,4 @@
import type { Props } from './types';
import type { FormComponentProps } from './types';
import type { MetaObject } from '@/api/ius/types';
import { mapFieldTypeElement } from '@/config/elements';
import { useFormStore } from '@/store/ius/form';
@ -49,7 +49,7 @@ function RenderElement({
);
}
export function Elements({ data, metaData }: Props) {
export function Elements({ data, metaData }: FormComponentProps) {
const { init } = useFormStore();
useEffect(() => {

View File

@ -1,10 +1,8 @@
import { FormContext } from './context/form-context';
import type { Props } from './types';
import type * as IUS from '@/api/ius/types';
import type { FormComponentProps } from './types';
import { ArrowDownTrayIcon } from '@heroicons/react/24/solid';
import { Heading, InputFile } from '@repo/ui';
import Link from 'next/link';
import { unique } from 'radash';
import { useContext } from 'react';
type DownloadDocumentProps = Pick<FileProps, 'document'>;
@ -28,7 +26,7 @@ function DownloadDocument({ document }: DownloadDocumentProps) {
}
type FileProps = {
readonly document: Docs[number];
readonly document: FormComponentProps['combinedDocuments'][number];
};
function File({ document }: FileProps) {
@ -56,32 +54,15 @@ function File({ document }: FileProps) {
);
}
export function Files({ documentTypes, documents }: Props) {
const docs = getDocs({ documentTypes, documents });
export function Files({ combinedDocuments }: FormComponentProps) {
return (
<div className="grid gap-4">
<Heading className="text-sm">Документы</Heading>
<div className="grid gap-2 md:grid-cols-2">
{docs.map((document) => (
{combinedDocuments.map((document) => (
<File key={document.documentTypeId} document={document} />
))}
</div>
</div>
);
}
type Docs = Array<IUS.Document & { canUpload: boolean }>;
function getDocs({ documentTypes, documents }: Pick<Props, 'documentTypes' | 'documents'>): Docs {
if (!documents.length) {
return documentTypes.map((x) => ({ ...x, canUpload: true }));
}
return unique(documents, ({ documentTypeId }) => documentTypeId).map((document) => ({
...document,
canUpload: documentTypes.some(
(documentType) => documentType.documentTypeId === document.documentTypeId
),
}));
}

View File

@ -5,13 +5,13 @@ import { Elements } from './Elements';
import { Files } from './Files';
import { Header } from './Header';
import { Overlay } from './Overlay';
import type { Props } from './types';
import type { FormComponentProps } from './types';
import { createUrl } from '@/utils/url';
import { Background, Divider } from '@repo/ui';
import type { FC } from 'react';
import { useContext } from 'react';
function Content(props: Props) {
function Content(props: FormComponentProps) {
const { title } = props;
const { pageUrlParams } = useContext(FormContext);
@ -28,7 +28,7 @@ function Content(props: Props) {
);
}
function withContext<T extends Props>(Component: FC<T>) {
function withContext<T extends FormComponentProps>(Component: FC<T>) {
return (props: T) => {
const { pageUrlParams } = props;

View File

@ -1,10 +1,10 @@
import type { CombinedDocuments } from '@/api/ius/tools';
import type * as IUS from '@/api/ius/types';
import type { PageUrlParams } from '@/utils/url';
export type Props = {
export type FormComponentProps = {
readonly combinedDocuments: CombinedDocuments;
readonly data: IUS.ResponseGetData;
readonly documentTypes: IUS.ResponseDocumentTypes;
readonly documents: IUS.ResponseDocuments;
readonly metaData: IUS.ResponseMetaData;
readonly pageUrlParams: PageUrlParams;
readonly title: string;