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 EditGroceryItem from "./EditGroceryItem";
|
||||||
import {StyleSheet} from "react-native";
|
import {StyleSheet} from "react-native";
|
||||||
import {IGrocery} from "@/hooks/firebase/types/groceryData";
|
import {IGrocery} from "@/hooks/firebase/types/groceryData";
|
||||||
|
import firestore from "@react-native-firebase/firestore";
|
||||||
|
import {UserProfile} from "@/hooks/firebase/types/profileTypes";
|
||||||
|
|
||||||
const GroceryItem = ({
|
const GroceryItem = ({
|
||||||
item,
|
item,
|
||||||
@ -22,6 +24,7 @@ const GroceryItem = ({
|
|||||||
const [category, setCategory] = useState<GroceryCategory>(
|
const [category, setCategory] = useState<GroceryCategory>(
|
||||||
GroceryCategory.None
|
GroceryCategory.None
|
||||||
);
|
);
|
||||||
|
const [itemCreator, setItemCreator] = useState<UserProfile>(null);
|
||||||
|
|
||||||
const handleTitleChange = (newTitle: string) => {
|
const handleTitleChange = (newTitle: string) => {
|
||||||
updateGroceryItem({id: item?.id, title: newTitle});
|
updateGroceryItem({id: item?.id, title: newTitle});
|
||||||
@ -33,8 +36,23 @@ const GroceryItem = ({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setNewTitle(item.title);
|
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 (
|
return (
|
||||||
<View
|
<View
|
||||||
key={item.id}
|
key={item.id}
|
||||||
@ -122,7 +140,7 @@ const GroceryItem = ({
|
|||||||
}}
|
}}
|
||||||
></View>
|
></View>
|
||||||
<Text color="#858585" text70>
|
<Text color="#858585" text70>
|
||||||
Requested by Austin
|
Requested by {itemCreator?.firstName}
|
||||||
</Text>
|
</Text>
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
|
|||||||
@ -9,4 +9,5 @@ export interface IGrocery {
|
|||||||
frequency: GroceryFrequency;
|
frequency: GroceryFrequency;
|
||||||
bought: boolean;
|
bought: boolean;
|
||||||
familyId?: string,
|
familyId?: string,
|
||||||
|
creatorId?: string
|
||||||
}
|
}
|
||||||
@ -4,7 +4,7 @@ import { useAuthContext } from "@/contexts/AuthContext";
|
|||||||
import {IGrocery} from "@/hooks/firebase/types/groceryData";
|
import {IGrocery} from "@/hooks/firebase/types/groceryData";
|
||||||
|
|
||||||
export const useCreateGrocery = () => {
|
export const useCreateGrocery = () => {
|
||||||
const { profileData } = useAuthContext();
|
const { user: currentUser, profileData } = useAuthContext();
|
||||||
const queryClients = useQueryClient();
|
const queryClients = useQueryClient();
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
@ -14,7 +14,7 @@ export const useCreateGrocery = () => {
|
|||||||
const newDoc = firestore().collection('Groceries').doc();
|
const newDoc = firestore().collection('Groceries').doc();
|
||||||
await firestore()
|
await firestore()
|
||||||
.collection("Groceries")
|
.collection("Groceries")
|
||||||
.add({...groceryData, id: newDoc.id, familyId: profileData?.familyId})
|
.add({...groceryData, id: newDoc.id, familyId: profileData?.familyId, creatorId: currentUser?.uid})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,7 +23,8 @@ export const useGetGroceries = () => {
|
|||||||
approved: data.approved,
|
approved: data.approved,
|
||||||
bought: data.bought,
|
bought: data.bought,
|
||||||
recurring: data.recurring,
|
recurring: data.recurring,
|
||||||
frequency: data.frequency
|
frequency: data.frequency,
|
||||||
|
creatorId: data.creatorId
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,7 +8,6 @@ export const useUpdateGrocery = () => {
|
|||||||
return useMutation({
|
return useMutation({
|
||||||
mutationKey: ["updateGrocery"],
|
mutationKey: ["updateGrocery"],
|
||||||
mutationFn: async (groceryData: Partial<IGrocery>) => {
|
mutationFn: async (groceryData: Partial<IGrocery>) => {
|
||||||
console.log(groceryData);
|
|
||||||
try {
|
try {
|
||||||
await firestore()
|
await firestore()
|
||||||
.collection("Groceries")
|
.collection("Groceries")
|
||||||
|
|||||||
Reference in New Issue
Block a user