2021-06-15 20:13:42 +00:00
|
|
|
import React from 'react';
|
2021-12-24 13:12:49 +00:00
|
|
|
import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
|
2021-10-01 18:12:19 +00:00
|
|
|
import { FlatList, StyleSheet, Text, View } from 'react-native';
|
2021-06-15 20:13:42 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { RadioButton } from 'react-native-ui-lib';
|
2021-12-24 13:12:49 +00:00
|
|
|
import { RouteProp } from '@react-navigation/native';
|
2021-06-15 20:13:42 +00:00
|
|
|
|
2022-01-24 20:12:25 +00:00
|
|
|
import { ChatsStackParamList } from '../stacks/types';
|
2021-06-15 20:13:42 +00:00
|
|
|
import log from '../utils/log';
|
|
|
|
import * as List from '../containers/List';
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
|
|
|
import { themes } from '../constants/colors';
|
|
|
|
import { withTheme } from '../theme';
|
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
|
|
|
import { animateNextTransition } from '../utils/layoutAnimation';
|
|
|
|
import { ICON_SIZE } from '../containers/List/constants';
|
|
|
|
import SearchBox from '../containers/SearchBox';
|
2021-10-01 18:12:19 +00:00
|
|
|
import sharedStyles from './Styles';
|
2022-01-24 20:12:25 +00:00
|
|
|
import { IRoom } from '../definitions/IRoom';
|
2021-06-15 20:13:42 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
buttonText: {
|
|
|
|
fontSize: 16,
|
|
|
|
margin: 16,
|
|
|
|
...sharedStyles.textRegular
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
interface ISelectListViewState {
|
2022-03-29 18:53:27 +00:00
|
|
|
data?: IRoom[];
|
2022-01-24 20:12:25 +00:00
|
|
|
dataFiltered?: IRoom[];
|
2021-12-24 13:12:49 +00:00
|
|
|
isSearching: boolean;
|
|
|
|
selected: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ISelectListViewProps {
|
2022-01-24 20:12:25 +00:00
|
|
|
navigation: StackNavigationProp<ChatsStackParamList, 'SelectListView'>;
|
|
|
|
route: RouteProp<ChatsStackParamList, 'SelectListView'>;
|
2021-12-24 13:12:49 +00:00
|
|
|
theme: string;
|
|
|
|
isMasterDetail: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
class SelectListView extends React.Component<ISelectListViewProps, ISelectListViewState> {
|
|
|
|
private title: string;
|
|
|
|
|
|
|
|
private infoText: string;
|
|
|
|
|
|
|
|
private nextAction: (selected: string[]) => void;
|
|
|
|
|
|
|
|
private showAlert: () => void;
|
|
|
|
|
|
|
|
private isSearch: boolean;
|
|
|
|
|
2022-03-29 18:53:27 +00:00
|
|
|
private onSearch?: (text: string) => Promise<Partial<IRoom[]> | any>;
|
2021-12-24 13:12:49 +00:00
|
|
|
|
2022-01-24 20:12:25 +00:00
|
|
|
private isRadio?: boolean;
|
2021-06-15 20:13:42 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
constructor(props: ISelectListViewProps) {
|
2021-06-15 20:13:42 +00:00
|
|
|
super(props);
|
|
|
|
const data = props.route?.params?.data;
|
|
|
|
this.title = props.route?.params?.title;
|
2022-03-29 18:53:27 +00:00
|
|
|
this.infoText = props.route?.params?.infoText ?? '';
|
2021-06-15 20:13:42 +00:00
|
|
|
this.nextAction = props.route?.params?.nextAction;
|
2022-03-29 18:53:27 +00:00
|
|
|
this.showAlert = props.route?.params?.showAlert ?? (() => {});
|
|
|
|
this.isSearch = props.route?.params?.isSearch ?? false;
|
2021-06-15 20:13:42 +00:00
|
|
|
this.onSearch = props.route?.params?.onSearch;
|
|
|
|
this.isRadio = props.route?.params?.isRadio;
|
|
|
|
this.state = {
|
|
|
|
data,
|
|
|
|
dataFiltered: [],
|
|
|
|
isSearching: false,
|
|
|
|
selected: []
|
|
|
|
};
|
|
|
|
this.setHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
setHeader = () => {
|
|
|
|
const { navigation, isMasterDetail } = this.props;
|
|
|
|
const { selected } = this.state;
|
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
const options: StackNavigationOptions = {
|
2021-06-15 20:13:42 +00:00
|
|
|
headerTitle: I18n.t(this.title)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isMasterDetail) {
|
|
|
|
options.headerLeft = () => <HeaderButton.CloseModal navigation={navigation} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.headerRight = () => (
|
|
|
|
<HeaderButton.Container>
|
|
|
|
<HeaderButton.Item title={I18n.t('Next')} onPress={() => this.nextAction(selected)} testID='select-list-view-submit' />
|
|
|
|
</HeaderButton.Container>
|
|
|
|
);
|
|
|
|
|
|
|
|
navigation.setOptions(options);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2021-06-15 20:13:42 +00:00
|
|
|
|
|
|
|
renderInfoText = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<View style={{ backgroundColor: themes[theme].backgroundColor }}>
|
|
|
|
<Text style={[styles.buttonText, { color: themes[theme].bodyText }]}>{I18n.t(this.infoText)}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2021-06-15 20:13:42 +00:00
|
|
|
|
|
|
|
renderSearch = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<View style={{ backgroundColor: themes[theme].auxiliaryBackground }}>
|
2021-10-01 18:12:19 +00:00
|
|
|
<SearchBox
|
2021-12-24 13:12:49 +00:00
|
|
|
onChangeText={(text: string) => this.search(text)}
|
2021-10-01 18:12:19 +00:00
|
|
|
testID='select-list-view-search'
|
|
|
|
onCancelPress={() => this.setState({ isSearching: false })}
|
|
|
|
/>
|
2021-06-15 20:13:42 +00:00
|
|
|
</View>
|
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2021-06-15 20:13:42 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
search = async (text: string) => {
|
2021-06-15 20:13:42 +00:00
|
|
|
try {
|
|
|
|
this.setState({ isSearching: true });
|
2022-03-29 18:53:27 +00:00
|
|
|
const result = (await this.onSearch?.(text)) as IRoom[];
|
2021-06-15 20:13:42 +00:00
|
|
|
this.setState({ dataFiltered: result });
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2021-06-15 20:13:42 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
isChecked = (rid: string) => {
|
2021-06-15 20:13:42 +00:00
|
|
|
const { selected } = this.state;
|
|
|
|
return selected.includes(rid);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2021-06-15 20:13:42 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
toggleItem = (rid: string) => {
|
2021-06-15 20:13:42 +00:00
|
|
|
const { selected } = this.state;
|
|
|
|
|
|
|
|
animateNextTransition();
|
|
|
|
if (this.isRadio) {
|
|
|
|
if (!this.isChecked(rid)) {
|
|
|
|
this.setState({ selected: [rid] }, () => this.setHeader());
|
|
|
|
}
|
|
|
|
} else if (!this.isChecked(rid)) {
|
|
|
|
this.setState({ selected: [...selected, rid] }, () => this.setHeader());
|
|
|
|
} else {
|
|
|
|
const filterSelected = selected.filter(el => el !== rid);
|
|
|
|
this.setState({ selected: filterSelected }, () => this.setHeader());
|
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2021-06-15 20:13:42 +00:00
|
|
|
|
2022-01-24 20:12:25 +00:00
|
|
|
renderItem = ({ item }: { item: Partial<IRoom> }) => {
|
2021-06-15 20:13:42 +00:00
|
|
|
const { theme } = this.props;
|
|
|
|
const { selected } = this.state;
|
|
|
|
|
|
|
|
const channelIcon = item.t === 'p' ? 'channel-private' : 'channel-public';
|
|
|
|
const teamIcon = item.t === 'p' ? 'teams-private' : 'teams';
|
|
|
|
const icon = item.teamMain ? teamIcon : channelIcon;
|
2022-01-24 20:12:25 +00:00
|
|
|
const checked = this.isChecked(item.rid!) ? 'check' : '';
|
2021-06-15 20:13:42 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
const showRadio = () => (
|
|
|
|
<RadioButton
|
|
|
|
testID={selected ? `radio-button-selected-${item.name}` : `radio-button-unselected-${item.name}`}
|
2022-01-24 20:12:25 +00:00
|
|
|
selected={selected.includes(item.rid!)}
|
2021-10-01 18:12:19 +00:00
|
|
|
color={themes[theme].actionTintColor}
|
|
|
|
size={ICON_SIZE}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
const showCheck = () => (
|
|
|
|
<List.Icon
|
|
|
|
testID={checked ? `${item.name}-checked` : `${item.name}-unchecked`}
|
|
|
|
name={checked}
|
|
|
|
color={themes[theme].actionTintColor}
|
|
|
|
/>
|
|
|
|
);
|
2021-06-15 20:13:42 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title={item.name}
|
|
|
|
translateTitle={false}
|
2021-10-01 18:12:19 +00:00
|
|
|
testID={`select-list-view-item-${item.name}`}
|
2022-01-24 20:12:25 +00:00
|
|
|
onPress={() => (item.alert ? this.showAlert() : this.toggleItem(item.rid!))}
|
2021-06-15 20:13:42 +00:00
|
|
|
alert={item.alert}
|
|
|
|
left={() => <List.Icon name={icon} color={themes[theme].controlText} />}
|
|
|
|
right={() => (this.isRadio ? showRadio() : showCheck())}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2021-06-15 20:13:42 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { data, isSearching, dataFiltered } = this.state;
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<SafeAreaView testID='select-list-view'>
|
|
|
|
<StatusBar />
|
|
|
|
<FlatList
|
|
|
|
data={!isSearching ? data : dataFiltered}
|
|
|
|
extraData={this.state}
|
|
|
|
keyExtractor={item => item.rid}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
ListHeaderComponent={this.isSearch ? this.renderSearch : this.renderInfoText}
|
|
|
|
contentContainerStyle={{ backgroundColor: themes[theme].backgroundColor }}
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
const mapStateToProps = (state: any) => ({
|
2021-06-15 20:13:42 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(withTheme(SelectListView));
|