summaryrefslogtreecommitdiffstats
path: root/src/app/GuardarProducto.tsx
blob: abdd8c1b237aaa43b6b76413506ac83d4704a822 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { StyleSheet, Text, View, Button, Alert} from 'react-native'
import React from 'react'
import { useSQLiteContext } from 'expo-sqlite';
import { drizzle, useLiveQuery } from 'drizzle-orm/expo-sqlite';
//import { personas, categorias, productos } from '../db/schema';
import { SafeAreaView } from 'react-native-safe-area-context';


const GuardarProducto = () => {
    const db = useSQLiteContext();

    /*
    const drizzleDb = drizzle(db, { schema: { personas } });
    const { data: listaCategorias } = useLiveQuery(drizzleDb.select().from(categorias));
    const { data: listaProductos } = useLiveQuery(drizzleDb.select().from(productos));

    async function almacenarCategoria(){
        let edadAleatoria = Math.trunc(Math.random()*100);
        let nombreAleatorio = "categoria_"+Math.trunc(Math.random()*10000);
        await drizzleDb.insert(categorias).values({ nombre: nombreAleatorio });
    }

    async function almacenarProducto(){
        let edadAleatoria = Math.trunc(Math.random()*100);
        let nombreAleatorio = "producto_"+Math.trunc(Math.random()*10000);
        let precioAleatorio = (Math.trunc(Math.random()*10000) ) / 100
        if( listaCategorias.length <= 0 ){
            Alert.alert("No hay categorías aún. No se guarda producto");
            return;
        }
        let posCategoria = Math.trunc(Math.random()*listaCategorias.length);
        let idCategoria = listaCategorias[posCategoria].id;

        await drizzleDb.insert(productos).values({ nombre: nombreAleatorio, precio: precioAleatorio, categoriaId: idCategoria });
    }
    */
    return (
        <SafeAreaView style={{ flex: 1, justifyContent: 'flex-start', alignItems: 'center' }}>



            {/*
            <Text style={{fontSize: 24, fontWeight: 'bold', marginBottom: 20  }}>Guardar Categoría</Text>
            <Button title='Guardar Categoria' onPress={almacenarCategoria} />
            <View style={{marginTop:20, flex: 1}}>
                <Text>Lista de categorias guardadas:</Text>
                {listaCategorias?.map((p)=><Text key={p.id}>{JSON.stringify(p)} </Text>)}
            </View>
            <Button title='Guardar Producto' onPress={almacenarProducto} />
            <View style={{marginTop:20, flex: 1}}>
                <Text>Lista de productos guardadas:</Text>
                {listaProductos?.map((p)=><Text key={p.id}>{JSON.stringify(p)}</Text>)}
            </View>
            */}
        </SafeAreaView>
    )
}

export default GuardarProducto

const styles = StyleSheet.create({})