Skip to main content

useCreatePost

Hook for creating new cricket discussion posts. Returns:
createNewPost
function
Async function to create a new post. Returns the created post object.
Usage Example:
import useCreatePost from '@/hooks/useCreatePost';
import { showToast } from '@/libs/showToast';
import { PostSchema } from '@/schemas/PostSchema';

function CreatePostModal({ onClose }: Props) {
  const [content, setContent] = useState<string>("");
  const { createNewPost } = useCreatePost();

  async function handleCreatePost() {
    try {
      // Validate content
      const result = PostSchema.safeParse({ content });
      
      if (!result.success) {
        showToast({
          type: "error",
          text1: "Error",
          text2: result.error.issues[0].message,
        });
        return;
      }

      // Create the post
      await createNewPost({ content });
      onClose();
      setContent("");

      showToast({
        type: "success",
        text1: "Post Created",
        text2: "Your post has been created successfully.",
      });
    } catch (error) {
      showToast({
        type: "error",
        text1: "Error",
        text2: "Could not create post. Please try again later.",
      });
    }
  }
}
Source: /workspace/source/hooks/useCreatePost.ts:5

useDeletePost

Hook for deleting posts with confirmation dialog. Parameters:
postId
string
required
The ID of the post to delete
Returns:
handleDeletePost
function
Function that shows a confirmation dialog and deletes the post if confirmed
Usage Example:
import useDeletePost from '@/hooks/useDeletePost';

function PostCard({ post, userId }: Props) {
  const { handleDeletePost } = useDeletePost({ postId: post.$id });

  return (
    <View>
      {post.authorId === userId && (
        <Pressable onPress={handleDeletePost}>
          <Ionicons name="trash-outline" size={18} color="black" />
        </Pressable>
      )}
    </View>
  );
}
Error Handling: The hook automatically displays error toasts if the deletion fails:
  • Success: “Post deleted successfully”
  • Error: “Failed to delete the post. Please try again later.”
Source: /workspace/source/hooks/useDeletePost.ts:6

useEditPost

Hook for editing existing posts with validation and change detection. Parameters:
postId
string
required
The ID of the post to edit
Returns:
handleEditPost
function
Async function to update a post with new content
Usage Example:
import useEditPost from '@/hooks/useEditPost';

function EditPostModal({ postId, initialContent, onClose }: Props) {
  const [content, setContent] = useState<string>(initialContent);
  const { handleEditPost } = useEditPost({ postId });

  return (
    <Pressable
      onPress={() =>
        handleEditPost({ content, initialContent, onClose })
      }
    >
      <Ionicons name="checkmark" size={24} color="#0f172b" />
    </Pressable>
  );
}
Validation: The hook performs the following validations:
  • Content must pass PostSchema validation
  • Content must be different from initialContent
  • Content is automatically trimmed
Error Messages:
  • No changes: “Nothing to edit. You haven’t made any changes yet.”
  • Validation error: Displays the schema validation message
  • Edit failed: “Failed editing the post. Please try again later.”
Source: /workspace/source/hooks/useEditPost.ts:12

useLikePost

Hook for liking/unliking posts with optimistic UI updates. Returns:
likePost
function
Async function to toggle like status on a post
Usage Example:
import useLikePost from '@/hooks/useLikePost';
import { usePosts } from '@/store/usePosts';

function PostCard({ post, userId }: Props) {
  const posts = usePosts((s) => s.posts);
  const { likePost } = useLikePost();
  
  const currentPost = posts.find((p) => p.$id === post.$id);
  const hasLiked = currentPost?.likedBy.includes(userId);

  return (
    <Pressable
      onPress={() => likePost({ postId: post.$id, userId })}
    >
      <Ionicons
        name={hasLiked ? "heart-sharp" : "heart-outline"}
        size={18}
        color={hasLiked ? "red" : "gray"}
      />
      <Text>{post.likes} Likes</Text>
    </Pressable>
  );
}
Optimistic Updates: The hook immediately updates the UI before the server responds:
  • Increments/decrements the like count
  • Adds/removes the user from the likedBy array
  • Reverts changes if the server request fails
Error Handling: If the like operation fails, the hook:
  • Displays an error toast: “Could not like. Please try again later.”
  • Automatically reverts the optimistic update
Source: /workspace/source/hooks/useLikePost.ts:6

useViewPost

Hook for tracking post views with optimistic updates. Returns:
incrementView
function
Async function to increment the view count for a post
Usage Example:
import useViewPost from '@/hooks/useViewPost';

function PostDetailScreen({ postId, userId }: Props) {
  const { incrementView } = useViewPost();

  useEffect(() => {
    // Track view when component mounts
    incrementView({ postId, userId });
  }, [postId, userId]);

  return (
    <View>
      {/* Post content */}
    </View>
  );
}
Behavior:
  • Only increments view count once per user (checks viewedBy array)
  • Uses optimistic updates for immediate UI feedback
  • Silently reverts on error (no toast notifications)
  • Returns early if post not found or user already viewed
Source: /workspace/source/hooks/useViewPost.ts:4

Build docs developers (and LLMs) love