diff options
| author | Alberto Duarte Delgado <alberto@albertoduarte.com> | 2026-03-25 15:32:20 +0000 |
|---|---|---|
| committer | Alberto Duarte Delgado <alberto@albertoduarte.com> | 2026-03-25 15:32:20 +0000 |
| commit | 7809ccbba56be8c5377f94cf2d287a0e1521fdc1 (patch) | |
| tree | 1c590049df5546f141eeb3bf58ad5ea9d1e0fbf6 /src/app | |
| parent | d071a19fdeb0fc369e356004118f59a7fe4fed91 (diff) | |
Initial commit: App Oficina base project
Diffstat (limited to 'src/app')
| -rw-r--r-- | src/app/GuardarPersona.tsx | 34 | ||||
| -rw-r--r-- | src/app/GuardarProducto.tsx | 61 | ||||
| -rw-r--r-- | src/app/_layout.tsx | 58 | ||||
| -rw-r--r-- | src/app/index.tsx | 39 |
4 files changed, 192 insertions, 0 deletions
diff --git a/src/app/GuardarPersona.tsx b/src/app/GuardarPersona.tsx new file mode 100644 index 0000000..58b1d2d --- /dev/null +++ b/src/app/GuardarPersona.tsx @@ -0,0 +1,34 @@ +import { StyleSheet, Text, View, Button} from 'react-native' +import React from 'react' +import { useSQLiteContext } from 'expo-sqlite'; +import { drizzle, useLiveQuery } from 'drizzle-orm/expo-sqlite'; +import { personas } from '../db/schema'; +import { SafeAreaView } from 'react-native-safe-area-context'; + + +const GuardarPersona = () => { + const db = useSQLiteContext(); + const drizzleDb = drizzle(db, { schema: { personas } }); + const { data: lista } = useLiveQuery(drizzleDb.select().from(personas)); + async function almacenarPersona(){ + let edadAleatoria = Math.trunc(Math.random()*100); + let nombreAleatorio = "persona_"+Math.trunc(Math.random()*10000); + await drizzleDb.insert(personas).values({ nombre: nombreAleatorio, edad: edadAleatoria }); + } + + return ( + <SafeAreaView style={{ flex: 1, justifyContent: 'flex-start', alignItems: 'center' }}> + <Text style={{fontSize: 24, fontWeight: 'bold', marginBottom: 20 }}>Guardar Persona</Text> + <Button title='Guardar Persona' onPress={almacenarPersona} /> + <View style={{marginTop:20, flex: 1}}> + <Text>Lista de personas guardadas:</Text> + {lista?.map((p)=><Text key={p.id}>{p.id} - {p.nombre} - {p.edad} años</Text>)} + </View> + + </SafeAreaView> + ) +} + +export default GuardarPersona + +const styles = StyleSheet.create({})
\ No newline at end of file diff --git a/src/app/GuardarProducto.tsx b/src/app/GuardarProducto.tsx new file mode 100644 index 0000000..abdd8c1 --- /dev/null +++ b/src/app/GuardarProducto.tsx @@ -0,0 +1,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({})
\ No newline at end of file diff --git a/src/app/_layout.tsx b/src/app/_layout.tsx new file mode 100644 index 0000000..e2eb936 --- /dev/null +++ b/src/app/_layout.tsx @@ -0,0 +1,58 @@ +import { Slot, Stack } from "expo-router"; +import React, { Suspense } from 'react' +import AppContextProvider from "../context/AppContextProvider"; +import { ActivityIndicator, Text, View } from "react-native"; +import { openDatabaseSync, SQLiteProvider } from "expo-sqlite"; +import { drizzle } from "drizzle-orm/expo-sqlite"; +import { sql } from 'drizzle-orm'; +import { migrate, useMigrations } from "drizzle-orm/expo-sqlite/migrator"; +import migrations from "../../drizzle/migrations"; +type Props = {} +export const DATABASE_NAME = 'prueba3Drizzle'; +const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); +function LoadingScreen() { + return ( + <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> + <ActivityIndicator size="large" color="#0000ff" /> + <Text >Cargando la app...</Text> + </View> + ); +} +const RootLayout = (props: Props) => { + return ( + <AppContextProvider> + <Suspense fallback={<LoadingScreen />}> + <SQLiteProvider + databaseName={DATABASE_NAME} + options={{ enableChangeListener: true }} + useSuspense + onInit={async (database) => { + try { + console.log("Simulando carga lenta..."); + await sleep(2000); // 2 segundos retraso + const db = drizzle(database); + await migrate(db, migrations); + console.log("Migration success"); + + // Activa FK aquí (después de migraciones) + await db.run(sql`PRAGMA foreign_keys = ON`); + console.log("Foreign keys enabled"); + + } catch (error) { + console.error("Migration error", error); + } + }} + > + <Stack + screenOptions={{ headerShown: false }} + + /> + + </SQLiteProvider> + </Suspense> + </AppContextProvider> + + ) +} + +export default RootLayout;
\ No newline at end of file diff --git a/src/app/index.tsx b/src/app/index.tsx new file mode 100644 index 0000000..cf5f7c1 --- /dev/null +++ b/src/app/index.tsx @@ -0,0 +1,39 @@ +import { router } from "expo-router"; +import { Button, Text, View } from "react-native"; +import { SafeAreaView } from "react-native-safe-area-context"; + +export default function Index() { + + + function navegarGuardarPersona(){ + console.log("ir a pantalla") + router.push({ + pathname: "/GuardarPersona", + //params: + }) + } + + + function navegarGuardarProducto(){ + console.log("ir a pantalla") + router.push({ + pathname: "/GuardarProducto", + //params: + }) + } + return ( + <SafeAreaView + style={{ + flex: 1, + justifyContent: "center", + alignItems: "center", + }} + > + + <Button title="crear usuario " testID="btn-crear-usuario" color={"green"} onPress={()=> navegarGuardarPersona()} /> + {/* + <Button title="crear categorias y productos " testID="btn-crear-categoria-producto" color={"blue"} onPress={()=> navegarGuardarProducto()} /> + */} + </SafeAreaView> + ); +} |