2023-06-30 14:01:01 +00:00
|
|
|
import React, { useLayoutEffect, useState } from 'react';
|
|
|
|
import { FlatList, StyleSheet, Text, TextInputProps } from 'react-native';
|
2020-04-06 21:40:18 +00:00
|
|
|
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../containers/List';
|
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2023-06-30 14:01:01 +00:00
|
|
|
import SearchBox from '../containers/SearchBox';
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import { useAppNavigation, useAppRoute } from '../lib/hooks/navigation';
|
|
|
|
import { useDebounce } from '../lib/methods/helpers';
|
|
|
|
import { TNavigation } from '../stacks/stackType';
|
|
|
|
import { useTheme } from '../theme';
|
2021-12-03 19:27:57 +00:00
|
|
|
import { IOptionsField } from './NotificationPreferencesView/options';
|
2023-06-30 14:01:01 +00:00
|
|
|
import sharedStyles from './Styles';
|
2020-04-06 21:40:18 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2020-05-08 17:36:10 +00:00
|
|
|
noResult: {
|
|
|
|
fontSize: 16,
|
|
|
|
paddingVertical: 56,
|
2020-11-30 21:47:05 +00:00
|
|
|
...sharedStyles.textSemibold,
|
|
|
|
...sharedStyles.textAlignCenter
|
2020-04-06 21:40:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-11-17 19:56:28 +00:00
|
|
|
interface IItem {
|
2021-12-03 19:27:57 +00:00
|
|
|
item: IOptionsField;
|
2021-11-17 19:56:28 +00:00
|
|
|
selected: boolean;
|
|
|
|
onItemPress: () => void;
|
|
|
|
}
|
|
|
|
|
2023-06-30 14:01:01 +00:00
|
|
|
const Item = ({ item, selected, onItemPress }: IItem) => {
|
|
|
|
const { colors } = useTheme();
|
2022-02-17 14:57:30 +00:00
|
|
|
return (
|
2023-06-30 14:01:01 +00:00
|
|
|
<List.Item
|
|
|
|
title={I18n.t(item.label, { defaultValue: item.label, second: item?.second })}
|
|
|
|
right={() => (selected ? <List.Icon name='check' color={colors.tintColor} /> : null)}
|
|
|
|
onPress={onItemPress}
|
|
|
|
translateTitle={false}
|
|
|
|
/>
|
2022-02-17 14:57:30 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-06-30 14:01:01 +00:00
|
|
|
const RenderSearch = ({ onChangeText }: TextInputProps) => (
|
|
|
|
<>
|
|
|
|
<SearchBox onChangeText={onChangeText} />
|
|
|
|
<List.Separator />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
const PickerView = (): React.ReactElement => {
|
|
|
|
const navigation = useAppNavigation();
|
|
|
|
const {
|
|
|
|
params: { title, data: paramData, value: paramValue, total: paramTotal, onSearch, onChangeValue, onEndReached }
|
|
|
|
} = useAppRoute<TNavigation, 'PickerView'>();
|
|
|
|
|
|
|
|
const { colors } = useTheme();
|
|
|
|
|
|
|
|
const [data, setData] = useState(paramData);
|
|
|
|
const [total, setTotal] = useState(paramTotal ?? 0);
|
|
|
|
const [searchText, setSearchText] = useState('');
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
navigation.setOptions({
|
|
|
|
title: title ?? I18n.t('Select_an_option')
|
|
|
|
});
|
|
|
|
}, [navigation, title]);
|
|
|
|
|
|
|
|
const handleChangeValue = (value: string | number) => {
|
|
|
|
onChangeValue(value);
|
|
|
|
navigation.goBack();
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2023-06-30 14:01:01 +00:00
|
|
|
const onChangeText = useDebounce(async (text: string) => {
|
|
|
|
const search = await onSearch(text);
|
|
|
|
if (search?.data) {
|
|
|
|
setSearchText(text);
|
|
|
|
setData(search?.data);
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
2022-02-17 14:57:30 +00:00
|
|
|
}, 500);
|
|
|
|
|
2023-06-30 14:01:01 +00:00
|
|
|
const handleOnEndReached = async () => {
|
|
|
|
if (onEndReached && total && data.length < total) {
|
|
|
|
const end = await onEndReached(searchText, data.length);
|
|
|
|
if (end?.data) {
|
|
|
|
setData([...data, ...end.data]);
|
|
|
|
setTotal(end.total);
|
2022-02-17 14:57:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-04-06 21:40:18 +00:00
|
|
|
|
2023-06-30 14:01:01 +00:00
|
|
|
return (
|
|
|
|
<SafeAreaView>
|
|
|
|
<FlatList
|
|
|
|
data={data}
|
|
|
|
keyExtractor={item => item.value as string}
|
|
|
|
renderItem={({ item }) => (
|
|
|
|
<Item
|
|
|
|
item={item}
|
|
|
|
selected={(paramValue || data[0]?.value) === item.value}
|
|
|
|
onItemPress={() => handleChangeValue(item.value)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
onEndReached={handleOnEndReached}
|
|
|
|
onEndReachedThreshold={0.5}
|
|
|
|
ItemSeparatorComponent={List.Separator}
|
|
|
|
ListHeaderComponent={<RenderSearch onChangeText={onChangeText} />}
|
|
|
|
ListFooterComponent={List.Separator}
|
|
|
|
ListEmptyComponent={() => (
|
|
|
|
<Text style={[styles.noResult, { color: colors.titleText }]}>{I18n.t('No_results_found')}</Text>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
};
|
2020-04-06 21:40:18 +00:00
|
|
|
|
2023-06-30 14:01:01 +00:00
|
|
|
export default PickerView;
|