@@ -10,6 +10,7 @@ import {
1010 TextInput ,
1111 Alert ,
1212 ActivityIndicator ,
13+ Modal ,
1314} from 'react-native' ;
1415import { openDatabase , SQLiteDatabase } from 'react-native-sqlite-windows' ;
1516
@@ -32,6 +33,9 @@ const App = () => {
3233 const [ email , setEmail ] = useState ( '' ) ;
3334 const [ age , setAge ] = useState ( '' ) ;
3435
36+ const [ editVisible , setEditVisible ] = useState ( false ) ;
37+ const [ editUser , setEditUser ] = useState < any | null > ( null ) ;
38+ const [ editName , setEditName ] = useState ( '' ) ;
3539 // Initialize database
3640 const initDatabase = async ( ) => {
3741 try {
@@ -125,7 +129,7 @@ const App = () => {
125129 setLoading ( true ) ;
126130 const result = await db . executeSql (
127131 'INSERT INTO users (name, email, age) VALUES (?, ?, ?)' ,
128- [ name , email , parseInt ( age ) ]
132+ [ name , email , parseInt ( age , 10 ) ]
129133 ) ;
130134
131135 // Ensure insertId is valid number
@@ -148,27 +152,27 @@ const App = () => {
148152 }
149153 } ;
150154
151- // Update user
152- // const updateUser = async (id: number, newName: string) => {
153- // if (!db) return;
154-
155- // try {
156- // setLoading(true);
157- // const result = await db.executeSql(
158- // 'UPDATE users SET name = ? WHERE id = ?',
159- // [newName, id]
160- // );
161-
162- // setStatus(`Updated ${result.rowsAffected} user(s)`);
163- // Alert.alert('Success', `User updated successfully`);
164- // await loadUsers();
165- // } catch (error) {
166- // setStatus(`Error updating user: ${error}`);
167- // Alert.alert('Update Error', String(error));
168- // } finally {
169- // setLoading(false);
170- // }
171- // };
155+ //Update user
156+ const updateUser = async ( id : number , newName : string ) => {
157+ if ( ! db ) return ;
158+
159+ try {
160+ setLoading ( true ) ;
161+ const result = await db . executeSql (
162+ 'UPDATE users SET name = ? WHERE id = ?' ,
163+ [ newName , id ]
164+ ) ;
165+
166+ setStatus ( `Updated ${ result . rowsAffected } user(s)` ) ;
167+ Alert . alert ( 'Success' , `User updated successfully` ) ;
168+ await loadUsers ( ) ;
169+ } catch ( error ) {
170+ setStatus ( `Error updating user: ${ error } ` ) ;
171+ Alert . alert ( 'Update Error' , String ( error ) ) ;
172+ } finally {
173+ setLoading ( false ) ;
174+ }
175+ } ;
172176
173177 // Delete user
174178 const deleteUser = async ( id : number , userName : string ) => {
@@ -486,14 +490,14 @@ const App = () => {
486490 < Text style = { styles . userDetail } > Age: { user . age } </ Text >
487491 </ View >
488492 < View style = { styles . userActions } >
489- { /* <Button
493+ < Button
490494 title = "Edit"
491495 onPress = { ( ) => {
492- Alert.alert('Update Name', 'Enter new name:', (text) =>
493- updateUser (user.id, text)
494- );
496+ setEditUser ( user ) ;
497+ setEditName ( user . name ) ;
498+ setEditVisible ( true ) ;
495499 } }
496- /> */ }
500+ />
497501 < Button
498502 title = "Delete"
499503 color = "red"
@@ -505,6 +509,37 @@ const App = () => {
505509 ) }
506510 </ View >
507511 ) }
512+ { /* Edit Modal */ }
513+ < Modal visible = { editVisible } transparent animationType = "fade" >
514+ < View style = { styles . modalOverlay } >
515+ < View style = { styles . modalContainer } >
516+ < Text style = { styles . sectionTitle } > Edit User</ Text >
517+ < TextInput
518+ style = { styles . input }
519+ placeholder = "Enter new name"
520+ value = { editName }
521+ onChangeText = { setEditName }
522+ />
523+ < View style = { styles . buttonRow } >
524+ < Button
525+ title = "Save"
526+ onPress = { ( ) => {
527+ if ( editUser && editName . trim ( ) ) {
528+ updateUser ( editUser . id , editName . trim ( ) ) ;
529+ setEditVisible ( false ) ;
530+ Alert . alert ( 'Success' , 'User updated' ) ;
531+ }
532+ } }
533+ />
534+ < Button
535+ title = "Cancel"
536+ color = "gray"
537+ onPress = { ( ) => setEditVisible ( false ) }
538+ />
539+ </ View >
540+ </ View >
541+ </ View >
542+ </ Modal >
508543 </ ScrollView >
509544 </ SafeAreaView >
510545 ) ;
@@ -604,6 +639,19 @@ const styles = StyleSheet.create({
604639 fontSize : 16 ,
605640 padding : 20 ,
606641 } ,
642+ modalOverlay : {
643+ flex : 1 ,
644+ backgroundColor : 'rgba(0,0,0,0.4)' ,
645+ justifyContent : 'center' ,
646+ alignItems : 'center' ,
647+ } ,
648+ modalContainer : {
649+ backgroundColor : 'white' ,
650+ padding : 20 ,
651+ borderRadius : 12 ,
652+ width : '80%' ,
653+ elevation : 5 ,
654+ } ,
607655} ) ;
608656
609657export default App ;
0 commit comments