|
|
|
@ -35,8 +35,9 @@ const getDefaultProps = () => {
@@ -35,8 +35,9 @@ const getDefaultProps = () => {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const useFlatList = <T>(props: IProps<T>) => { |
|
|
|
|
if (!props.fetchItems) |
|
|
|
|
if (!props.fetchItems) { |
|
|
|
|
throw new Error('Use flat list need a fetchItems function') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
props = Object.assign(getDefaultProps(), props) |
|
|
|
|
|
|
|
|
@ -58,8 +59,11 @@ export const useFlatList = <T>(props: IProps<T>) => {
@@ -58,8 +59,11 @@ export const useFlatList = <T>(props: IProps<T>) => {
|
|
|
|
|
useEffect(() => { |
|
|
|
|
if (props.skeletonDataKey) { |
|
|
|
|
const items = skeletonDataService.get(props.skeletonDataKey) |
|
|
|
|
if (items) setItems(_.defaultTo(items, [])) |
|
|
|
|
else setLoading(true) |
|
|
|
|
if (items) { |
|
|
|
|
setItems(_.defaultTo(items, [])) |
|
|
|
|
} else { |
|
|
|
|
setLoading(true) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, []) |
|
|
|
|
|
|
|
|
@ -67,23 +71,31 @@ export const useFlatList = <T>(props: IProps<T>) => {
@@ -67,23 +71,31 @@ export const useFlatList = <T>(props: IProps<T>) => {
|
|
|
|
|
const { count, page, limit } = loadParams.current |
|
|
|
|
|
|
|
|
|
if (firstFetch) { |
|
|
|
|
if (isInit || !props.skeletonDataKey) setLoading(true) |
|
|
|
|
if (isInit || !props.skeletonDataKey) { |
|
|
|
|
setLoading(true) |
|
|
|
|
} |
|
|
|
|
loadParams.current.page = defaultProps.page |
|
|
|
|
loadParams.current.count = undefined |
|
|
|
|
} else if (blockLoadingRef.current) return |
|
|
|
|
else if ((count && page > Math.ceil(count / limit)) || count === 0) |
|
|
|
|
} else if (blockLoadingRef.current) { |
|
|
|
|
return |
|
|
|
|
} else if ((count && page > Math.ceil(count / limit)) || count === 0) { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setIsLoadingNext(true) |
|
|
|
|
|
|
|
|
|
blockLoadingRef.current = true |
|
|
|
|
try { |
|
|
|
|
if (props.clearWhenReload) setItems([]) |
|
|
|
|
if (props.clearWhenReload) { |
|
|
|
|
setItems([]) |
|
|
|
|
} |
|
|
|
|
const response = props.serrializatorResponse( |
|
|
|
|
await props.fetchItems({ params: loadParams.current }), |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if (!response) throw {} |
|
|
|
|
if (!response) { |
|
|
|
|
throw {} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
loadParams.current = { |
|
|
|
|
...props.loadParams, |
|
|
|
@ -94,19 +106,24 @@ export const useFlatList = <T>(props: IProps<T>) => {
@@ -94,19 +106,24 @@ export const useFlatList = <T>(props: IProps<T>) => {
|
|
|
|
|
} |
|
|
|
|
const fetchedItems = props.serrializatorItems(response.data.items) |
|
|
|
|
|
|
|
|
|
if (firstFetch) setItems(fetchedItems) |
|
|
|
|
else setItems([...items, ...fetchedItems]) |
|
|
|
|
if (firstFetch) { |
|
|
|
|
setItems(fetchedItems) |
|
|
|
|
} else { |
|
|
|
|
setItems([...items, ...fetchedItems]) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setTotalCount(response.data.count) |
|
|
|
|
|
|
|
|
|
if (!isInit) { |
|
|
|
|
if (props.skeletonDataKey) |
|
|
|
|
if (props.skeletonDataKey) { |
|
|
|
|
skeletonDataService.set(props.skeletonDataKey, fetchedItems) |
|
|
|
|
} |
|
|
|
|
setIsInit(true) |
|
|
|
|
} |
|
|
|
|
} catch (e) { |
|
|
|
|
console.log(e) |
|
|
|
|
setItems([]) |
|
|
|
|
loadParams.current.count = 0 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
blockLoadingRef.current = false |
|
|
|
@ -131,11 +148,15 @@ export const useFlatList = <T>(props: IProps<T>) => {
@@ -131,11 +148,15 @@ export const useFlatList = <T>(props: IProps<T>) => {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
if (props.needInit) fetchItems(true) |
|
|
|
|
if (props.needInit) { |
|
|
|
|
fetchItems(true) |
|
|
|
|
} |
|
|
|
|
}, []) |
|
|
|
|
|
|
|
|
|
const loadAll = () => { |
|
|
|
|
if (!loadParams.current.count) return |
|
|
|
|
if (!loadParams.current.count) { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
setLoadParams({ limit: loadParams.current.count }) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|