diff --git a/apps/web/api/ius/tools.ts b/apps/web/api/ius/tools.ts new file mode 100644 index 0000000..f87a744 --- /dev/null +++ b/apps/web/api/ius/tools.ts @@ -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; diff --git a/apps/web/app/ius/[slug]/page.tsx b/apps/web/app/ius/[slug]/page.tsx index 8115a1a..9dd1407 100644 --- a/apps/web/app/ius/[slug]/page.tsx +++ b/apps/web/app/ius/[slug]/page.tsx @@ -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
; }); diff --git a/apps/web/components/Form/Elements.tsx b/apps/web/components/Form/Elements.tsx index 636bc4a..015fe1f 100644 --- a/apps/web/components/Form/Elements.tsx +++ b/apps/web/components/Form/Elements.tsx @@ -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(() => { diff --git a/apps/web/components/Form/Files.tsx b/apps/web/components/Form/Files.tsx index a22db05..9c94302 100644 --- a/apps/web/components/Form/Files.tsx +++ b/apps/web/components/Form/Files.tsx @@ -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; @@ -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 (
Документы
- {docs.map((document) => ( + {combinedDocuments.map((document) => ( ))}
); } - -type Docs = Array; - -function getDocs({ documentTypes, documents }: Pick): 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 - ), - })); -} diff --git a/apps/web/components/Form/index.tsx b/apps/web/components/Form/index.tsx index bf856d9..a8f3c5b 100644 --- a/apps/web/components/Form/index.tsx +++ b/apps/web/components/Form/index.tsx @@ -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(Component: FC) { +function withContext(Component: FC) { return (props: T) => { const { pageUrlParams } = props; diff --git a/apps/web/components/Form/types.ts b/apps/web/components/Form/types.ts index e4fcd24..7c3a64c 100644 --- a/apps/web/components/Form/types.ts +++ b/apps/web/components/Form/types.ts @@ -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;