Skip to content

Instantly share code, notes, and snippets.

@whisher
Last active June 12, 2021 18:52

Revisions

  1. whisher revised this gist Jun 12, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original 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) => {
  2. whisher created this gist Jun 5, 2021.
    91 changes: 91 additions & 0 deletions gistfile1.txt
    Original 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);
    }
    };