-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathuseFetchData.js
55 lines (53 loc) · 2.04 KB
/
useFetchData.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { useRef, useState } from "react";
export default function useFetchData(limitNumber, get,initCount, snapHandler, name) {
const limit = useRef(0);
const dataUnsub = useRef();
const [data, setData] = useState(null);
const snapCount = useRef(0);
return [data, function fetchData(setScrollFetch) {
if (limit.current || limit.current === 0) {
if (limit.current === "limit attended") {
setScrollFetch(false);
limit.current = null;
} else {
setScrollFetch(true);
limit.current = limit.current + limitNumber;
};
if (dataUnsub.current) {
dataUnsub.current();
};
if (initCount) {
snapCount.current = 0;
};
let timeout = null;
const getData = limit.current ? get.limit(limit.current) : get;
dataUnsub.current = getData.onSnapshot(snapshot => {
snapCount.current++;
const s = () => {
const arr = snapHandler(snapshot);
setData(arr);
setScrollFetch(false);
if ((name === "users" && arr.length < limit.current - 1) || (name !== "users" && arr.length < limit.current)) {
limit.current = "limit attended";
fetchData(setScrollFetch)
};
};
if (snapshot.docs?.length > 0) {
if (snapCount.current >= 2) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
s();
} else {
timeout = setTimeout(s, 1500)
};
} else if (snapshot.docs?.length === 0) {
setData([]);
setScrollFetch(false);
};
});
};
return dataUnsub;
}, setData];
};