Skip to content

Commit 510d131

Browse files
committed
removed bug in friends list
1 parent bb04c4d commit 510d131

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

src/components/Authentication/DataProvider.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const DataProvider = ({ children }) => {
1515
const [requestUsers, setRequestUsers] = useState(true);
1616
useEffect(() => {
1717
const temp = [];
18-
const tempFriends = []
18+
// const tempFriends = []
1919
db.collection("transactions")
2020
.where("involved", "array-contains", user.uid)
2121
.get()
@@ -28,18 +28,18 @@ export const DataProvider = ({ children }) => {
2828
id,
2929
data,
3030
}
31-
data.involved.forEach(personId => {
32-
if (tempFriends.includes(personId)) { }
33-
else tempFriends.push(personId)
34-
});
31+
// data.involved.forEach(personId => {
32+
// if (tempFriends.includes(personId)) { }
33+
// else tempFriends.push(personId)
34+
// });
3535
temp.push(finalTransaction);
3636
})
3737
})
3838
.catch((error) => {
3939
console.log("Error getting documents: ", error);
4040
})
4141
.finally(() => {
42-
setFriends(tempFriends);
42+
// setFriends(tempFriends);
4343
setTransactions(temp);
4444
});
4545

@@ -71,20 +71,27 @@ export const DataProvider = ({ children }) => {
7171
})
7272
}, [friends, requestUsers]);
7373
useEffect(() => {
74-
let temp = [];
74+
const temp = [];
75+
const tempFriends = []
7576
db.collection("groups")
7677
.where("members", "array-contains", user.uid)
7778
.get()
7879
.then((data) => {
7980
data.forEach((doc) => {
8081
// doc.data() is never undefined for query doc snapshots
8182
temp.push({ id: doc.id, data: doc.data() });
83+
const data = doc.data();
84+
data.members.forEach(personId => {
85+
if (tempFriends.includes(personId)) { }
86+
else tempFriends.push(personId)
87+
});
8288
});
8389
})
8490
.catch((error) => {
8591
console.log("Error getting documents: ", error);
8692
})
8793
.finally(() => {
94+
setFriends(tempFriends);
8895
setGroups(temp)
8996
});
9097
}, [user, requestGroups]);

src/pages/Home/index.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ const colorss = ["#B794F4", "#c5fb9d", "#f5a9b8", "#faf089", "#90D2DA", "#FEB2B2
1616

1717
const Home = () => {
1818
const { user } = useContext(AuthContext);
19-
const { transactions}=useContext(DataContext);
19+
const { transactions } = useContext(DataContext);
2020
const [groups, setGroups] = useState([]);
21-
const [amount,setAmount]=useState({total:0,iOwe:0,peopleOwe:0})
21+
const [amount, setAmount] = useState({ total: 0, iOwe: 0, peopleOwe: 0 })
2222
const [loading, setLoading] = useState(true);
2323
const [show, setShow] = useState(false);
2424
const handleClose = () => setShow(false);
2525
const handleShow = () => setShow(true);
2626

2727
useEffect(() => {
28-
let temp = [];
28+
const temp = [];
2929
db.collection("groups")
3030
.where("members", "array-contains", user.uid)
3131
.get()
@@ -45,25 +45,25 @@ const Home = () => {
4545

4646
}, [user]);
4747

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
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,
5864
}
59-
})
60-
const updatedAmount={
61-
total:total,
62-
iOwe:negative,
63-
peopleOwe:positive,
64-
}
65-
setAmount(updatedAmount)
66-
},[transactions,user])
65+
setAmount(updatedAmount)
66+
}, [transactions, user])
6767

6868
// const [spent, setSpent] = useState(0);
6969
// const [remain, setRemain] = useState(0);

0 commit comments

Comments
 (0)