mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-29 19:06:24 +02:00
add documents queries & mutations atoms
This commit is contained in:
parent
5a887e9243
commit
ed07699ffc
4 changed files with 216 additions and 0 deletions
115
surfsense_web/atoms/documents/document-mutation.atoms.ts
Normal file
115
surfsense_web/atoms/documents/document-mutation.atoms.ts
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
import { atomWithMutation } from "jotai-tanstack-query";
|
||||
import { toast } from "sonner";
|
||||
import { activeSearchSpaceIdAtom } from "@/atoms/seach-spaces/seach-space-queries.atom";
|
||||
import type {
|
||||
CreateDocumentRequest,
|
||||
DeleteDocumentRequest,
|
||||
GetDocumentsResponse,
|
||||
UpdateDocumentRequest,
|
||||
UploadDocumentRequest,
|
||||
} from "@/contracts/types/document.types";
|
||||
import { documentsApiService } from "@/lib/apis/documents-api.service";
|
||||
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
||||
import { queryClient } from "@/lib/query-client/client";
|
||||
import { globalDocumentsQueryParamsAtom } from "./ui.atoms";
|
||||
|
||||
export const createDocumentMutationAtom = atomWithMutation((get) => {
|
||||
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
||||
const documentsQueryParams = get(globalDocumentsQueryParamsAtom);
|
||||
|
||||
return {
|
||||
mutationKey: cacheKeys.documents.globalQueryParams(documentsQueryParams),
|
||||
enabled: !!searchSpaceId,
|
||||
mutationFn: async (request: CreateDocumentRequest) => {
|
||||
return documentsApiService.createDocument(request);
|
||||
},
|
||||
|
||||
onSuccess: () => {
|
||||
toast.success("Document created successfully");
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.documents.globalQueryParams(documentsQueryParams),
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.documents.typeCounts(searchSpaceId ?? undefined),
|
||||
});
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
export const uploadDocumentMutationAtom = atomWithMutation((get) => {
|
||||
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
||||
const documentsQueryParams = get(globalDocumentsQueryParamsAtom);
|
||||
|
||||
return {
|
||||
mutationKey: cacheKeys.documents.globalQueryParams(documentsQueryParams),
|
||||
enabled: !!searchSpaceId,
|
||||
mutationFn: async (request: UploadDocumentRequest) => {
|
||||
return documentsApiService.uploadDocument(request);
|
||||
},
|
||||
|
||||
onSuccess: () => {
|
||||
toast.success("Files uploaded for processing");
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
export const updateDocumentMutationAtom = atomWithMutation((get) => {
|
||||
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
||||
const documentsQueryParams = get(globalDocumentsQueryParamsAtom);
|
||||
|
||||
return {
|
||||
mutationKey: cacheKeys.documents.globalQueryParams(documentsQueryParams),
|
||||
enabled: !!searchSpaceId,
|
||||
mutationFn: async (request: UpdateDocumentRequest) => {
|
||||
return documentsApiService.updateDocument(request);
|
||||
},
|
||||
|
||||
onSuccess: (_, request: UpdateDocumentRequest) => {
|
||||
toast.success("Document updated successfully");
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.documents.globalQueryParams(documentsQueryParams),
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.documents.document(String(request.id)),
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.documents.typeCounts(searchSpaceId ?? undefined),
|
||||
});
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
export const deleteDocumentMutationAtom = atomWithMutation((get) => {
|
||||
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
||||
const authToken = localStorage.getItem("surfsense_bearer_token");
|
||||
const documentsQueryParams = get(globalDocumentsQueryParamsAtom);
|
||||
|
||||
return {
|
||||
mutationKey: cacheKeys.documents.globalQueryParams(documentsQueryParams),
|
||||
enabled: !!searchSpaceId && !!authToken,
|
||||
mutationFn: async (request: DeleteDocumentRequest) => {
|
||||
return documentsApiService.deleteDocument(request);
|
||||
},
|
||||
|
||||
onSuccess: (_, request: DeleteDocumentRequest) => {
|
||||
toast.success("Document deleted successfully");
|
||||
queryClient.setQueryData(
|
||||
cacheKeys.documents.globalQueryParams(documentsQueryParams),
|
||||
(oldData: GetDocumentsResponse | undefined) => {
|
||||
if (!oldData) return oldData;
|
||||
return {
|
||||
...oldData,
|
||||
items: oldData.items.filter((doc) => doc.id !== request.id),
|
||||
total: oldData.total - 1,
|
||||
};
|
||||
}
|
||||
);
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.documents.document(String(request.id)),
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.documents.typeCounts(searchSpaceId ?? undefined),
|
||||
});
|
||||
},
|
||||
};
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue