Files
feladat3mobil/App.tsx
2026-02-25 09:16:09 +01:00

89 lines
3.1 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { useState, useEffect } from 'react';
import { View, Text, TextInput, Button, FlatList, TouchableOpacity, StyleSheet } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
interface Product {
id: string;
name: string;
price: number;
}
export default function App() {
const [products, setProducts] = useState<Product[]>([]);
const [name, setName] = useState('');
const [price, setPrice] = useState('');
const [editId, setEditId] = useState<string | null>(null);
useEffect(() => {
loadProducts();
}, []);
const loadProducts = async () => {
const data = await AsyncStorage.getItem('products');
if (data) setProducts(JSON.parse(data));
};
const saveProducts = async (newProducts: Product[]) => {
await AsyncStorage.setItem('products', JSON.stringify(newProducts));
setProducts(newProducts);
};
const addProduct = () => {
const newProduct = { id: Date.now().toString(), name, price: parseFloat(price) };
saveProducts([...products, newProduct]);
setName('');
setPrice('');
};
const updateProduct = () => {
const updated = products.map(p => p.id === editId ? { ...p, name, price: parseFloat(price) } : p);
saveProducts(updated);
setEditId(null);
setName('');
setPrice('');
};
const deleteProduct = (id: string) => {
saveProducts(products.filter(p => p.id !== id));
};
const sortByPrice = () => {
saveProducts([...products].sort((a, b) => a.price - b.price));
};
return (
<View style={styles.container}>
<TextInput style={styles.input} placeholder="Név" value={name} onChangeText={setName} />
<TextInput style={styles.input} placeholder="Ár" value={price} onChangeText={setPrice} keyboardType="numeric" />
<Button title={editId ? 'Frissítés' : 'Hozzáadás'} onPress={editId ? updateProduct : addProduct} color="#ff00ff" />
<Button title="Rendezés ár szerint" onPress={sortByPrice} color="#ff00ff" />
<FlatList
data={products}
keyExtractor={item => item.id}
renderItem={({ item }) => (
<View style={styles.item}>
<Text style={{ color: '#ffffff' }}>{item.name} - {item.price} Ft</Text>
<View style={styles.buttons}>
<TouchableOpacity onPress={() => { setEditId(item.id); setName(item.name); setPrice(item.price.toString()); }}>
<Text style={styles.edit}></Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => deleteProduct(item.id)}>
<Text style={styles.delete}>🗑</Text>
</TouchableOpacity>
</View>
</View>
)}
/>
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, padding: 16, backgroundColor: '#000000' },
input: { borderWidth: 1, borderColor: '#333333', padding: 8, marginBottom: 8, borderRadius: 4, backgroundColor: '#1a1a1a', color: '#ffffff' },
item: { padding: 16, borderBottomWidth: 1, borderBottomColor: '#333333', flexDirection: 'row', justifyContent: 'space-between' },
buttons: { flexDirection: 'row', gap: 8 },
edit: { fontSize: 18 },
delete: { fontSize: 18 },
});