This commit is contained in:
marzban-dev
2025-01-06 19:26:16 +03:30
parent 73d68810be
commit cdc0ebec26
13 changed files with 536 additions and 0 deletions
@@ -0,0 +1,45 @@
// imports
import { useMutation } from "@tanstack/vue-query";
import axios from "~/configs/axios.config";
import { API_ENDPOINTS } from "~/constants";
// types
export type AddDocRequest = {
name: string,
parent?: string,
branch: string | undefined,
type: {
title: "File" | "Folder",
icon: "bi:folder" | "bi:file-earmark"
},
content: File | undefined
};
// methods
export const handleAddDoc = async (variables: AddDocRequest & { updateUploadProgress: (p: number) => void }) => {
const { data } = await axios.post<AddDocRequest>(`${API_ENDPOINTS.branch.getDoc}/`, {
...variables,
type: variables.type.title.toLocaleLowerCase()
}, {
onUploadProgress: (progressEvent) => {
variables.updateUploadProgress(progressEvent.progress!);
},
headers: {
"Content-Type": "multipart/form-data"
}
});
return data;
};
// composable
const useAddDoc = () => {
return useMutation({
mutationFn: (variables: AddDocRequest & { updateUploadProgress: (p: number) => void }) => handleAddDoc(variables)
});
};
export default useAddDoc;
@@ -0,0 +1,27 @@
// imports
import { useMutation } from "@tanstack/vue-query";
import axios from "~/configs/axios.config";
import { API_ENDPOINTS } from "~/constants";
// types
export type DeleteDocRequest = {
id: number
};
// methods
export const handleDeleteDoc = async ({ id }: { id: string | undefined }) => {
await axios.delete<DeleteDocRequest>(`${API_ENDPOINTS.branch.getDoc}/${id}`);
};
// composable
const useDeleteDoc = (id: Ref<string | undefined>) => {
return useMutation({
mutationFn: () => handleDeleteDoc({ id: id.value })
});
};
export default useDeleteDoc;
@@ -0,0 +1,29 @@
// imports
import { useMutation } from "@tanstack/vue-query";
import axios from "~/configs/axios.config";
import { API_ENDPOINTS } from "~/constants";
// types
export type EditDocRequest = {
id: number
};
// methods
export const handleEditDoc = async ({ id, name }: { id: string | undefined, name: string | undefined }) => {
await axios.patch<EditDocRequest>(`${API_ENDPOINTS.branch.getDoc}/${id}`, { name });
};
// composable
const useEditDoc = (id: Ref<string | undefined>) => {
return useMutation({
mutationFn: ({ name }: { name: Ref<string | undefined> }) => {
return handleEditDoc({ id: id.value, name: name.value });
}
});
};
export default useEditDoc;
@@ -0,0 +1,33 @@
// imports
import { useQuery } from "@tanstack/vue-query";
import axios from "~/configs/axios.config";
import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";
// types
export type GetDocResponse = DocumentStructure;
// methods
export const handleGetDoc = async (id : string | undefined) => {
const { data } = await axios.get<GetDocResponse>(`${API_ENDPOINTS.branch.getDoc}/${id}`);
return data;
};
// composable
const useGetDoc = (id: ComputedRef<string | undefined>) => {
const enabled = computed(() => {
return !!id.value
});
return useQuery({
enabled,
queryKey: [QUERY_KEYS.document, id],
queryFn: () => handleGetDoc(id.value)
});
};
export default useGetDoc;
@@ -0,0 +1,32 @@
// imports
import { useMutation } from "@tanstack/vue-query";
import axios from "~/configs/axios.config";
import { API_ENDPOINTS } from "~/constants";
// types
export type MoveDocRequest = {
itemsToMove: number[] | string[],
parent: number | string
};
// methods
export const handleMoveDoc = async ({ itemsToMove, parent }: MoveDocRequest) => {
const apiUrl = `${API_ENDPOINTS.branch.moveDoc}?new_parent_id=${parent}&${itemsToMove.map(i => `patch_list=${i}&`)}`
const splittedUrl = apiUrl.split("");
splittedUrl.pop()
await axios.patch<MoveDocRequest>(splittedUrl.join("").replaceAll(",", ""));
};
// composable
const useMoveDoc = () => {
return useMutation({
mutationFn: (variables: MoveDocRequest) => handleMoveDoc(variables)
});
};
export default useMoveDoc;
@@ -0,0 +1,40 @@
// imports
import { useMutation } from "@tanstack/vue-query";
import axios from "~/configs/axios.config";
import { API_ENDPOINTS } from "~/constants";
// types
export type ReplyDocRequest = {
user_id: number;
message: string;
reply_id: number;
};
export type ReplyDocResponse = {
chat_id: number;
};
// methods
export const handleReplyDoc = async ({ user_id, message, reply_id }: ReplyDocRequest) => {
const payload = {
user_id,
message,
item_id: reply_id,
};
const { data } = await axios.post<ReplyDocResponse>(API_ENDPOINTS.branch.replyDoc, payload);
return data;
};
// composable
const useReplyDoc = () => {
return useMutation({
mutationFn: (data: ReplyDocRequest) => handleReplyDoc(data),
});
};
export default useReplyDoc;
@@ -0,0 +1,78 @@
// imports
import { useInfiniteQuery } from "@tanstack/vue-query";
import axios from "~/configs/axios.config";
import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";
import type { ComputedRef } from "vue";
// types
export type SearchFileResponse = Branch;
type HandlerProps = typeof initialPageParam & {
signal: AbortSignal,
search: string,
id: number | undefined;
sort: string | undefined;
}
// state
const initialPageParam = {
limit: 10,
offset: 0
};
// methods
export const handleSearchFile = async ({ search, offset, limit, id, signal, sort }: HandlerProps) => {
const { data } = await axios.get<SearchFileResponse>(`${API_ENDPOINTS.branch.get}/${id}`, {
params: {
offset,
limit,
search,
sort_by: sort
},
signal
});
return data;
};
// composable
const useSearchFile = (search: Ref<string>, id: Ref<number | undefined>, sort: ComputedRef<string | undefined>) => {
const enabled = computed(() => {
return search.value.trim() != "" && !!id.value;
});
return useInfiniteQuery({
enabled,
retry: false,
refetchOnMount: false,
refetchOnWindowFocus: false,
queryKey: [QUERY_KEYS.searchFile, search, id, sort],
queryFn: ({ pageParam, signal }) => handleSearchFile({
...pageParam,
signal,
search: search.value,
sort: sort.value,
id: id.value
}),
initialPageParam,
getNextPageParam: (lastPage, pages) => {
const page = pages.length + 1;
const limit = initialPageParam.limit;
const nextPageParams = {
offset: page * limit - limit,
limit
};
return lastPage?.structure.next ? nextPageParams : undefined;
}
});
};
export default useSearchFile;