useAddTopic Hook logic - abhiram-shaji/Langroove GitHub Wiki
This documentation outlines the implementation of the useAddTopic
hook, which handles adding new topics to Firestore in a React Native Firebase application.
The useAddTopic
hook:
- Manages the input for a topic description.
- Handles validation and error reporting for adding topics.
- Saves new topics to Firestore and navigates back to the previous screen after a successful addition.
- Manages the topic description input.
-
State Variables:
-
description
: Stores the topic description entered by the user.
-
-
Code Snippet:
const [description, setDescription] = useState<string>('');
-
Process:
- The
description
state variable is updated using thesetDescription
method as the user types.
- The
- Ensures the topic description is provided and the user is authenticated before saving.
-
Validation Steps:
- Checks if the
description
is non-empty. - Ensures the user is logged in by verifying the presence of
auth.currentUser
.
- Checks if the
-
Code Snippet:
if (!description) { Alert.alert('Error', 'Please enter a description'); return; } const currentUser = auth.currentUser; if (!currentUser) { Alert.alert('Error', 'You must be logged in to add a topic'); return; }
-
Process:
- Displays an alert for missing descriptions or unauthenticated users.
- Prevents further execution if validation fails.
- Saves the new topic to the Firestore
topics
collection with a unique ID.
-
Functionality:
- Creates a topic document with fields:
-
description
: The entered topic description. -
ownerId
: The UID of the current user. -
ownerName
: The display name of the current user or "Anonymous." -
createdAt
: A server-side timestamp.
-
- Creates a topic document with fields:
-
Code Snippet:
const newTopic = { description, ownerId: currentUser.uid, ownerName: currentUser.displayName || 'Anonymous', createdAt: serverTimestamp(), }; try { const topicDocRef = doc(db, 'topics', `${currentUser.uid}-${Date.now()}`); await setDoc(topicDocRef, newTopic); Alert.alert('Success', 'Topic added successfully'); navigation.goBack(); } catch (error) { console.error('Error saving topic:', error); Alert.alert('Error', 'Failed to save topic. Please try again.'); }
-
Process:
- Generates a unique document ID using the user's UID and the current timestamp.
- Saves the topic document using
setDoc
.
- Navigates back to the previous screen (e.g.,
FeedScreen
) after a successful topic addition.
-
Functionality:
- Uses the
navigation.goBack()
method to return to the previous screen.
- Uses the
-
Code Snippet:
navigation.goBack();
-
Process:
- Triggered after a successful Firestore write operation.
Property/Method | Description |
---|---|
description |
Stores the topic description entered by the user. |
setDescription |
Updates the description state variable. |
handleAddTopic |
Validates input, saves the topic to Firestore, and navigates back. |
-
Input Errors:
- Displays alerts if the description is missing or the user is not logged in.
-
Firestore Errors:
- Logs errors and displays an alert if saving the topic to Firestore fails.
- The document ID is generated using the current user's UID and a timestamp to ensure uniqueness.
- If the current user has no display name, the owner name defaults to "Anonymous."
For more details on Firebase Firestore methods, visit the Firebase Firestore Documentation. For React Navigation, refer to the React Navigation Documentation.