@@ -5,88 +5,20 @@ import AddGroup from "./Sections/AddGroup";
55import GroupCard from "./Sections/GroupCard" ;
66import { db } from "../../utils/firebase" ;
77import { AuthContext } from "../../components/Authentication/AuthProvider" ;
8+ import { DataContext } from "../../components/Authentication/DataProvider" ;
89import AddButton from "../../components/AddButton" ;
910import Loader from "../../components/Loader" ;
1011import img1 from "../../assets/images/i5.jpeg"
1112import img2 from "../../assets/images/p3.jpeg"
1213import img3 from "../../assets/images/t3.jpeg"
13- // export const DataContext = createContext();
14- // export const DataProvider = ({ children }) => {
15- // const { user } = useContext(AuthContext);
16- // const [currentGroup, setCurrentGroup] = useState('');
17- // const [transactions, setTransactions] = useState([]);
18- // const [users, setUsers] = useState({});
19- // const [friends, setFriends] = useState([]);
20- // useEffect(() => {
21- // const temp = [];
22- // const tempFriends = []
23- // db.collection("transactions")
24- // .where("involved", "array-contains", user.uid)
25- // .get()
26- // .then((querySnapshot) => {
27- // querySnapshot.forEach((doc) => {
28- // // console.log(doc.id, " => ", doc.data());
29- // const id = doc.id;
30- // const data = doc.data();
31- // const finalTransaction = {
32- // id,
33- // data,
34- // }
35- // data.involved.forEach(personId => {
36- // if (tempFriends.includes(personId)) { }
37- // else tempFriends.push(personId)
38- // });
39- // temp.push(finalTransaction);
40- // })
41- // })
42- // .catch((error) => {
43- // console.log("Error getting documents: ", error);
44- // })
45- // .finally(() => {
46- // setFriends(tempFriends);
47- // setTransactions(temp);
48- // });
49-
50-
51- // }, [user]);
52- // useEffect(() => {
53- // const temp = {}
54- // if (friends.length) db.collection("users")
55- // .where("userId", "in", friends)
56- // .get()
57- // .then((querySnapshot) => {
58- // querySnapshot.forEach((doc) => {
59- // // doc.data() is never undefined for query doc snapshots
60- // // console.log(doc.id, " => ", doc.data());
61- // const data = doc.data();
62- // const dataWithAmt = { ...data, amount: 0 };
63- // temp[data.userId] = dataWithAmt;
64- // // setUsers((prevUsers) => {
65- // // return { ...prevUsers, [id]: dataWithAmt, }
66- // // })
67- // });
68- // })
69- // .catch((error) => {
70- // console.log("Error getting documents: ", error);
71- // })
72- // .finally(() => {
73- // setUsers(temp)
74- // // setLoading(false)
75- // })
76- // }, [friends]);
77-
78- // return (
79- // <DataContext.Provider value={{ transactions, setTransactions, users, setUsers, currentGroup, setCurrentGroup }} >
80- // {children}
81- // </DataContext.Provider>
82- // )
83- // }
8414
8515const colorss = [ "#B794F4" , "#c5fb9d" , "#f5a9b8" , "#faf089" , "#90D2DA" , "#FEB2B2" , "#FBD38D" , "#DA90CB" , "#DB90CB" ]
8616
8717const Home = ( ) => {
8818 const { user } = useContext ( AuthContext ) ;
19+ const { transactions} = useContext ( DataContext ) ;
8920 const [ groups , setGroups ] = useState ( [ ] ) ;
21+ const [ amount , setAmount ] = useState ( { total :0 , iOwe :0 , peopleOwe :0 } )
9022 const [ loading , setLoading ] = useState ( true ) ;
9123 const [ show , setShow ] = useState ( false ) ;
9224 const handleClose = ( ) => setShow ( false ) ;
@@ -113,6 +45,26 @@ const Home = () => {
11345
11446 } , [ user ] ) ;
11547
48+ useEffect ( ( ) => {
49+ let total = 0 , negative = 0 , positive = 0 ;
50+ transactions . forEach ( ( transaction ) => {
51+ const amt = transaction . data . amount / transaction . data . involved . length
52+ if ( transaction . data . paidBy === user . uid ) {
53+ positive += amt
54+ total += amt
55+ } else {
56+ negative -= amt
57+ total += amt
58+ }
59+ } )
60+ const updatedAmount = {
61+ total :total ,
62+ iOwe :negative ,
63+ peopleOwe :positive ,
64+ }
65+ setAmount ( updatedAmount )
66+ } , [ transactions , user ] )
67+
11668 // const [spent, setSpent] = useState(0);
11769 // const [remain, setRemain] = useState(0);
11870 // const spentLink = "/";
@@ -125,21 +77,24 @@ const Home = () => {
12577 className = "fbcard"
12678 imgUrl = { img3 }
12779 head = "Total Expenditure"
128- text = "₹ 10000"
80+ text = { `₹ ${ amount . total } ` }
81+ textColor = { 'info' }
12982 link = "/transaction"
13083 />
13184 < Card
13285 className = "fbcard"
13386 imgUrl = { img2 }
13487 head = "I OWE PEOPLE"
135- text = "₹ 3000"
88+ text = { `₹ ${ amount . iOwe } ` }
89+ textColor = { 'danger' }
13690 link = "/transaction"
13791 />
13892 < Card
13993 className = "fbcard"
14094 imgUrl = { img1 }
14195 head = "PEOPLE OWE ME"
142- text = "₹ 800"
96+ text = { `₹ ${ amount . peopleOwe } ` }
97+ textColor = { 'success' }
14398 link = "/user"
14499 />
145100 </ Container >
0 commit comments