This commit is contained in:
marzban-dev
2025-01-13 21:13:23 +03:30
parent 1c33dde9e2
commit 6f3fba9b8e
4 changed files with 65 additions and 60 deletions
+12 -10
View File
@@ -1,7 +1,6 @@
// imports // imports
import { useMutation } from "@tanstack/vue-query"; import { useMutation } from "@tanstack/vue-query";
import axios from "~/configs/axios.config";
import { API_ENDPOINTS } from "~/constants"; import { API_ENDPOINTS } from "~/constants";
// types // types
@@ -10,16 +9,19 @@ export type OtpRequest = {
phone: string; phone: string;
}; };
// methods
export const handleOtp = async (variables: OtpRequest) => {
const { data } = await axios.post(`${API_ENDPOINTS.account.send_otp}`, variables);
return data;
};
// composable
const useOtp = () => { const useOtp = () => {
// state
const { $axios: axios } = useNuxtApp();
// methods
const handleOtp = async (variables: OtpRequest) => {
const { data } = await axios.post(`${API_ENDPOINTS.account.send_otp}`, variables);
return data;
};
return useMutation({ return useMutation({
mutationFn: (variables: OtpRequest) => handleOtp(variables) mutationFn: (variables: OtpRequest) => handleOtp(variables)
}); });
+12 -10
View File
@@ -1,7 +1,6 @@
// imports // imports
import { useMutation } from "@tanstack/vue-query"; import { useMutation } from "@tanstack/vue-query";
import axios from "~/configs/axios.config";
import { API_ENDPOINTS } from "~/constants"; import { API_ENDPOINTS } from "~/constants";
// types // types
@@ -11,16 +10,19 @@ export type SignInRequest = {
phone: string; phone: string;
}; };
// methods
export const handleSignIn = async (variables: SignInRequest) => {
const { data } = await axios.post(`${API_ENDPOINTS.auth.signin}/`, variables);
return data;
};
// composable
const useSignIn = () => { const useSignIn = () => {
// state
const { $axios: axios } = useNuxtApp();
// methods
const handleSignIn = async (variables: SignInRequest) => {
const { data } = await axios.post(`${API_ENDPOINTS.auth.signin}/`, variables);
return data;
};
return useMutation({ return useMutation({
mutationFn: (variables: SignInRequest) => handleSignIn(variables) mutationFn: (variables: SignInRequest) => handleSignIn(variables)
}); });
@@ -2,7 +2,6 @@
import { QueryClient, useMutation } from "@tanstack/vue-query"; import { QueryClient, useMutation } from "@tanstack/vue-query";
import type { InfiniteData } from "@tanstack/vue-query"; import type { InfiniteData } from "@tanstack/vue-query";
import axios from "~/configs/axios.config";
import { API_ENDPOINTS, MUTATION_KEYS, QUERY_KEYS } from "~/constants"; import { API_ENDPOINTS, MUTATION_KEYS, QUERY_KEYS } from "~/constants";
// types // types
@@ -14,20 +13,20 @@ export type CreateChatMessageRequest = {
export type CreateChatMessageResponse = Chat[] export type CreateChatMessageResponse = Chat[]
// methods
export const handleCreateChatMessage = async (variables: CreateChatMessageRequest) => {
const { data } = await axios.post<CreateChatMessageResponse>(`${API_ENDPOINTS.chat.new_message}/${variables.productId}`, variables, {
headers: {
Authorization: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoyMTY3ODE2OTAwLCJpYXQiOjE3MzU4MTY5MDAsImp0aSI6ImQwN2E2Y2Y2NzgwZjRlNTE5NWIzOGQxMTAzYzU4NDQ3IiwidXNlcl9pZCI6NX0.slwd7ZSV7nUXEuDTYwwHUOo9ekCefwEEL4kVv2vSTFo`
}
});
return data;
};
// composable
const useCreateChatMessage = (queryClient: QueryClient) => { const useCreateChatMessage = (queryClient: QueryClient) => {
// state
const { $axios: axios } = useNuxtApp();
// method
const handleCreateChatMessage = async (variables: CreateChatMessageRequest) => {
const { data } = await axios.post<CreateChatMessageResponse>(`${API_ENDPOINTS.chat.new_message}/${variables.productId}`, variables);
return data;
};
return useMutation({ return useMutation({
mutationKey: [MUTATION_KEYS.create_chat], mutationKey: [MUTATION_KEYS.create_chat],
mutationFn: (variables: CreateChatMessageRequest) => handleCreateChatMessage(variables), mutationFn: (variables: CreateChatMessageRequest) => handleCreateChatMessage(variables),
+28 -26
View File
@@ -1,39 +1,41 @@
// imports // imports
import { useInfiniteQuery, useQuery } from "@tanstack/vue-query"; import { useInfiniteQuery } from "@tanstack/vue-query";
import axios from "~/configs/axios.config";
import { API_ENDPOINTS, QUERY_KEYS } from "~/constants"; import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";
import type { ComputedRef } from "vue";
// types // types
export type GetBranchResponse = ApiPaginated<Chat>; export type GetBranchResponse = ApiPaginated<Chat>;
// methods
export const handleGetChat = async ({ productId, limit, offset }: {
productId: number | string,
limit: number,
offset: number
}) => {
const { data } = await axios.get<GetBranchResponse>(`${API_ENDPOINTS.chat.messages}/${productId}`, {
params: {
offset,
limit
},
headers: {
Authorization: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoyMTY3ODE2OTAwLCJpYXQiOjE3MzU4MTY5MDAsImp0aSI6ImQwN2E2Y2Y2NzgwZjRlNTE5NWIzOGQxMTAzYzU4NDQ3IiwidXNlcl9pZCI6NX0.slwd7ZSV7nUXEuDTYwwHUOo9ekCefwEEL4kVv2vSTFo`
}
});
return data;
};
// composable
const useGetBranch = ( const useGetBranch = (
productId: Ref<string | number>, productId: string | number,
enabled: Ref<boolean> enabled: Ref<boolean>
) => { ) => {
// state
const { $axios: axios } = useNuxtApp();
// method
const handleGetChat = async ({ productId, limit, offset }: {
productId: number | string,
limit: number,
offset: number
}) => {
const { data } = await axios.get<GetBranchResponse>(`${API_ENDPOINTS.chat.messages}/${productId}`, {
params: {
offset,
limit
},
headers: {
Authorization: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoyMTY3ODE2OTAwLCJpYXQiOjE3MzU4MTY5MDAsImp0aSI6ImQwN2E2Y2Y2NzgwZjRlNTE5NWIzOGQxMTAzYzU4NDQ3IiwidXNlcl9pZCI6NX0.slwd7ZSV7nUXEuDTYwwHUOo9ekCefwEEL4kVv2vSTFo`
}
});
return data;
};
return useInfiniteQuery({ return useInfiniteQuery({
enabled, enabled,
queryKey: [QUERY_KEYS.chat], queryKey: [QUERY_KEYS.chat],
@@ -44,7 +46,7 @@ const useGetBranch = (
queryFn: ({ pageParam }) => handleGetChat({ queryFn: ({ pageParam }) => handleGetChat({
limit: pageParam.limit, limit: pageParam.limit,
offset: pageParam.offset, offset: pageParam.offset,
productId: productId.value productId: productId
}), }),
getNextPageParam: (lastPage, pages) => { getNextPageParam: (lastPage, pages) => {
if (!lastPage.next) return undefined; if (!lastPage.next) return undefined;