import React, { useCallback } from 'react' import { StyleSheet, View, TouchableOpacity } from 'react-native' import { useTheme } from 'styled-components/native'; import { OIcon, OText } from '../shared'; import { NotFoundSource } from '../NotFoundSource'; import ToggleSwitch from 'toggle-switch-react-native'; import { Placeholder, PlaceholderLine, Fade } from 'rn-placeholder'; import { SearchBar } from '../SearchBar'; import { IOScrollView } from 'react-native-intersection-observer'; import { useLanguage, useUtils, ToastType, useToast, } from 'ordering-components/native' const PIXELS_TO_SCROLL = 2000 export const ProductList = (props: any) => { const { productsList, onClose, updateProduct, searchValue, handleChangeSearch, getCategoryProducts } = props const { loading, products, error } = productsList const theme = useTheme() const [{ optimizeImage }] = useUtils(); const [, { showToast }] = useToast() const [, t] = useLanguage() const styles = StyleSheet.create({ container: { flex: 1, marginBottom: 0 }, header: { flexDirection: 'row', justifyContent: 'space-between', marginBottom: 10, }, btnBackArrow: { borderWidth: 0, width: 32, height: 32, tintColor: theme.colors.textGray, backgroundColor: theme.colors.clear, borderColor: theme.colors.clear, shadowColor: theme.colors.clear, paddingLeft: 0, paddingRight: 0 }, sectionTitle: { fontStyle: 'normal', fontWeight: '600', fontSize: 20, color: theme.colors.textGray, }, logo: { padding: 2, borderRadius: 18, shadowColor: '#000', shadowOffset: { width: 0, height: 1.5, }, shadowOpacity: 0.21, shadowRadius: 3, elevation: 7, }, icon: { borderRadius: 7.6, width: 35, height: 35, marginRight: 5 }, borderStyle: { borderColor: theme.colors.red, borderWidth: 0, borderRadius: 10, }, }); const handleSwitch = (enabled: boolean, categoryId: any, productId: any) => { updateProduct && updateProduct(categoryId, productId, { enabled }) }; const handleScroll = ({ nativeEvent }: any) => { const y = nativeEvent.contentOffset.y; const height = nativeEvent.contentSize.height; const hasMore = !( productsList.pagination.totalPages === productsList.pagination.currentPage ); if (y + PIXELS_TO_SCROLL > height && !productsList.loading && hasMore && productsList?.products?.length > 0) { getCategoryProducts(false) showToast(ToastType.Info, t('LOADING_MORE_PRODUCTS', 'Loading more products')) } }; return ( {t('PRODUCTS', 'Products')} handleChangeSearch('')} placeholder={t('SEARCH', 'Search')} containerStyle={{ width: 180 }} /> {!loading && products?.length === 0 && ( )} {!loading && products?.length > 0 && ( {products.map((product: any, i: number) => ( {product?.name} handleSwitch(value, product?.category_id, product.id)} disabled={loading} animationSpeed={200} /> ))} )} {loading && ( {[...Array(6)].map((item, i) => ( ))} )} ) }