Remove asset/document hydration pipeline and introduce client-side cache updates for correspondents, tags, and search results
This commit is contained in:
@@ -21,7 +21,6 @@ interface UseBulkDocumentActionsArgs {
|
||||
resolveTargetDocumentIds: (ids?: Identifier[] | null) => Identifier[];
|
||||
correspondentLookupByName: Map<string, { id?: Identifier }>;
|
||||
handleCorrespondentCreate: (payload: { name: string }) => Promise<{ id?: Identifier } | null>;
|
||||
refreshCurrentFolder: () => Promise<void> | void;
|
||||
setStatusMessage: (message: string, variant?: string) => void;
|
||||
selectedDocumentIds?: Identifier[];
|
||||
selectedFolderIds?: Identifier[];
|
||||
@@ -29,6 +28,7 @@ interface UseBulkDocumentActionsArgs {
|
||||
handleFolderDelete: (id: Identifier, options?: { showMessage?: boolean; manageLoading?: boolean }) => Promise<boolean>;
|
||||
clearDocumentSelection: () => void;
|
||||
setLoading: (value: boolean) => void;
|
||||
updateDocumentCaches?: (id: Identifier, updater: (doc: any) => any) => void;
|
||||
}
|
||||
|
||||
const useBulkDocumentActions = ({
|
||||
@@ -36,7 +36,6 @@ const useBulkDocumentActions = ({
|
||||
resolveTargetDocumentIds,
|
||||
correspondentLookupByName,
|
||||
handleCorrespondentCreate,
|
||||
refreshCurrentFolder,
|
||||
setStatusMessage,
|
||||
selectedDocumentIds,
|
||||
selectedFolderIds,
|
||||
@@ -44,6 +43,7 @@ const useBulkDocumentActions = ({
|
||||
handleFolderDelete,
|
||||
clearDocumentSelection,
|
||||
setLoading,
|
||||
updateDocumentCaches,
|
||||
}: UseBulkDocumentActionsArgs) => {
|
||||
const handleBulkCorrespondentAdd = useCallback(
|
||||
async ({ name, input, documentIds }: { name?: string; input?: HTMLInputElement | null; documentIds?: Identifier[] }) => {
|
||||
@@ -70,21 +70,35 @@ const useBulkDocumentActions = ({
|
||||
if (!target?.id) {
|
||||
setStatusMessage('Unable to resolve correspondent.', 'error');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const response = await api.post<BulkAssignmentResponse>('/documents/bulk/correspondents', {
|
||||
document_ids: targets,
|
||||
assignments: [
|
||||
{
|
||||
correspondent_id: target.id,
|
||||
},
|
||||
],
|
||||
action: 'add',
|
||||
const response = await api.post<BulkAssignmentResponse>('/documents/bulk/correspondents', {
|
||||
document_ids: targets,
|
||||
assignments: [
|
||||
{
|
||||
correspondent_id: target.id,
|
||||
},
|
||||
],
|
||||
action: 'add',
|
||||
});
|
||||
|
||||
const { assigned = 0, removed = 0 } = 'data' in response ? response.data : response;
|
||||
|
||||
if (updateDocumentCaches && target.id) {
|
||||
targets.forEach((docId) => {
|
||||
updateDocumentCaches(docId, (doc) => {
|
||||
if (!doc) return doc;
|
||||
const current = Array.isArray((doc as any).correspondents) ? (doc as any).correspondents : [];
|
||||
if (current.some((entry: any) => entry?.id === target.id)) {
|
||||
return doc;
|
||||
}
|
||||
return {
|
||||
...(doc as any),
|
||||
correspondents: [...current, { id: target.id, name: (target as any).name }],
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
const { assigned = 0, removed = 0 } = 'data' in response ? response.data : response;
|
||||
|
||||
await refreshCurrentFolder();
|
||||
}
|
||||
const assignedSuffix = assigned === 1 ? '' : 's';
|
||||
if (removed > 0) {
|
||||
const removedSuffix = removed === 1 ? '' : 's';
|
||||
@@ -99,19 +113,19 @@ const useBulkDocumentActions = ({
|
||||
);
|
||||
}
|
||||
|
||||
if (input) {
|
||||
input.value = '';
|
||||
}
|
||||
},
|
||||
[
|
||||
api,
|
||||
correspondentLookupByName,
|
||||
handleCorrespondentCreate,
|
||||
refreshCurrentFolder,
|
||||
resolveTargetDocumentIds,
|
||||
setStatusMessage,
|
||||
],
|
||||
);
|
||||
if (input) {
|
||||
input.value = '';
|
||||
}
|
||||
},
|
||||
[
|
||||
api,
|
||||
correspondentLookupByName,
|
||||
handleCorrespondentCreate,
|
||||
resolveTargetDocumentIds,
|
||||
setStatusMessage,
|
||||
updateDocumentCaches,
|
||||
],
|
||||
);
|
||||
|
||||
const handleBulkCorrespondentRemove = useCallback(
|
||||
async ({ assignments = [], documentIds }: { assignments?: CorrespondentAssignment[]; documentIds?: Identifier[] }) => {
|
||||
@@ -131,14 +145,29 @@ const useBulkDocumentActions = ({
|
||||
correspondent_id: entry.correspondent_id,
|
||||
}));
|
||||
|
||||
const response = await api.post<BulkAssignmentResponse>('/documents/bulk/correspondents', {
|
||||
document_ids: targets,
|
||||
assignments: normalizedAssignments,
|
||||
action: 'remove',
|
||||
});
|
||||
const response = await api.post<BulkAssignmentResponse>('/documents/bulk/correspondents', {
|
||||
document_ids: targets,
|
||||
assignments: normalizedAssignments,
|
||||
action: 'remove',
|
||||
});
|
||||
|
||||
const { assigned = 0, removed = 0 } = 'data' in response ? response.data : response;
|
||||
await refreshCurrentFolder();
|
||||
const { assigned = 0, removed = 0 } = 'data' in response ? response.data : response;
|
||||
if (updateDocumentCaches) {
|
||||
targets.forEach((docId) => {
|
||||
updateDocumentCaches(docId, (doc) => {
|
||||
if (!doc || !Array.isArray((doc as any).correspondents)) {
|
||||
return doc;
|
||||
}
|
||||
const filtered = (doc as any).correspondents.filter(
|
||||
(entry: any) =>
|
||||
entry && !normalizedAssignments.some((assignment) => assignment.correspondent_id === entry.id),
|
||||
);
|
||||
return filtered.length === (doc as any).correspondents.length
|
||||
? doc
|
||||
: { ...(doc as any), correspondents: filtered };
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (removed > 0) {
|
||||
const removedSuffix = removed === 1 ? '' : 's';
|
||||
@@ -153,7 +182,7 @@ const useBulkDocumentActions = ({
|
||||
setStatusMessage('No correspondents changed.', 'info');
|
||||
}
|
||||
},
|
||||
[api, refreshCurrentFolder, resolveTargetDocumentIds, setStatusMessage],
|
||||
[api, resolveTargetDocumentIds, setStatusMessage, updateDocumentCaches],
|
||||
);
|
||||
|
||||
const handleDeleteSelection = useCallback(async () => {
|
||||
|
||||
Reference in New Issue
Block a user