apps/web: simplify combineDocuments function
This commit is contained in:
parent
a8d3c96d6d
commit
27d5fe4741
@ -1,5 +1,4 @@
|
|||||||
import type { Document, DocumentType } from './types';
|
import type { Document, DocumentType } from './types';
|
||||||
import { unique } from 'radash';
|
|
||||||
|
|
||||||
export function combineDocuments({
|
export function combineDocuments({
|
||||||
documentTypes,
|
documentTypes,
|
||||||
@ -12,20 +11,24 @@ export function combineDocuments({
|
|||||||
return documentTypes.map((x) => ({ ...(x as Document), canUpload: true }));
|
return documentTypes.map((x) => ({ ...(x as Document), canUpload: true }));
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = unique(documents, ({ documentTypeId }) => documentTypeId).map((document) => ({
|
const nonUploadableDocuments = documents
|
||||||
...document,
|
.filter(
|
||||||
canUpload: documentTypes.some(
|
(document) =>
|
||||||
(documentType) => documentType.documentTypeId === document.documentTypeId
|
!documentTypes.some(
|
||||||
),
|
(documentType) => documentType.documentTypeId === document.documentTypeId
|
||||||
}));
|
)
|
||||||
|
)
|
||||||
|
.map((document) => ({ ...document, canUpload: false }));
|
||||||
|
|
||||||
documentTypes.forEach((documentType) => {
|
return documentTypes
|
||||||
if (!res.some((document) => document.documentTypeId === documentType.documentTypeId)) {
|
.map((documentType) => {
|
||||||
res.push({ ...documentType, canUpload: true });
|
const targetDocument = documents.find(
|
||||||
}
|
(document) => document.documentTypeId === documentType.documentTypeId
|
||||||
});
|
);
|
||||||
|
|
||||||
return res;
|
return { ...documentType, ...targetDocument, canUpload: true };
|
||||||
|
})
|
||||||
|
.concat(nonUploadableDocuments);
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CombinedDocuments = ReturnType<typeof combineDocuments>;
|
export type CombinedDocuments = ReturnType<typeof combineDocuments>;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user