Last active
June 12, 2021 18:52
Revisions
-
whisher revised this gist
Jun 12, 2021 . 1 changed file with 1 addition and 1 deletion.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 charactersOriginal file line number Diff line number Diff line change @@ -71,7 +71,7 @@ export { useUsers, useUserFollow, useUserUnFollow }; // Component const { data: posts, isError:isErrorPosts, isLoading: isLoadingPosts } = usePosts(); const createPostMutation = useCreatePost(); const handlerSubmitData = (data: PostDataDto) => { -
whisher created this gist
Jun 5, 2021 .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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,91 @@ // 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 }; 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); } };