Last active
June 12, 2021 18:52
-
-
Save whisher/bd76ad47f4da37f5da3fdd1378d66cf4 to your computer and use it in GitHub Desktop.
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
// POSTS | |
const useCreatePost = () => { | |
const axios = useAxios(); | |
const queryClient = useQueryClient(); | |
return useMutation( | |
(post: PostDataDto) => { | |
let data; | |
if (post.image) { | |
const formData = new FormData(); | |
formData.append('text', post.text); | |
formData.append('image', post.image); | |
data = formData; | |
} else { | |
data = post; | |
} | |
return axios.post(`${URL_POST}`, data).then((res) => res.data); | |
}, | |
{ | |
onSuccess: () => { | |
queryClient.invalidateQueries('posts'); | |
}, | |
} | |
); | |
}; | |
const useDeletePost = () => { | |
const axios = useAxios(); | |
const queryClient = useQueryClient(); | |
return useMutation((post: PostDto) => axios.delete(`${URL_POST}/${post._id}`).then((res) => res.data), { | |
onSuccess: () => queryClient.invalidateQueries('posts'), | |
}); | |
}; | |
const usePosts = () => { | |
const axios = useAxios(); | |
return useQuery('posts', async () => { | |
const { data } = await axios.get(`${URL_POST}/by`); | |
return data; | |
}); | |
}; | |
// USERS | |
const useUsers = () => { | |
const axios = useAxios(); | |
return useQuery('users', async () => { | |
const { data } = await axios.get(`${URL_USERS}`); | |
return data; | |
}); | |
}; | |
const useUserFollow = () => { | |
const axios = useAxios(); | |
const queryClient = useQueryClient(); | |
return useMutation((user: UserDto) => axios.put(`${URL_USERS_FOLLOW}`, user).then((res) => res.data), { | |
onSuccess: () => { | |
queryClient.invalidateQueries('users'); | |
}, | |
}); | |
}; | |
const useUserUnFollow = () => { | |
const axios = useAxios(); | |
const queryClient = useQueryClient(); | |
return useMutation((user: UserDto) => axios.put(`${URL_USERS_UNFOLLOW}`, user).then((res) => res.data), { | |
onSuccess: () => { | |
queryClient.invalidateQueries('users'); | |
}, | |
}); | |
}; | |
export { useUsers, useUserFollow, useUserUnFollow }; | |
// Component | |
const { data: posts, isError:isErrorPosts, isLoading: isLoadingPosts } = usePosts(); | |
const createPostMutation = useCreatePost(); | |
const handlerSubmitData = (data: PostDataDto) => { | |
createPostMutation.mutate(data); | |
}; | |
const { data: users, isError:isErrorUsers, isLoading: isLoadingUsers } = useUsers(); | |
const userFollowMutation = useUserFollow(); | |
const userUnFollowMutation = useUserUnFollow(); | |
const handlerUserStatus = (data: UserStatusDto) => { | |
if (data.status === UserStatus.FOLLOW) { | |
userFollowMutation.mutate(data.user); | |
} else { | |
userUnFollowMutation.mutate(data.user); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment