i need to make a search. Here a need to enter name of what i need to find from PastMK…
- User
i need to make a search. Here a need to enter name of what i need to find from PastMK collection and FutureMK collection (first would be FutureMK, second PastMK) and the result has to be shown on Search screen. import { Pressable, Text, TextInput, View, Image, TouchableOpacity, Modal } from 'react-native'; import { gStyle } from '../styles/style'; import { Feather } from '@expo/vector-icons'; import React, { useState, useEffect } from 'react'; import { useNavigation } from '@react-navigation/native'; import { firebase } from '../Firebase/firebase'; import 'firebase/compat/auth'; import 'firebase/compat/database'; import 'firebase/compat/firestore';
export default function Header() { const [searchTerm, setSearchTerm] = useState('');
const [searchResult, setSearchResult]= useState(''); const handleSearch = async ()=>{ const futureMKSnapshot = await firebase.firestore().collection('FutureMK').where('name','>=',searchTerm).where('name','<=',searchTerm + '\uf8ff').get(); const pastMKSnapshot = await firebase.firestore().collection('PastMK').where('name','>=',searchTerm).where('name','<=',searchTerm + '\uf8ff').get(); const futureMKData = futureMKSnapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })); const pastMKData = pastMKSnapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })); const results = [...futureMKData, ...pastMKData]; setSearchResult(results); navigation.navigate('Search', { results }); } const [userAuthenticated, setUserAuthenticated] = useState(false); const [isModalVisible, setIsModalVisible] = useState(false); const showModal = () => { setIsModalVisible(true); }; const hideModal = () => { setIsModalVisible(false); }; const navigation = useNavigation(); useEffect(() => { const unsubscribe = firebase.auth().onAuthStateChanged(user => { if (user) { setUserAuthenticated(true); } else { setUserAuthenticated(false); } }); return unsubscribe; }, []); return ( <View style={gStyle.main}> <View style={gStyle.headerContainer}> <Image source={require('../assets/logo.png')} style={gStyle.logoImg} /> <View style={gStyle.menu}> <TouchableOpacity onPress={showModal}> <Text style={gStyle.menuRedLine}></Text> <Text style={gStyle.menuRedLine}></Text> <Text style={gStyle.menuRedLine}></Text> <Modal visible={isModalVisible} animationType='fade' transparent={true}> <View style={gStyle.modal}> <TouchableOpacity onPress={hideModal}> <Text style={gStyle.close}>Х</Text> </TouchableOpacity> <TouchableOpacity onPress={()=>navigation.navigate('Home')} > <Text style={gStyle.menuItem}>Главная</Text> </TouchableOpacity> <TouchableOpacity onPress={()=>navigation.navigate('AllFutureMK')} > <Text style={gStyle.menuItem}>Будущие мастер-классы</Text> </TouchableOpacity> <TouchableOpacity onPress={()=>navigation.navigate('AllPastMK')} > <Text style={gStyle.menuItem}>Архив мастер-классов</Text> </TouchableOpacity> <TouchableOpacity onPress={()=>navigation.navigate('Sales')} > <Text style={gStyle.menuItem}>Скидки</Text> </TouchableOpacity> {userAuthenticated ? <TouchableOpacity onPress={()=>navigation.navigate('Profile')} > <Text style={gStyle.menuItem}>Личный кабинет</Text> </TouchableOpacity> : <TouchableOpacity onPress={()=>navigation.navigate('Auth')} > <Text style={gStyle.menuItem}>Личный кабинет</Text> </TouchableOpacity> } </View> </Modal> </TouchableOpacity> </View> </View> <View style={gStyle.searchBox}> <TextInput style={gStyle.search} placeholder="Поиск" /> <View style={gStyle.boxBtn}> <Pressable style={gStyle.searchBtn} onPress={handleSearch} > <Feather name="search" size={25} color="white" /> </Pressable> </View> </View> </View> ); - Assistant
In order to display the search results in the Search screen, you'll need to modify the Search component to accept a route parameter. Here is the updated version of your search component:
import React from 'react'; import { Text, View } from 'react-native'; import { gStyle } from '../styles/style'; import Header from '../components/Header'; import Footer from '../components/Footer'; import { useNavigation, useRoute } from '@react-navigation/native'; export default function Search() { const navigation = useNavigation(); const route = useRoute(); const results = route.params?.results || []; return ( <View> <Header /> <View style={gStyle.container}> {results.length > 0 ? ( results.map((item, index) => ( <View key={index}> <Text>{item.name}</Text> </View> )) ) : ( <Text>No results found.</Text> )} </View> <Footer /> </View> ); } - User
it shows all posts not what i want to find
- Assistant
- User
i need to show in results name, image and description (100symbols). how can i do it?
- Assistant
To display the name, image, and a truncated description in the search results, you can modify the rendering part of the Search component like this:
import React from 'react'; import { Text, View, ScrollView, Image } from 'react-native'; import { gStyle } from '../styles/style'; import Header from '../components/Header'; import Footer from '../components/Footer'; import { useNavigation, useRoute } from '@react-navigation/native'; export default function Search() { const navigation = useNavigation(); const route = useRoute(); const results = route.params?.results || []; return ( <View> <Header /> <ScrollView style={gStyle.container}> {results.length > 0 ? ( results.map((item, index) => ( <View key={index} style={gStyle.resultItem}> <Image source={{ uri: item.image }} style={gStyle.resultItemImage} /> <View style={gStyle.resultItemDetails}> <Text style={gStyle.resultItemName}>{item.name}</Text> <Text style={gStyle.resultItemDescription}> {item.description.length > 100 ? item.description.substring(0, 100) + '...' : item.description} </Text> </View> </View> )) ) : ( <Text>No results found.</Text> )} </ScrollView> <Footer /> </View> ); } - User
<View style={[gStyle.bookedMK, {flex:1}]}> <View style={{ flexDirection: 'column' }}> {mkData.name} {formattedTime} Цена: {mkData.price} Р. Посмотреть can u write me code so i just add this component?
- Assistant
To create a reusable ResultItem component, you can create a new file named
ResultItem.jsand use the following code:ResultItem.js:
import React from 'react'; import { View, Image, Text, Pressable } from 'react-native'; import { gStyle } from '../styles/style'; const ResultItem = ({ item }) => { const formattedTime = 'YOUR_FORMATTED_TIME'; // Replace with your formatted time logic return ( <View style={[gStyle.bookedMK, { flex: 1 }]}> <View style={{ flexDirection: 'column' }}> <View style={gStyle.ProfileDetails}> <Image source={{ uri: item.image }} style={gStyle.ProfileImg} /> <View style={gStyle.ProfileDescription}> <Text style={gStyle.ProfileTitleOfMK}>{item.name}</Text> <Text style={gStyle.ProfileDate}>{formattedTime}</Text> <Text style={gStyle.ProfilePrice}>Цена: {item.price} Р.</Text> </View> </View> </View> <Pressable> <Text>Посмотреть</Text> </Pressable> <Text style={gStyle.ProfileLine}></Text> </View> ); }; export default ResultItem;
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.