2020-04-02 19:19:30 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import * as React from 'react';
|
2020-04-03 23:58:44 +02:00
|
|
|
import {FlatList, Platform, View} from "react-native";
|
|
|
|
import {Chip, Searchbar, withTheme} from 'react-native-paper';
|
2020-04-02 19:19:30 +02:00
|
|
|
import AuthenticatedScreen from "../../components/Amicale/AuthenticatedScreen";
|
2020-04-03 23:58:44 +02:00
|
|
|
import i18n from "i18n-js";
|
|
|
|
import ClubListItem from "../../components/Lists/ClubListItem";
|
2020-04-04 11:55:51 +02:00
|
|
|
import {isItemInCategoryFilter, stringMatchQuery} from "../../utils/Search";
|
2020-04-02 19:19:30 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
navigation: Object,
|
|
|
|
theme: Object,
|
|
|
|
}
|
|
|
|
|
2020-04-03 23:58:44 +02:00
|
|
|
type State = {
|
|
|
|
currentlySelectedCategories: Array<string>,
|
|
|
|
currentSearchString: string,
|
|
|
|
}
|
2020-04-02 19:19:30 +02:00
|
|
|
|
|
|
|
class ClubListScreen extends React.Component<Props, State> {
|
|
|
|
|
2020-04-03 23:58:44 +02:00
|
|
|
state = {
|
|
|
|
currentlySelectedCategories: [],
|
|
|
|
currentSearchString: '',
|
|
|
|
};
|
2020-04-02 19:19:30 +02:00
|
|
|
|
|
|
|
colors: Object;
|
|
|
|
|
|
|
|
getRenderItem: Function;
|
|
|
|
categories: Array<Object>;
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.colors = props.theme.colors;
|
|
|
|
}
|
|
|
|
|
2020-04-03 23:58:44 +02:00
|
|
|
/**
|
|
|
|
* Creates the header content
|
|
|
|
*/
|
|
|
|
componentDidMount() {
|
|
|
|
const title = this.getSearchBar.bind(this);
|
|
|
|
this.props.navigation.setOptions({
|
|
|
|
headerTitle: title,
|
|
|
|
headerBackTitleVisible: false,
|
|
|
|
headerTitleContainerStyle: Platform.OS === 'ios' ?
|
|
|
|
{marginHorizontal: 0, width: '70%'} :
|
|
|
|
{marginHorizontal: 0, right: 50, left: 50},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the header search bar
|
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
getSearchBar() {
|
|
|
|
return (
|
|
|
|
<Searchbar
|
|
|
|
placeholder={i18n.t('proximoScreen.search')}
|
|
|
|
onChangeText={this.onSearchStringChange}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback used when the search changes
|
|
|
|
*
|
|
|
|
* @param str The new search string
|
|
|
|
*/
|
|
|
|
onSearchStringChange = (str: string) => {
|
2020-04-04 11:55:51 +02:00
|
|
|
this.updateFilteredData(str, null);
|
2020-04-03 23:58:44 +02:00
|
|
|
};
|
|
|
|
|
2020-04-02 19:19:30 +02:00
|
|
|
keyExtractor = (item: Object) => {
|
2020-04-04 11:55:51 +02:00
|
|
|
return item.id.toString();
|
2020-04-02 19:19:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
getScreen = (data: Object) => {
|
|
|
|
this.categories = data.categories;
|
|
|
|
return (
|
2020-04-04 11:55:51 +02:00
|
|
|
//$FlowFixMe
|
2020-04-03 23:58:44 +02:00
|
|
|
<FlatList
|
2020-04-02 19:19:30 +02:00
|
|
|
data={data.clubs}
|
|
|
|
keyExtractor={this.keyExtractor}
|
|
|
|
renderItem={this.getRenderItem}
|
2020-04-03 23:58:44 +02:00
|
|
|
ListHeaderComponent={this.getListHeader()}
|
2020-04-02 19:19:30 +02:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2020-04-03 23:58:44 +02:00
|
|
|
onChipSelect(id: string) {
|
|
|
|
this.updateFilteredData(null, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFilteredData(filterStr: string | null, categoryId: string | null) {
|
|
|
|
let newCategoriesState = [...this.state.currentlySelectedCategories];
|
|
|
|
let newStrState = this.state.currentSearchString;
|
|
|
|
if (filterStr !== null)
|
|
|
|
newStrState = filterStr;
|
|
|
|
if (categoryId !== null) {
|
|
|
|
let index = newCategoriesState.indexOf(categoryId);
|
|
|
|
if (index === -1)
|
|
|
|
newCategoriesState.push(categoryId);
|
|
|
|
else
|
2020-04-04 11:55:51 +02:00
|
|
|
newCategoriesState.splice(index,1);
|
2020-04-02 19:19:30 +02:00
|
|
|
}
|
2020-04-03 23:58:44 +02:00
|
|
|
if (filterStr !== null || categoryId !== null)
|
|
|
|
this.setState({
|
|
|
|
currentSearchString: newStrState,
|
|
|
|
currentlySelectedCategories: newCategoriesState,
|
|
|
|
})
|
2020-04-02 19:19:30 +02:00
|
|
|
}
|
|
|
|
|
2020-04-04 11:55:51 +02:00
|
|
|
getChipRender = (category: Object, key: string) => {
|
2020-04-03 23:58:44 +02:00
|
|
|
const onPress = this.onChipSelect.bind(this, category.id);
|
|
|
|
return <Chip
|
2020-04-04 11:55:51 +02:00
|
|
|
selected={isItemInCategoryFilter(this.state.currentlySelectedCategories, [category.id])}
|
2020-04-03 23:58:44 +02:00
|
|
|
mode={'outlined'}
|
|
|
|
onPress={onPress}
|
|
|
|
style={{marginRight: 5, marginBottom: 5}}
|
2020-04-04 11:55:51 +02:00
|
|
|
key={key}
|
2020-04-03 23:58:44 +02:00
|
|
|
>
|
|
|
|
{category.name}
|
|
|
|
</Chip>;
|
|
|
|
};
|
|
|
|
|
|
|
|
getListHeader() {
|
2020-04-02 19:19:30 +02:00
|
|
|
let final = [];
|
2020-04-03 23:58:44 +02:00
|
|
|
for (let i = 0; i < this.categories.length; i++) {
|
2020-04-04 11:55:51 +02:00
|
|
|
final.push(this.getChipRender(this.categories[i], this.categories[i].id));
|
2020-04-02 19:19:30 +02:00
|
|
|
}
|
2020-04-03 23:58:44 +02:00
|
|
|
return <View style={{
|
|
|
|
justifyContent: 'space-around',
|
|
|
|
flexDirection: 'row',
|
|
|
|
flexWrap: 'wrap',
|
|
|
|
margin: 10,
|
|
|
|
}}>{final}</View>;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCategoryOfId = (id: number) => {
|
|
|
|
for (let i = 0; i < this.categories.length; i++) {
|
|
|
|
if (id === this.categories[i].id)
|
|
|
|
return this.categories[i];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
shouldRenderItem(item) {
|
|
|
|
let shouldRender = this.state.currentlySelectedCategories.length === 0
|
2020-04-04 11:55:51 +02:00
|
|
|
|| isItemInCategoryFilter(this.state.currentlySelectedCategories, item.category);
|
2020-04-03 23:58:44 +02:00
|
|
|
if (shouldRender)
|
2020-04-04 11:55:51 +02:00
|
|
|
shouldRender = stringMatchQuery(item.name, this.state.currentSearchString);
|
2020-04-03 23:58:44 +02:00
|
|
|
return shouldRender;
|
2020-04-02 19:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getRenderItem = ({item}: Object) => {
|
|
|
|
const onPress = this.onListItemPress.bind(this, item);
|
2020-04-03 23:58:44 +02:00
|
|
|
if (this.shouldRenderItem(item)) {
|
|
|
|
return (
|
|
|
|
<ClubListItem
|
|
|
|
categoryTranslator={this.getCategoryOfId}
|
|
|
|
chipRender={this.getChipRender}
|
|
|
|
item={item}
|
|
|
|
onPress={onPress}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else
|
|
|
|
return null;
|
2020-04-02 19:19:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback used when clicking an article in the list.
|
|
|
|
* It opens the modal to show detailed information about the article
|
|
|
|
*
|
|
|
|
* @param item The article pressed
|
|
|
|
*/
|
|
|
|
onListItemPress(item: Object) {
|
2020-04-02 19:55:50 +02:00
|
|
|
this.props.navigation.navigate("ClubDisplayScreen", {data: item, categories: this.categories});
|
2020-04-02 19:19:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<AuthenticatedScreen
|
|
|
|
{...this.props}
|
|
|
|
link={'https://www.amicale-insat.fr/api/clubs/list'}
|
|
|
|
renderFunction={this.getScreen}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withTheme(ClubListScreen);
|