128 lines
3.8 KiB
JavaScript
128 lines
3.8 KiB
JavaScript
import { useCallback, useState } from 'react';
|
|
|
|
const useCorrespondents = ({
|
|
apiClient,
|
|
notifyApiError,
|
|
setStatusMessage,
|
|
tenantIdRef,
|
|
mapDocumentCaches,
|
|
}) => {
|
|
const [correspondents, setCorrespondents] = useState([]);
|
|
|
|
const refreshCorrespondents = useCallback(async () => {
|
|
const requestTenantId = tenantIdRef.current;
|
|
try {
|
|
const { data } = await apiClient.get('/correspondents');
|
|
if (tenantIdRef.current !== requestTenantId) {
|
|
return;
|
|
}
|
|
setCorrespondents(data || []);
|
|
} catch (error) {
|
|
if (tenantIdRef.current !== requestTenantId) {
|
|
return;
|
|
}
|
|
notifyApiError(error, 'Unable to load correspondents.');
|
|
}
|
|
}, [apiClient, notifyApiError, tenantIdRef]);
|
|
|
|
const handleCorrespondentUpdate = useCallback(
|
|
async (correspondentId, changes) => {
|
|
if (!correspondentId) {
|
|
throw new Error('Missing correspondent identifier.');
|
|
}
|
|
|
|
const payload = {};
|
|
if (typeof changes.name === 'string') {
|
|
const trimmed = changes.name.trim();
|
|
if (!trimmed) {
|
|
throw new Error('Correspondent name cannot be empty.');
|
|
}
|
|
payload.name = trimmed;
|
|
}
|
|
|
|
if (Object.keys(payload).length === 0) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
await apiClient.patch(`/correspondents/${correspondentId}`, payload);
|
|
await refreshCorrespondents();
|
|
setStatusMessage('Correspondent updated.', 'success');
|
|
return true;
|
|
} catch (error) {
|
|
const message = error.response?.data?.error || 'Failed to update correspondent.';
|
|
notifyApiError(error, message);
|
|
throw new Error(message);
|
|
}
|
|
},
|
|
[apiClient, notifyApiError, refreshCorrespondents, setStatusMessage],
|
|
);
|
|
|
|
const handleCorrespondentCreate = useCallback(
|
|
async ({ name }) => {
|
|
const trimmed = typeof name === 'string' ? name.trim() : '';
|
|
if (!trimmed) {
|
|
throw new Error('Correspondent name is required.');
|
|
}
|
|
try {
|
|
const { data } = await apiClient.post('/correspondents', { name: trimmed });
|
|
await refreshCorrespondents();
|
|
setStatusMessage('Correspondent created.', 'success');
|
|
return data;
|
|
} catch (error) {
|
|
const message = error.response?.data?.error || 'Failed to create correspondent.';
|
|
notifyApiError(error, message);
|
|
throw new Error(message);
|
|
}
|
|
},
|
|
[apiClient, notifyApiError, refreshCorrespondents, setStatusMessage],
|
|
);
|
|
|
|
const handleCorrespondentDelete = useCallback(
|
|
async (correspondentId) => {
|
|
if (!correspondentId) {
|
|
throw new Error('Missing correspondent identifier.');
|
|
}
|
|
|
|
const stripFromDoc = (doc) => {
|
|
if (!doc || !Array.isArray(doc.correspondents)) {
|
|
return doc;
|
|
}
|
|
const next = doc.correspondents.filter((entry) => entry.id !== correspondentId);
|
|
if (next.length === doc.correspondents.length) {
|
|
return doc;
|
|
}
|
|
return { ...doc, correspondents: next };
|
|
};
|
|
|
|
try {
|
|
await apiClient.delete(`/correspondents/${correspondentId}`);
|
|
await refreshCorrespondents();
|
|
|
|
if (typeof mapDocumentCaches === 'function') {
|
|
mapDocumentCaches(stripFromDoc);
|
|
}
|
|
|
|
setStatusMessage('Correspondent deleted.', 'success');
|
|
return true;
|
|
} catch (error) {
|
|
const message = error.response?.data?.error || 'Failed to delete correspondent.';
|
|
notifyApiError(error, message);
|
|
throw new Error(message);
|
|
}
|
|
},
|
|
[apiClient, mapDocumentCaches, notifyApiError, refreshCorrespondents, setStatusMessage],
|
|
);
|
|
|
|
return {
|
|
correspondents,
|
|
refreshCorrespondents,
|
|
handleCorrespondentCreate,
|
|
handleCorrespondentUpdate,
|
|
handleCorrespondentDelete,
|
|
setCorrespondents,
|
|
};
|
|
};
|
|
|
|
export default useCorrespondents;
|