From 3f13c5ce785122e80140062dd77fae45d1751805 Mon Sep 17 00:00:00 2001 From: Reinaldo Neto <47038980+reinaldonetof@users.noreply.github.com> Date: Thu, 16 Mar 2023 20:30:58 -0300 Subject: [PATCH] [FIX] Searching for Discussion (#4843) * [FIX] Searching for Discussion * minor tweak * fixed pagination * update typescript return * remove await from load --- app/definitions/rest/v1/chat.ts | 1 + app/views/DiscussionsView/index.tsx | 33 +++++++++++++++++------------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/app/definitions/rest/v1/chat.ts b/app/definitions/rest/v1/chat.ts index 16f568756..4a15a5619 100644 --- a/app/definitions/rest/v1/chat.ts +++ b/app/definitions/rest/v1/chat.ts @@ -33,6 +33,7 @@ export type ChatEndpoints = { GET: (params: { roomId: IServerRoom['_id']; text?: string; offset: number; count: number }) => { messages: IMessageFromServer[]; total: number; + count: number; }; }; 'chat.getThreadsList': { diff --git a/app/views/DiscussionsView/index.tsx b/app/views/DiscussionsView/index.tsx index dee6e3534..ea937eaa0 100644 --- a/app/views/DiscussionsView/index.tsx +++ b/app/views/DiscussionsView/index.tsx @@ -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 { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack'; import { HeaderBackButton } from '@react-navigation/elements'; @@ -46,12 +46,13 @@ const DiscussionsView = ({ navigation, route }: IDiscussionsViewProps): React.Re const [discussions, setDiscussions] = useState([]); const [search, setSearch] = useState([]); const [isSearching, setIsSearching] = useState(false); - const [total, setTotal] = useState(0); - const [searchTotal, setSearchTotal] = useState(0); + const total = useRef(0); + const searchText = useRef(''); + const offset = useRef(0); const { colors } = useTheme(); - const load = async (text = '') => { + const load = async () => { if (loading) { return; } @@ -60,18 +61,18 @@ const DiscussionsView = ({ navigation, route }: IDiscussionsViewProps): React.Re try { const result = await Services.getDiscussions({ roomId: rid, - offset: isSearching ? search.length : discussions.length, + offset: offset.current, count: API_FETCH_COUNT, - text + text: searchText.current }); if (result.success) { + offset.current += result.count; + total.current = result.total; if (isSearching) { - setSearch(result.messages); - setSearchTotal(result.total); + setSearch(prevState => (offset.current ? [...prevState, ...result.messages] : result.messages)); } else { setDiscussions(result.messages); - setTotal(result.total); } } setLoading(false); @@ -81,15 +82,19 @@ const DiscussionsView = ({ navigation, route }: IDiscussionsViewProps): React.Re } }; - const onSearchChangeText = useDebounce(async (text: string) => { + const onSearchChangeText = useDebounce((text: string) => { setIsSearching(true); - await load(text); + setSearch([]); + searchText.current = text; + offset.current = 0; + load(); }, 500); const onCancelSearchPress = () => { setIsSearching(false); setSearch([]); - setSearchTotal(0); + searchText.current = ''; + offset.current = 0; }; const onSearchPress = () => { @@ -181,12 +186,12 @@ const DiscussionsView = ({ navigation, route }: IDiscussionsViewProps): React.Re item.msg} + keyExtractor={(item: any) => item._id} style={{ backgroundColor: colors.backgroundColor }} contentContainerStyle={styles.contentContainer} onEndReachedThreshold={0.5} removeClippedSubviews={isIOS} - onEndReached={() => (isSearching ? searchTotal : total) > API_FETCH_COUNT ?? load()} + onEndReached={() => isSearching && offset.current < total.current && load()} ItemSeparatorComponent={List.Separator} ListFooterComponent={loading ? : null} scrollIndicatorInsets={{ right: 1 }}