From 1dcd3647ac2fde95a2af9295ba36e3baabffb411 Mon Sep 17 00:00:00 2001 From: Mamalizz Date: Sun, 23 Feb 2025 23:21:41 +0330 Subject: [PATCH 01/12] new changes --- .../api/account/useCreateOrUpdateAddress.ts | 8 +- .../api/account/useDeleteAddress.ts | 2 +- .../api/account/useUpdateAccount.ts | 2 +- frontend/composables/api/auth/useAuth.ts | 17 ++- .../composables/api/auth/useRefreshAuth.ts | 16 ++- .../api/chat/useCreateChatMessage.ts | 123 +++++++++--------- frontend/composables/api/chat/useGetChat.ts | 57 ++++---- .../api/product/useCreateComment.ts | 13 +- 8 files changed, 129 insertions(+), 109 deletions(-) diff --git a/frontend/composables/api/account/useCreateOrUpdateAddress.ts b/frontend/composables/api/account/useCreateOrUpdateAddress.ts index f5a069c..a2f978b 100644 --- a/frontend/composables/api/account/useCreateOrUpdateAddress.ts +++ b/frontend/composables/api/account/useCreateOrUpdateAddress.ts @@ -5,17 +5,17 @@ import { API_ENDPOINTS } from "~/constants"; // types -export type CreateOrUpdateAddressRequest = Omit; +export type CreateOrUpdateAddressResponse = Omit; const useCreateOrUpdateAddress = (update: ComputedRef) => { // state const { $axios: axios } = useNuxtApp(); - // method + // methods const handleCreateOrUpdateAddress = async ( - addressData: CreateOrUpdateAddressRequest + addressData: CreateOrUpdateAddressResponse ) => { const { data } = await axios[update.value ? "put" : "post"]( update.value @@ -30,7 +30,7 @@ const useCreateOrUpdateAddress = (update: ComputedRef) => { }; return useMutation({ - mutationFn: (addressData: CreateOrUpdateAddressRequest) => + mutationFn: (addressData: CreateOrUpdateAddressResponse) => handleCreateOrUpdateAddress(addressData), }); }; diff --git a/frontend/composables/api/account/useDeleteAddress.ts b/frontend/composables/api/account/useDeleteAddress.ts index f894016..ada4989 100644 --- a/frontend/composables/api/account/useDeleteAddress.ts +++ b/frontend/composables/api/account/useDeleteAddress.ts @@ -14,7 +14,7 @@ const useDeleteAddress = () => { const { $axios: axios } = useNuxtApp(); - // method + // methods const handleDeleteAddress = async (id: number) => { const { data } = await axios.delete( diff --git a/frontend/composables/api/account/useUpdateAccount.ts b/frontend/composables/api/account/useUpdateAccount.ts index 521d56a..8a4882e 100644 --- a/frontend/composables/api/account/useUpdateAccount.ts +++ b/frontend/composables/api/account/useUpdateAccount.ts @@ -20,7 +20,7 @@ const useUpdateAccount = () => { const { $axios: axios } = useNuxtApp(); - // method + // methods const handleUpdateAccount = async (params: UpdateAccountRequest) => { const { data } = await axios.patch( diff --git a/frontend/composables/api/auth/useAuth.ts b/frontend/composables/api/auth/useAuth.ts index e74d967..d65e25d 100644 --- a/frontend/composables/api/auth/useAuth.ts +++ b/frontend/composables/api/auth/useAuth.ts @@ -1,11 +1,10 @@ export const useAuth = () => { - // state const token = useCookie("token"); const refreshToken = useCookie("refresh-token"); - // method + // methods const updateToken = (newToken: string) => { token.value = newToken; @@ -15,7 +14,7 @@ export const useAuth = () => { refreshToken.value = newToken; }; - const logout = (reload ?: boolean) => { + const logout = (reload?: boolean) => { token.value = undefined; refreshToken.value = undefined; if (reload) window.location.reload(); @@ -25,6 +24,12 @@ export const useAuth = () => { const isLoggedIn = computed(() => !!token.value); - return { token, refreshToken, updateRefreshToken, updateToken, logout, isLoggedIn }; - -}; \ No newline at end of file + return { + token, + refreshToken, + updateRefreshToken, + updateToken, + logout, + isLoggedIn, + }; +}; diff --git a/frontend/composables/api/auth/useRefreshAuth.ts b/frontend/composables/api/auth/useRefreshAuth.ts index 6540427..520c175 100644 --- a/frontend/composables/api/auth/useRefreshAuth.ts +++ b/frontend/composables/api/auth/useRefreshAuth.ts @@ -6,17 +6,15 @@ import { API_ENDPOINTS } from "~/constants"; // types export type RefreshAuthRequest = { - refresh: string, + refresh: string; }; export type RefreshAuthResponse = { - access: string, - refresh: string, + access: string; + refresh: string; }; - const useRefreshAuth = () => { - // state const { $axios: axios } = useNuxtApp(); @@ -24,12 +22,16 @@ const useRefreshAuth = () => { // methods const handleRefreshAuth = async (variables: RefreshAuthRequest) => { - const { data } = await axios.post(`${API_ENDPOINTS.auth.refresh}/`, variables); + const { data } = await axios.post( + `${API_ENDPOINTS.auth.refresh}`, + variables + ); return data; }; return useMutation({ - mutationFn: (variables: RefreshAuthRequest) => handleRefreshAuth(variables) + mutationFn: (variables: RefreshAuthRequest) => + handleRefreshAuth(variables), }); }; diff --git a/frontend/composables/api/chat/useCreateChatMessage.ts b/frontend/composables/api/chat/useCreateChatMessage.ts index 87f5da6..700b8b4 100644 --- a/frontend/composables/api/chat/useCreateChatMessage.ts +++ b/frontend/composables/api/chat/useCreateChatMessage.ts @@ -11,64 +11,37 @@ export type CreateChatMessageRequest = { new_message: string; }; -export type CreateChatMessageResponse = Chat[] +export type CreateChatMessageResponse = Chat[]; const useCreateChatMessage = (queryClient: QueryClient) => { - // state const { $axios: axios } = useNuxtApp(); - // method + // methods - const handleCreateChatMessage = async (variables: CreateChatMessageRequest) => { - - const { data } = await axios.post(`${API_ENDPOINTS.chat.new_message}/${variables.productId}`, variables); + const handleCreateChatMessage = async ( + variables: CreateChatMessageRequest + ) => { + const { data } = await axios.post( + `${API_ENDPOINTS.chat.new_message}/${variables.productId}`, + variables + ); return data; }; return useMutation({ mutationKey: [MUTATION_KEYS.create_chat], - mutationFn: (variables: CreateChatMessageRequest) => handleCreateChatMessage(variables), + mutationFn: (variables: CreateChatMessageRequest) => + handleCreateChatMessage(variables), onMutate: (newMessage) => { - const prevData = queryClient.getQueriesData({ queryKey: [QUERY_KEYS.chat] }); - - queryClient.setQueryData>>([QUERY_KEYS.chat], (oldData) => { - const lastPage = oldData!.pages[oldData!.pages.length - 1]; - - return { - pages: [ - { - count: lastPage.count, - next: lastPage.next, - previous: lastPage.previous, - results: [ - { - id: Date.now(), - content: newMessage.new_message, - sender: "user" - } - ] - }, - ...oldData!.pages - ], - pageParams: [ - ...oldData!.pageParams, - { - limit: 10, - offset: 0 - } - ] - }; + const prevData = queryClient.getQueriesData({ + queryKey: [QUERY_KEYS.chat], }); - - return { prevData: prevData ? prevData[0][1] : undefined }; - }, - onSuccess: (response) => { - - queryClient.setQueryData>>([QUERY_KEYS.chat], (oldData) => { - if (oldData) { + queryClient.setQueryData>>( + [QUERY_KEYS.chat], + (oldData) => { const lastPage = oldData!.pages[oldData!.pages.length - 1]; return { @@ -77,38 +50,72 @@ const useCreateChatMessage = (queryClient: QueryClient) => { count: lastPage.count, next: lastPage.next, previous: lastPage.previous, - results: { - ...response[0], - id: Date.now() - } + results: [ + { + id: Date.now(), + content: newMessage.new_message, + sender: "user", + }, + ], }, - ...oldData!.pages + ...oldData!.pages, ], pageParams: [ ...oldData!.pageParams, { limit: 10, - offset: 0 - } - ] + offset: 0, + }, + ], }; } + ); - return oldData; - }); + return { prevData: prevData ? prevData[0][1] : undefined }; + }, + onSuccess: (response) => { + queryClient.setQueryData>>( + [QUERY_KEYS.chat], + (oldData) => { + if (oldData) { + const lastPage = + oldData!.pages[oldData!.pages.length - 1]; + return { + pages: [ + { + count: lastPage.count, + next: lastPage.next, + previous: lastPage.previous, + results: { + ...response[0], + id: Date.now(), + }, + }, + ...oldData!.pages, + ], + pageParams: [ + ...oldData!.pageParams, + { + limit: 10, + offset: 0, + }, + ], + }; + } + + return oldData; + } + ); }, onError: (err, newMessage, context) => { if (context) { - queryClient.setQueryData( - [QUERY_KEYS.chat], - context.prevData - ); + queryClient.setQueryData([QUERY_KEYS.chat], context.prevData); } }, onSettled: (newMessage) => { queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.chat] }); - } + }, }); }; diff --git a/frontend/composables/api/chat/useGetChat.ts b/frontend/composables/api/chat/useGetChat.ts index 8209937..927af3e 100644 --- a/frontend/composables/api/chat/useGetChat.ts +++ b/frontend/composables/api/chat/useGetChat.ts @@ -8,34 +8,36 @@ import { useAuth } from "~/composables/api/auth/useAuth"; export type GetBranchResponse = ApiPaginated; -const useGetBranch = ( - productId: string | number, - enabled: Ref -) => { - +const useGetBranch = (productId: string | number, enabled: Ref) => { // state const { $axios: axios } = useNuxtApp(); const { isLoggedIn } = useAuth(); - // method + // methods - const handleGetChat = async ({ productId, limit, offset }: { - productId: number | string, - limit: number, - offset: number + const handleGetChat = async ({ + productId, + limit, + offset, + }: { + productId: number | string; + limit: number; + offset: number; }) => { - - const { data } = await axios.get(`${API_ENDPOINTS.chat.messages}/${productId}`, { - params: { - offset, - limit - }, - headers: { - Authorization: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoyMTY3ODE2OTAwLCJpYXQiOjE3MzU4MTY5MDAsImp0aSI6ImQwN2E2Y2Y2NzgwZjRlNTE5NWIzOGQxMTAzYzU4NDQ3IiwidXNlcl9pZCI6NX0.slwd7ZSV7nUXEuDTYwwHUOo9ekCefwEEL4kVv2vSTFo` + const { data } = await axios.get( + `${API_ENDPOINTS.chat.messages}/${productId}`, + { + params: { + offset, + limit, + }, + headers: { + Authorization: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoyMTY3ODE2OTAwLCJpYXQiOjE3MzU4MTY5MDAsImp0aSI6ImQwN2E2Y2Y2NzgwZjRlNTE5NWIzOGQxMTAzYzU4NDQ3IiwidXNlcl9pZCI6NX0.slwd7ZSV7nUXEuDTYwwHUOo9ekCefwEEL4kVv2vSTFo`, + }, } - }); + ); return data; }; @@ -44,21 +46,22 @@ const useGetBranch = ( queryKey: [QUERY_KEYS.chat], initialPageParam: { limit: 10, - offset: 0 + offset: 0, }, - queryFn: ({ pageParam }) => handleGetChat({ - limit: pageParam.limit, - offset: pageParam.offset, - productId: productId - }), + queryFn: ({ pageParam }) => + handleGetChat({ + limit: pageParam.limit, + offset: pageParam.offset, + productId: productId, + }), getNextPageParam: (lastPage, pages) => { if (!lastPage.next) return undefined; return { limit: 10, - offset: pages.length * 10 + offset: pages.length * 10, }; - } + }, }); }; diff --git a/frontend/composables/api/product/useCreateComment.ts b/frontend/composables/api/product/useCreateComment.ts index 9443850..cd43eff 100644 --- a/frontend/composables/api/product/useCreateComment.ts +++ b/frontend/composables/api/product/useCreateComment.ts @@ -6,24 +6,27 @@ import { API_ENDPOINTS } from "~/constants"; // types export type CreateCommentRequest = { - content: string + content: string; }; const useCreateComment = (id: number | string | undefined) => { - // state const { $axios: axios } = useNuxtApp(); - // method + // methods const handleCreateComment = async (variables: CreateCommentRequest) => { - const { data } = await axios.post(`${API_ENDPOINTS.product.create_comment}/${id}`, variables); + const { data } = await axios.post( + `${API_ENDPOINTS.product.create_comment}/${id}`, + variables + ); return data; }; return useMutation({ - mutationFn: (variables: CreateCommentRequest) => handleCreateComment(variables) + mutationFn: (variables: CreateCommentRequest) => + handleCreateComment(variables), }); }; From 0e171e167bf01784b749463ba4a4e1cf593cda37 Mon Sep 17 00:00:00 2001 From: Mamalizz Date: Sun, 23 Feb 2025 23:22:42 +0330 Subject: [PATCH 02/12] new changes --- .../global/ToastContainer/ToastBox.vue | 34 ++++---- .../global/product-detail/Slider.vue | 20 +++-- .../product/ChatBox/ChatBoxContainer.vue | 30 ++++--- .../components/product/ChatBox/ChatButton.vue | 10 +-- .../components/product/ChatBox/ChatInput.vue | 43 ++++++---- .../product/ChatBox/ChatMessage.vue | 80 ++++++++++--------- .../components/product/ProductComments.vue | 32 ++++---- 7 files changed, 135 insertions(+), 114 deletions(-) diff --git a/frontend/components/global/ToastContainer/ToastBox.vue b/frontend/components/global/ToastContainer/ToastBox.vue index d12d88e..40db762 100644 --- a/frontend/components/global/ToastContainer/ToastBox.vue +++ b/frontend/components/global/ToastContainer/ToastBox.vue @@ -1,5 +1,4 @@ \ No newline at end of file + diff --git a/frontend/components/global/product-detail/Slider.vue b/frontend/components/global/product-detail/Slider.vue index e1110d2..41d0e00 100644 --- a/frontend/components/global/product-detail/Slider.vue +++ b/frontend/components/global/product-detail/Slider.vue @@ -1,11 +1,10 @@ \ No newline at end of file + diff --git a/frontend/components/product/ChatBox/ChatBoxContainer.vue b/frontend/components/product/ChatBox/ChatBoxContainer.vue index cedb98c..91eafa5 100644 --- a/frontend/components/product/ChatBox/ChatBoxContainer.vue +++ b/frontend/components/product/ChatBox/ChatBoxContainer.vue @@ -29,17 +29,17 @@ const { isPending: isChatPending, isFetchingNextPage: isNextChatPagePending, hasNextPage: hasMoreChat, - fetchNextPage: loadMoreChat + fetchNextPage: loadMoreChat, } = useGetChat(id, isOpen); const isCreateMessagePending = useIsMutating({ - mutationKey: [MUTATION_KEYS.create_chat] + mutationKey: [MUTATION_KEYS.create_chat], }); const canLoadMoreChat = ref(false); const isChatScrollLocked = useScrollLock(chatContainerEl); const { y: chatContainerScrollY } = useScroll(chatContainerEl, { - behavior: "smooth" + behavior: "smooth", }); useInfiniteScroll( @@ -56,11 +56,11 @@ useInfiniteScroll( distance: 10, direction: "top", throttle: 1000, - canLoadMore: () => canLoadMoreChat.value + canLoadMore: () => canLoadMoreChat.value, } ); -// method +// methods const scrollToBottom = () => { chatContainerScrollY.value = chatContainerEl.value?.scrollHeight ?? 0; @@ -116,7 +116,7 @@ whenever( }, 2000); }, { - once: true + once: true, } ); @@ -131,16 +131,15 @@ whenever( -
+
Please sign in first
diff --git a/frontend/components/product/ChatBox/ChatButton.vue b/frontend/components/product/ChatBox/ChatButton.vue index 22ab002..114b7c2 100644 --- a/frontend/components/product/ChatBox/ChatButton.vue +++ b/frontend/components/product/ChatBox/ChatButton.vue @@ -1,24 +1,21 @@ diff --git a/frontend/components/product/ChatBox/ChatInput.vue b/frontend/components/product/ChatBox/ChatInput.vue index 69cfea4..d59a17c 100644 --- a/frontend/components/product/ChatBox/ChatInput.vue +++ b/frontend/components/product/ChatBox/ChatInput.vue @@ -1,5 +1,4 @@ @@ -303,4 +316,4 @@ const sendMessage = async () => { transform: translate(-50%, -50%) rotate(443deg); } } - \ No newline at end of file + diff --git a/frontend/components/product/ChatBox/ChatMessage.vue b/frontend/components/product/ChatBox/ChatMessage.vue index c142dce..97ecf3b 100644 --- a/frontend/components/product/ChatBox/ChatMessage.vue +++ b/frontend/components/product/ChatBox/ChatMessage.vue @@ -1,14 +1,13 @@ \ No newline at end of file + diff --git a/frontend/components/product/ProductComments.vue b/frontend/components/product/ProductComments.vue index 2d38b65..1b0bcd8 100644 --- a/frontend/components/product/ProductComments.vue +++ b/frontend/components/product/ProductComments.vue @@ -1,5 +1,4 @@