feat: Add document update functionality to frontend
This commit is contained in:
@@ -162,6 +162,19 @@ class DocumentsManager<T extends ManagedDocument = ManagedDocument> {
|
|||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
update(id: DocumentId, updater: (doc: T) => Partial<T> | T | undefined): boolean {
|
||||||
|
const doc = this.byId.get(id);
|
||||||
|
if (!doc) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const changes = updater(doc);
|
||||||
|
if (!changes) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const { changed } = this.ingest([{ ...doc, ...changes }]);
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
map(mapper: (doc: T) => T | undefined): boolean {
|
map(mapper: (doc: T) => T | undefined): boolean {
|
||||||
if (!this.byId.size) {
|
if (!this.byId.size) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -195,8 +195,7 @@ const useDocumentMutations = ({
|
|||||||
if (updatedDocument && documentsState.ingestDocuments) {
|
if (updatedDocument && documentsState.ingestDocuments) {
|
||||||
documentsState.ingestDocuments([updatedDocument]);
|
documentsState.ingestDocuments([updatedDocument]);
|
||||||
} else {
|
} else {
|
||||||
documentsState.documentsManager.map((doc) => {
|
documentsState.documentsManager.update(documentId, (doc) => {
|
||||||
if (doc.id !== documentId) return undefined;
|
|
||||||
if (updatedDocument) {
|
if (updatedDocument) {
|
||||||
return { ...doc, ...updatedDocument };
|
return { ...doc, ...updatedDocument };
|
||||||
}
|
}
|
||||||
@@ -229,8 +228,7 @@ const useDocumentMutations = ({
|
|||||||
if (updatedDocument && documentsState.ingestDocuments) {
|
if (updatedDocument && documentsState.ingestDocuments) {
|
||||||
documentsState.ingestDocuments([updatedDocument]);
|
documentsState.ingestDocuments([updatedDocument]);
|
||||||
} else {
|
} else {
|
||||||
documentsState.documentsManager.map((doc) => {
|
documentsState.documentsManager.update(documentId, (doc) => {
|
||||||
if (doc.id !== documentId) return undefined;
|
|
||||||
if (updatedDocument) {
|
if (updatedDocument) {
|
||||||
return { ...doc, ...updatedDocument };
|
return { ...doc, ...updatedDocument };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -780,10 +780,7 @@ const useDocumentsWorkspace = ({
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
documentsManager.map((doc) => {
|
documentsManager.update(documentId, (doc) => mergeAssetIntoDocument(doc, entry));
|
||||||
if (doc.id !== documentId) return undefined;
|
|
||||||
return mergeAssetIntoDocument(doc, entry);
|
|
||||||
});
|
|
||||||
|
|
||||||
return entry;
|
return entry;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import type CorrespondentManager from '../../lib/assets/CorrespondentManager';
|
|||||||
|
|
||||||
interface DocumentsManagerInterface {
|
interface DocumentsManagerInterface {
|
||||||
map(mapper: (doc: Document) => Document | undefined): boolean;
|
map(mapper: (doc: Document) => Document | undefined): boolean;
|
||||||
|
update(id: DocumentId, updater: (doc: Document) => Partial<Document> | Document | undefined): boolean;
|
||||||
ingest(rawDocs: unknown[]): { canonical: Document[]; changed: boolean };
|
ingest(rawDocs: unknown[]): { canonical: Document[]; changed: boolean };
|
||||||
remove(ids: Array<DocumentId>): boolean;
|
remove(ids: Array<DocumentId>): boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user