[FIX] Searching for Discussion

This commit is contained in:
Reinaldo Neto 2023-01-20 15:32:15 -03:00
parent 593d12b129
commit f81cb6d75b
1 changed files with 11 additions and 9 deletions

View File

@ -1,4 +1,4 @@
import React, { useEffect, useLayoutEffect, useState } from 'react'; import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
import { FlatList, StyleSheet } from 'react-native'; import { FlatList, StyleSheet } from 'react-native';
import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack'; import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
import { HeaderBackButton } from '@react-navigation/elements'; import { HeaderBackButton } from '@react-navigation/elements';
@ -47,7 +47,7 @@ const DiscussionsView = ({ navigation, route }: IDiscussionsViewProps): React.Re
const [search, setSearch] = useState<IMessageFromServer[]>([]); const [search, setSearch] = useState<IMessageFromServer[]>([]);
const [isSearching, setIsSearching] = useState(false); const [isSearching, setIsSearching] = useState(false);
const [total, setTotal] = useState(0); const [total, setTotal] = useState(0);
const [searchTotal, setSearchTotal] = useState(0); const offset = useRef(0);
const { colors } = useTheme(); const { colors } = useTheme();
@ -60,19 +60,19 @@ const DiscussionsView = ({ navigation, route }: IDiscussionsViewProps): React.Re
try { try {
const result = await Services.getDiscussions({ const result = await Services.getDiscussions({
roomId: rid, roomId: rid,
offset: isSearching ? search.length : discussions.length, offset: offset.current,
count: API_FETCH_COUNT, count: API_FETCH_COUNT,
text text
}); });
if (result.success) { if (result.success) {
if (isSearching) { if (isSearching) {
setSearch(result.messages); setSearch(prevState => (offset.current ? [...prevState, ...result.messages] : result.messages));
setSearchTotal(result.total);
} else { } else {
setDiscussions(result.messages); setDiscussions(result.messages);
setTotal(result.total);
} }
offset.current += API_FETCH_COUNT;
setTotal(result.total);
} }
setLoading(false); setLoading(false);
} catch (e) { } catch (e) {
@ -83,13 +83,15 @@ const DiscussionsView = ({ navigation, route }: IDiscussionsViewProps): React.Re
const onSearchChangeText = useDebounce(async (text: string) => { const onSearchChangeText = useDebounce(async (text: string) => {
setIsSearching(true); setIsSearching(true);
offset.current = 0;
setSearch([]);
await load(text); await load(text);
}, 500); }, 500);
const onCancelSearchPress = () => { const onCancelSearchPress = () => {
setIsSearching(false);
setSearch([]); setSearch([]);
setSearchTotal(0); setIsSearching(false);
offset.current = 0;
}; };
const onSearchPress = () => { const onSearchPress = () => {
@ -186,7 +188,7 @@ const DiscussionsView = ({ navigation, route }: IDiscussionsViewProps): React.Re
contentContainerStyle={styles.contentContainer} contentContainerStyle={styles.contentContainer}
onEndReachedThreshold={0.5} onEndReachedThreshold={0.5}
removeClippedSubviews={isIOS} removeClippedSubviews={isIOS}
onEndReached={() => (isSearching ? searchTotal : total) > API_FETCH_COUNT ?? load()} onEndReached={() => isSearching && offset.current < total && load()}
ItemSeparatorComponent={List.Separator} ItemSeparatorComponent={List.Separator}
ListFooterComponent={loading ? <ActivityIndicator /> : null} ListFooterComponent={loading ? <ActivityIndicator /> : null}
scrollIndicatorInsets={{ right: 1 }} scrollIndicatorInsets={{ right: 1 }}