Fix search
This commit is contained in:
parent
daba3b44e9
commit
da3bdd4272
|
@ -22,6 +22,7 @@ import { getHeaderTitlePosition } from '../containers/Header';
|
||||||
import SearchHeader from './ThreadMessagesView/SearchHeader';
|
import SearchHeader from './ThreadMessagesView/SearchHeader';
|
||||||
import { useTheme } from '../theme';
|
import { useTheme } from '../theme';
|
||||||
import Message from '../containers/message';
|
import Message from '../containers/message';
|
||||||
|
import { sanitizeLikeString } from '../lib/database/utils';
|
||||||
|
|
||||||
const DiscussionMessagesView = ({ navigation, route }) => {
|
const DiscussionMessagesView = ({ navigation, route }) => {
|
||||||
const rid = route.params?.rid;
|
const rid = route.params?.rid;
|
||||||
|
@ -38,7 +39,6 @@ const DiscussionMessagesView = ({ navigation, route }) => {
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [discussions, setDiscussions] = useState([]);
|
const [discussions, setDiscussions] = useState([]);
|
||||||
const [isSearching, setIsSearching] = useState(false);
|
const [isSearching, setIsSearching] = useState(false);
|
||||||
const [searchText, setSearchText] = useState('');
|
|
||||||
const { theme } = useTheme();
|
const { theme } = useTheme();
|
||||||
const insets = useSafeAreaInsets();
|
const insets = useSafeAreaInsets();
|
||||||
|
|
||||||
|
@ -63,13 +63,30 @@ const DiscussionMessagesView = ({ navigation, route }) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const onSearchChangeText = debounce((text) => {
|
const onSearchChangeText = debounce(async(text) => {
|
||||||
setSearchText(text);
|
try {
|
||||||
|
const db = database.active;
|
||||||
|
const whereClause = [
|
||||||
|
Q.where('rid', Q.eq(rid)),
|
||||||
|
Q.where('drid', Q.notEq(null))
|
||||||
|
];
|
||||||
|
|
||||||
|
if (text?.trim()) {
|
||||||
|
whereClause.push(Q.where('msg', Q.like(`%${ sanitizeLikeString(text?.trim()) }%`)));
|
||||||
|
}
|
||||||
|
|
||||||
|
const discussionsMessages = await db
|
||||||
|
.get('messages')
|
||||||
|
.query(...whereClause);
|
||||||
|
setDiscussions(discussionsMessages);
|
||||||
|
} catch (e) {
|
||||||
|
log(e);
|
||||||
|
}
|
||||||
}, 300);
|
}, 300);
|
||||||
|
|
||||||
const onCancelSearchPress = () => {
|
const onCancelSearchPress = () => {
|
||||||
setIsSearching(false);
|
setIsSearching(false);
|
||||||
setSearchText('');
|
load();
|
||||||
};
|
};
|
||||||
|
|
||||||
const onSearchPress = () => {
|
const onSearchPress = () => {
|
||||||
|
@ -128,9 +145,12 @@ const DiscussionMessagesView = ({ navigation, route }) => {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
load();
|
load();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
const options = setHeader();
|
const options = setHeader();
|
||||||
navigation.setOptions(options);
|
navigation.setOptions(options);
|
||||||
}, []);
|
}, [navigation, isSearching]);
|
||||||
|
|
||||||
|
|
||||||
const onDiscussionPress = debounce((item) => {
|
const onDiscussionPress = debounce((item) => {
|
||||||
|
@ -169,6 +189,8 @@ const DiscussionMessagesView = ({ navigation, route }) => {
|
||||||
<FlatList
|
<FlatList
|
||||||
data={discussions}
|
data={discussions}
|
||||||
renderItem={renderItem}
|
renderItem={renderItem}
|
||||||
|
// eslint-disable-next-line react/prop-types
|
||||||
|
keyExtractor={item => item.msg}
|
||||||
// style={[styles.list, { backgroundColor: themes[theme].backgroundColor }]}
|
// style={[styles.list, { backgroundColor: themes[theme].backgroundColor }]}
|
||||||
// contentContainerStyle={styles.contentContainer}
|
// contentContainerStyle={styles.contentContainer}
|
||||||
onEndReachedThreshold={0.5}
|
onEndReachedThreshold={0.5}
|
||||||
|
|
Loading…
Reference in New Issue