other means
This commit is contained in:
@@ -9,17 +9,18 @@ import barcodeApi from "../../api/barcodeApi";
|
||||
import {useDispatch, useSelector} from "react-redux";
|
||||
import {openScanModal, setScannedData} from "../../features/scanModal/scanModalSlice";
|
||||
import {RootState} from "../../redux/store";
|
||||
import {Product} from "../../types/product";
|
||||
|
||||
type Props = {
|
||||
onSearch?: (text: string) => void;
|
||||
onSupplierProductSelected?: (supplierProduct: SupplierProduct) => void
|
||||
onProductSelected?: (product: Product) => void
|
||||
}
|
||||
const SearchBar: FC<Props> = ({onSearch, onSupplierProductSelected}) => {
|
||||
const SearchBar: FC<Props> = ({onSearch, onProductSelected}) => {
|
||||
|
||||
const dispatch = useDispatch();
|
||||
const [searchInput, setSearchInput] = useState<string>("");
|
||||
const textInputRef = useRef<TextInput>(null);
|
||||
const [products, setProducts] = useState<SupplierProduct[]>([]);
|
||||
const [products, setProducts] = useState<Product[]>([]);
|
||||
|
||||
const scannedData = useSelector((state: RootState) => state.scanModal.scannedData);
|
||||
useEffect(() => {
|
||||
@@ -28,14 +29,22 @@ const SearchBar: FC<Props> = ({onSearch, onSupplierProductSelected}) => {
|
||||
setProducts(response)
|
||||
});
|
||||
}, [scannedData]);
|
||||
const selectProductModalVisible = products.length > 0;
|
||||
useEffect(() => {
|
||||
if (products.length == 0 || products.length > 1 || !onProductSelected) return;
|
||||
onProductSelected(products[0]);
|
||||
setProducts([]);
|
||||
dispatch(setScannedData(undefined));
|
||||
}, [products]);
|
||||
const selectProductModalVisible = products.length > 1;
|
||||
return (
|
||||
<View style={styles.container}>
|
||||
<SelectProductModal visible={selectProductModalVisible} products={products} onSelected={(product) => {
|
||||
if (onSupplierProductSelected) onSupplierProductSelected(product);
|
||||
setProducts([]);
|
||||
dispatch(setScannedData(undefined));
|
||||
}}/>
|
||||
<SelectProductModal visible={selectProductModalVisible}
|
||||
products={products}
|
||||
onSelected={(product) => {
|
||||
if (onProductSelected) onProductSelected(product);
|
||||
setProducts([]);
|
||||
dispatch(setScannedData(undefined));
|
||||
}}/>
|
||||
<BasicButton onPress={() => {
|
||||
if (!onSearch) return;
|
||||
onSearch(searchInput);
|
||||
|
||||
Reference in New Issue
Block a user