-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from BacPacNet/userpost
add userPost Queries, skeleton
- Loading branch information
Showing
6 changed files
with
197 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from 'react' | ||
import { Skeleton } from '../ui/Skeleton' | ||
const PostSkeleton = () => { | ||
return ( | ||
<div className="border rounded-lg border-border pb-10 bg-slate-50"> | ||
<div className="my-10 ml-10 flex gap-4 items-center"> | ||
<Skeleton className=" bg-slate-400 p-2 h-14 w-14 rounded-full" /> | ||
<div> | ||
<Skeleton className="bg-slate-300 h-4 w-24" /> | ||
<Skeleton className="mt-1 bg-slate-300 h-3 w-20" /> | ||
<Skeleton className="mt-1 bg-slate-300 h-3 w-16" /> | ||
</div> | ||
</div> | ||
<Skeleton className="mx-10 h-3 bg-slate-300" /> | ||
<Skeleton className="mx-10 my-1 h-3 bg-slate-300" /> | ||
<Skeleton className="mx-10 h-3 bg-slate-300" /> | ||
</div> | ||
) | ||
} | ||
|
||
export default PostSkeleton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' | ||
import { client } from './api-Client' | ||
import axios from 'axios' | ||
import useCookie from '@/hooks/useCookie' | ||
|
||
export async function DeleteUserPost(postId: string, token: any) { | ||
const response = await client(`/userpost/${postId}`, { method: 'DELETE', headers: { Authorization: `Bearer ${token}` } }) | ||
return response | ||
} | ||
|
||
export async function UpdateUserPost(data: any, postId: string, token: any) { | ||
const response = await client(`/userpost/${postId}`, { method: 'PUT', headers: { Authorization: `Bearer ${token}` }, data }) | ||
return response | ||
} | ||
|
||
export async function LikeUnilikeUserPost(postId: string, token: any) { | ||
const response = await client(`/userpost/likeunlike/${postId}`, { method: 'PUT', headers: { Authorization: `Bearer ${token}` } }) | ||
return response | ||
} | ||
|
||
export async function getAllUserPosts(token: any) { | ||
const response: any = await client('/userpost/', { headers: { Authorization: `Bearer ${token}` } }) | ||
return response | ||
} | ||
|
||
export async function CreateUserPost(data: any, token: any) { | ||
const response = await client(`/userpost/`, { method: 'POST', headers: { Authorization: `Bearer ${token}` }, data }) | ||
return response | ||
} | ||
|
||
//Query Functions | ||
|
||
export const useDeletePost = () => { | ||
const [cookieValue] = useCookie('uni_user_token') | ||
const queryClient = useQueryClient() | ||
return useMutation({ | ||
mutationFn: (postId: any) => DeleteUserPost(postId, cookieValue), | ||
onSuccess: (response: any) => { | ||
console.log(response, 'response') | ||
|
||
queryClient.invalidateQueries({ queryKey: ['userPosts'] }) | ||
}, | ||
onError: (res: any) => { | ||
console.log(res.response.data.message, 'res') | ||
}, | ||
}) | ||
} | ||
|
||
export const useUpdateUserPost = () => { | ||
const [cookieValue] = useCookie('uni_user_token') | ||
const queryClient = useQueryClient() | ||
return useMutation({ | ||
mutationFn: ({ postData, postId }: { postData: any; postId: any }) => UpdateUserPost(postData, postId, cookieValue), | ||
|
||
onSuccess: (response: any) => { | ||
console.log(response, 'response') | ||
|
||
queryClient.invalidateQueries({ queryKey: ['userPosts'] }) | ||
}, | ||
onError: (res: any) => { | ||
console.log(res.response.data.message, 'res') | ||
}, | ||
}) | ||
} | ||
|
||
export function useGetUserPosts() { | ||
const [cookieValue] = useCookie('uni_user_token') | ||
|
||
const { isLoading, data, error } = useQuery({ | ||
queryKey: ['userPosts'], | ||
queryFn: () => getAllUserPosts(cookieValue), | ||
}) | ||
|
||
let errorMessage = null | ||
if (axios.isAxiosError(error) && error.response) { | ||
errorMessage = error.response.data | ||
} | ||
|
||
return { isLoading, data, error: errorMessage } | ||
} | ||
|
||
export const useCreateUserPost = () => { | ||
const [cookieValue] = useCookie('uni_user_token') | ||
const queryClient = useQueryClient() | ||
return useMutation({ | ||
mutationFn: (data: any) => CreateUserPost(data, cookieValue), | ||
|
||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: ['userPosts'] }) | ||
}, | ||
onError: (res: any) => { | ||
console.log(res.response.data.message, 'res') | ||
}, | ||
}) | ||
} | ||
|
||
export const useLikeUnilikeGroupPost = () => { | ||
const [cookieValue] = useCookie('uni_user_token') | ||
const queryClient = useQueryClient() | ||
return useMutation({ | ||
mutationFn: (postId: any) => LikeUnilikeUserPost(postId, cookieValue), | ||
|
||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: ['userPosts'] }) | ||
}, | ||
onError: (res: any) => { | ||
console.log(res.response.data.message, 'res') | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters