mirror of
https://github.com/urosran/cally.git
synced 2025-11-27 08:54:54 +00:00
Shopping List backend implementation
- Added creatorId to the grocery item in db and showed the name of the user that requested to add the new grocery
This commit is contained in:
@ -6,6 +6,8 @@ import EditGroceryFrequency from "./EditGroceryFrequency";
|
||||
import EditGroceryItem from "./EditGroceryItem";
|
||||
import {StyleSheet} from "react-native";
|
||||
import {IGrocery} from "@/hooks/firebase/types/groceryData";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import {UserProfile} from "@/hooks/firebase/types/profileTypes";
|
||||
|
||||
const GroceryItem = ({
|
||||
item,
|
||||
@ -22,6 +24,7 @@ const GroceryItem = ({
|
||||
const [category, setCategory] = useState<GroceryCategory>(
|
||||
GroceryCategory.None
|
||||
);
|
||||
const [itemCreator, setItemCreator] = useState<UserProfile>(null);
|
||||
|
||||
const handleTitleChange = (newTitle: string) => {
|
||||
updateGroceryItem({id: item?.id, title: newTitle});
|
||||
@ -33,8 +36,23 @@ const GroceryItem = ({
|
||||
|
||||
useEffect(() => {
|
||||
setNewTitle(item.title);
|
||||
|
||||
getItemCreator(item?.creatorId);
|
||||
}, []);
|
||||
|
||||
const getItemCreator = async (uid: string | undefined) => {
|
||||
if (uid) {
|
||||
const documentSnapshot = await firestore()
|
||||
.collection("Profiles")
|
||||
.doc(uid)
|
||||
.get();
|
||||
|
||||
if (documentSnapshot.exists) {
|
||||
setItemCreator(documentSnapshot.data() as UserProfile)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<View
|
||||
key={item.id}
|
||||
@ -122,7 +140,7 @@ const GroceryItem = ({
|
||||
}}
|
||||
></View>
|
||||
<Text color="#858585" text70>
|
||||
Requested by Austin
|
||||
Requested by {itemCreator?.firstName}
|
||||
</Text>
|
||||
</View>
|
||||
</View>
|
||||
|
||||
@ -9,4 +9,5 @@ export interface IGrocery {
|
||||
frequency: GroceryFrequency;
|
||||
bought: boolean;
|
||||
familyId?: string,
|
||||
creatorId?: string
|
||||
}
|
||||
@ -4,7 +4,7 @@ import { useAuthContext } from "@/contexts/AuthContext";
|
||||
import {IGrocery} from "@/hooks/firebase/types/groceryData";
|
||||
|
||||
export const useCreateGrocery = () => {
|
||||
const { profileData } = useAuthContext();
|
||||
const { user: currentUser, profileData } = useAuthContext();
|
||||
const queryClients = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
@ -14,7 +14,7 @@ export const useCreateGrocery = () => {
|
||||
const newDoc = firestore().collection('Groceries').doc();
|
||||
await firestore()
|
||||
.collection("Groceries")
|
||||
.add({...groceryData, id: newDoc.id, familyId: profileData?.familyId})
|
||||
.add({...groceryData, id: newDoc.id, familyId: profileData?.familyId, creatorId: currentUser?.uid})
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
|
||||
@ -23,7 +23,8 @@ export const useGetGroceries = () => {
|
||||
approved: data.approved,
|
||||
bought: data.bought,
|
||||
recurring: data.recurring,
|
||||
frequency: data.frequency
|
||||
frequency: data.frequency,
|
||||
creatorId: data.creatorId
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@ -8,7 +8,6 @@ export const useUpdateGrocery = () => {
|
||||
return useMutation({
|
||||
mutationKey: ["updateGrocery"],
|
||||
mutationFn: async (groceryData: Partial<IGrocery>) => {
|
||||
console.log(groceryData);
|
||||
try {
|
||||
await firestore()
|
||||
.collection("Groceries")
|
||||
|
||||
Reference in New Issue
Block a user