SearchBar and Read/Unread functionality not working together – React Native
I have a simple FlatList displaying 2 items in HomeComponent. The items are being displayed in the Item Component. I have added a Read/Unread functionality with a Badge using two states -> notificationRead and selectedNotificationList[]. My logic is working here. When i click into an item the Badge disappears.
I also have a SearchBar which searches the Description. The logic of Search works when both the items are Unread. But when one of the items or both the items are Read, then i Search any text the Badge re-appears.
My code is below:
Home:
import Item from './Item';
export default class HomeComponent extends Component {
constructor(props){
super(props)
this.state = {
isLoading: true,
notifications: [],
query: '',
temp:[],
notificationRead: false
}
};
componentDidMount() {
fetch('https://140xya6a67.execute-api.ap-southeast-1.amazonaws.com/dev/', {
method: 'GET',
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
notifications: responseJson,
notificationRead: false,
temp: responseJson,
})
})
};
goToDetails() {
return this.props.navigation.navigate(Details) ;
}
setNotifRead(bool){
this.setState({ notificationRead: bool });
console.log('Read status is set to:', bool);
}
setReadCount(Id) {
this.setState({ list: [...this.state.list, Id] });
console.log('Read count is:', Id);
}
renderItem({item}) {
return <Item item={item}
onPress = {()=> {this.goToDetails();this.setNotifRead(true)}}
notificationRead={this.state.notificationRead}
readNotifCountFn={(Id)=>{this.setReadCount(Id)}}
/>
}
handleSearch(text){
const newData = _.filter(this.state.temp, (item) => {
const itemData = item.Desc ? item.Desc.toUpperCase() : ''.toUpperCase();
const textData = text.toUpperCase();
return itemData.indexOf(textData) > -1;
});
this.setState({
notifications: newData,
query:text,
});
}
renderContent() {
let {notifications} = this.state;
return (
<View>
<SearchBar placeholder='type here...' lightTheme round value={this.state.query} onChangeText={(text)=>{this.handleSearch(text)}} />
<FlatList
keyExtractor={(item, id) => item.id}
data={notifications}
renderItem={this.renderItem.bind(this)}
/>
</View>
);
}
render () {
let {fill, container} = styles;
return (
<View style={ [fill, container] }>
{this.renderContent()}
</View>
);
}
}
Item:
export default class Item extends Component {
constructor(props){
super(props)
this.state = {
selectedNotificationList: []
}
};
handlePress () {
return this.props.onPress();
}
setList(Id) {
if(!this.state.selectedNotificationList.includes(Id)) {
this.setState({
selectedNotificationList: [...this.state.selectedNotificationList, Id]
});
console.log('List is set:',Id);
return this.props.readNotifCountFn(Id);
}
}
render() {
const {row, badge, textContainer, id, ordername, desc, seperator} = styles;
const { item, notificationRead } = this.props;
const { selectedNotificationList } = this.state;
const { Id, OrderName, Desc } = item;
return (
<View>
<TouchableOpacity onPress={()=>{this.handlePress(); this.setList(Id)}} >
<View style={seperator}></View>
<View style={row}>
{
notificationRead === false || selectedNotificationList.includes(Id) === false ?
<View style={badge}>
<Badge status="error" containerStyle={{ padding:8, position:'absolute'}} />
</View> : null
}
<View style={textContainer}>
<Text style={id}> {Id} </Text>
<Text style={ordername}> {OrderName} </Text>
<Text style={desc}> {Desc} </Text>
</View>
<View style={seperator}></View>
</View>
</TouchableOpacity>
</View>
)
}
}
Please help me fix my code or let me know a better way to do this.