Browse Source

edit depenses

master
DemiSel 2 years ago
parent
commit
65caaf37eb
6 changed files with 176 additions and 129 deletions
  1. 4
    1
      App.js
  2. 3
    96
      components/add_depense.js
  3. 129
    0
      components/depense_edit/depense_form.js
  4. 25
    8
      components/depense_list_entry.js
  5. 11
    22
      components/depenses_list.js
  6. 4
    2
      db/query.js

+ 4
- 1
App.js View File

@@ -40,7 +40,10 @@ export default function App() {
40 40
             durationMode={durationMode} 
41 41
             dailyExpense={dailyObjective}
42 42
             onChangeObjective={onChangeObjective}/>
43
-          <DepensesList depensesFetched={() => onFetchDepenses(false)} fetchDepenses={fetchDepenses} durationMode={durationMode} />
43
+          <DepensesList 
44
+            depensesFetched={() => onFetchDepenses(false)} 
45
+            fetchDepenses={fetchDepenses} 
46
+            durationMode={durationMode} />
44 47
           <StatusBar hidden />
45 48
       </View>
46 49
         {/* Overlay for expenses */}

+ 3
- 96
components/add_depense.js View File

@@ -1,88 +1,15 @@
1 1
 import React from 'react';
2 2
 import { StyleSheet, Text, View, TextInput, Button, Pressable } from 'react-native';
3
-import { sqlite_exec_parameterised } from '../db/query';
4
-import DateTimePicker from '@react-native-community/datetimepicker';
3
+import DepenseForm from './depense_edit/depense_form';
5 4
 
6 5
 
7 6
 export default function AddDepense(props){
8 7
     const [addingDepense, onAddDepense] = React.useState(false);
9
-    const [depenseValue, onValueEdit] = React.useState(null);
10
-    const [descriptionValue, onDescriptionEdit] = React.useState(null);
11
-    const [depenseDate, onDateChange] = React.useState(new Date())
12
-    const [timePickerDisplay, onDatePicker] = React.useState(false)
13
-
14
-    const toggleDateInput = () => {
15
-        onDatePicker(true);
16
-    }
17 8
 
18 9
     return (<View style={ add_depenses_styles.overlay }>
19 10
     <View >
20 11
         {addingDepense ? 
21
-            <View style={ add_depenses_styles.form_area }>
22
-                <Text> Nouvelle dépense</Text>
23
-                <View style={ add_depenses_styles.form_entry }>
24
-                    <Text> 💸</Text>
25
-                    <TextInput
26
-                        keyboardType="numeric"
27
-                        style={ add_depenses_styles.input }
28
-                        onChangeText={ (value) => {
29
-                            onValueEdit(value);
30
-                        }}
31
-                    />
32
-                </View>
33
-                <View style={ add_depenses_styles.form_entry }>
34
-                    <Text> 📃</Text>
35
-                    <TextInput style={ add_depenses_styles.input }
36
-                        onChangeText={ (value) => {
37
-                            onDescriptionEdit(value);
38
-                        }}
39
-                    />
40
-                </View>
41
-                <View style={ add_depenses_styles.form_entry }>
42
-                    <Text> 📅</Text>
43
-                    <TextInput 
44
-                    onPressIn={ () => toggleDateInput()}
45
-                    value={ depenseDate.toDateString() } style={ add_depenses_styles.input }
46
-                        onChangeText={ (value) => {
47
-                            onDescriptionEdit(value);
48
-                        }}
49
-                    />
50
-                    {/* https://github.com/react-native-datetimepicker/datetimepicker */}
51
-                    { timePickerDisplay && <DateTimePicker
52
-                        testID="dateTimePicker"
53
-                        value={depenseDate}
54
-                        mode={"date"}
55
-                        is24Hour={true}
56
-                        display="default"
57
-                        onChange={(event, value) => {
58
-                            console.log(value);
59
-                            console.log(value.valueOf());
60
-                            onDatePicker(false);
61
-                            onDateChange(new Date(value));
62
-                        }}
63
-                        />
64
-                    }
65
-                </View>
66
-                <Pressable 
67
-                    style={add_depenses_styles.button_add} 
68
-                    onPress={() => 
69
-                        {
70
-                            console.log(depenseValue);
71
-                            if(null != depenseValue)
72
-                            {
73
-                                sqlite_exec_parameterised(
74
-                                    "INSERT INTO expense (value, description, date) values (?, ?, ?)",
75
-                                    [depenseValue, descriptionValue, depenseDate.valueOf()], (res) => {
76
-                                        onAddDepense(false);
77
-                                    }
78
-                                    );
79
-                                props.fetchDepenses();
80
-                                
81
-                            }
82
-                        }}>
83
-                        <Text>Ajouter</Text>
84
-                    </Pressable>
85
-            </View>
12
+            <DepenseForm fetchDepenses={props.fetchDepenses} onAddDepense={onAddDepense}/>
86 13
          : <View/>}
87 14
     </View>
88 15
     <View style={ add_depenses_styles.button_add_parent }>
@@ -104,28 +31,8 @@ const add_depenses_styles = StyleSheet.create({
104 31
         height: "100%",
105 32
         width: "100%",
106 33
         position: 'absolute',
107
-
108
-    },
109
-    form_area: {
110
-        backgroundColor: '#f2f2f2',
111
-        zIndex: 10,
112
-        elevation: 10,
113
-        margin: 5,
114
-        marginTop: '50%',
115
-        display: 'flex',
116
-        flexDirection: 'column',
117
-        justifyContent: "space-between",
118
-        borderWidth: 1,
119
-    },
120
-    form_entry :{
121
-        display: 'flex',
122
-        flexDirection: 'row',
123
-    },
124
-    input :{
125
-        width: "50%",
126
-        borderWidth: 1,
127
-        textAlign: 'center',
128 34
     },
35
+    
129 36
     button_add: {
130 37
         borderWidth: 1,
131 38
         padding: 10,

+ 129
- 0
components/depense_edit/depense_form.js View File

@@ -0,0 +1,129 @@
1
+import React from 'react';
2
+import { StyleSheet, Text, View, TextInput, Button, Pressable } from 'react-native';
3
+import { sqlite_exec_parameterised } from '../../db/query';
4
+import DateTimePicker from '@react-native-community/datetimepicker';
5
+
6
+
7
+export default function DepenseForm(props){
8
+    const [depenseValue, onValueEdit] = 
9
+        React.useState(props.depense ? props.depense.value : null);
10
+    const [descriptionValue, onDescriptionEdit] = 
11
+        React.useState(props.depense ? props.depense.description : null);
12
+    const [depenseDate, onDateChange] = 
13
+        React.useState(props.depense ? new Date(props.depense.date) : new Date())
14
+    const [timePickerDisplay, onDatePicker] = 
15
+        React.useState(false)
16
+
17
+    let update_depense = null != props.depense;
18
+
19
+    const toggleDateInput = () => {
20
+        onDatePicker(true);
21
+    }
22
+
23
+    return (
24
+        <View style={ depense_form_styles.form_area }>
25
+                <Text> {update_depense ? "Modifier" : "Nouvelle dépense"}</Text>
26
+                <View style={ depense_form_styles.form_entry }>
27
+                    <Text> 💸</Text>
28
+                    <TextInput
29
+                        value={update_depense ? depenseValue.toString() : depenseValue ? depenseValue : ""}
30
+                        keyboardType="numeric"
31
+                        style={ depense_form_styles.input }
32
+                        onChangeText={ (value) => {
33
+                            onValueEdit(value);
34
+                        }}
35
+                    />
36
+                </View>
37
+                <View style={ depense_form_styles.form_entry }>
38
+                    <Text> 📃</Text>
39
+                    <TextInput 
40
+                        value={update_depense ? descriptionValue : descriptionValue ? descriptionValue : ""}
41
+                        style={ depense_form_styles.input }
42
+                        onChangeText={ (value) => {
43
+                            onDescriptionEdit(value);
44
+                        }}
45
+                    />
46
+                </View>
47
+                <View style={ depense_form_styles.form_entry }>
48
+                    <Text> 📅</Text>
49
+                    <TextInput 
50
+                    onPressIn={ () => toggleDateInput()}
51
+                    value={ depenseDate.toDateString() } style={ depense_form_styles.input }
52
+                        onChangeText={ (value) => {
53
+                            onDescriptionEdit(value);
54
+                        }}
55
+                    />
56
+                    {/* https://github.com/react-native-datetimepicker/datetimepicker */}
57
+                    { timePickerDisplay && <DateTimePicker
58
+                        testID="dateTimePicker"
59
+                        value={depenseDate}
60
+                        mode={"date"}
61
+                        is24Hour={true}
62
+                        display="default"
63
+                        onChange={(event, value) => {
64
+                            onDatePicker(false);
65
+                            onDateChange(new Date(value));
66
+                        }}
67
+                        />
68
+                    }
69
+                </View>
70
+                <Pressable 
71
+                    style={depense_form_styles.button_add} 
72
+                    onPress={() => 
73
+                        {
74
+                            if(null != depenseValue)
75
+                            {
76
+                                if(update_depense)
77
+                                {
78
+                                    sqlite_exec_parameterised(
79
+                                        "UPDATE expense SET value = ?, description = ?, date = ? WHERE id = ?",
80
+                                        [depenseValue, descriptionValue, depenseDate.valueOf(), props.depense.id],
81
+                                        (res) => {
82
+                                            props.onUpdateDepense(false)
83
+                                        }
84
+                                    );
85
+                                }
86
+                                else
87
+                                {
88
+                                    sqlite_exec_parameterised(
89
+                                        "INSERT INTO expense (value, description, date) values (?, ?, ?)",
90
+                                        [depenseValue, descriptionValue, depenseDate.valueOf()], (res) => {
91
+                                            props.onAddDepense(false);
92
+                                        }
93
+                                        );
94
+                                }
95
+                                props.fetchDepenses();
96
+                            }
97
+                        }}>
98
+                        <Text>{update_depense ? "Update" : "Add"}</Text>
99
+                    </Pressable>
100
+            </View>
101
+    );
102
+}
103
+
104
+const depense_form_styles = StyleSheet.create({
105
+    form_area: {
106
+        backgroundColor: '#ffffff',
107
+        zIndex: 10,
108
+        elevation: 10,
109
+        margin: 5,
110
+        // marginTop: '50%',
111
+        display: 'flex',
112
+        flexDirection: 'column',
113
+        justifyContent: "space-between",
114
+        borderWidth: 1,
115
+    },
116
+    form_entry :{
117
+        display: 'flex',
118
+        flexDirection: 'row',
119
+    },
120
+    input :{
121
+        width: "50%",
122
+        borderWidth: 1,
123
+        textAlign: 'center',
124
+    },
125
+    button_add: {
126
+        borderWidth: 1,
127
+        padding: 10,
128
+    },
129
+})

+ 25
- 8
components/depense_list_entry.js View File

@@ -1,24 +1,41 @@
1 1
 import React, { useEffect } from 'react';
2
-import { StyleSheet, Text, View, TextInput } from 'react-native';
2
+import { StyleSheet, Text, View, TextInput, Pressable } from 'react-native';
3
+import DepenseForm from './depense_edit/depense_form';
3 4
 
4 5
 
5 6
 export default function DepenseListEntry(props){
7
+    const [editing, toggleEdit] = React.useState(false);
6 8
 
7
-    return(
8
-        <View style={depense_list_entry_style.depense_list_entry}>
9
-                <Text >{props.depense.description}</Text>
10
-                <Text >{new Date(props.depense.date).toDateString()}</Text>
11
-                <Text >{props.depense.value}</Text>
12
-            </View>
9
+
10
+    return(<View style={depense_list_entry_style.depense_list_entry}>
11
+            <Pressable 
12
+            onPress={() => {
13
+                toggleEdit(!editing);
14
+                console.log(props.depense);
15
+            }}
16
+            style={depense_list_entry_style.pressable_depense_list_entry}>
17
+                    <Text >{props.depense.description}</Text>
18
+                    <Text >{new Date(props.depense.date).toDateString()}</Text>
19
+                    <Text >{props.depense.value}</Text>
20
+            </Pressable>
21
+            {editing ? 
22
+                <DepenseForm fetchDepenses={props.fetchDepenses} onUpdateDepense={(res) => {toggleEdit(res)}} depense={props.depense}/> : 
23
+                <View/>
24
+            }
25
+        
26
+        </View>
13 27
     )
14 28
 }
15 29
 
16 30
 const depense_list_entry_style = StyleSheet.create({
17 31
     depense_list_entry: {
32
+        borderBottomWidth: 0.5
33
+    },
34
+    pressable_depense_list_entry: {
18 35
         display: "flex",
19 36
         flexDirection: "row",
20 37
         justifyContent: "space-between",
21 38
         margin: 2,
22
-        borderBottomWidth: 0.5
39
+
23 40
     }
24 41
   });

+ 11
- 22
components/depenses_list.js View File

@@ -1,5 +1,6 @@
1 1
 import React, { useEffect } from 'react';
2
-import { StyleSheet, Text, View, TextInput } from 'react-native';
2
+import { StyleSheet, Text, View, TextInput, FlatList, SafeAreaView  } from 'react-native';
3
+import { ScrollView } from 'react-native-gesture-handler';
3 4
 import { sqlite_exec_query } from '../db/query';
4 5
 import DepenseListEntry from './depense_list_entry';
5 6
 
@@ -30,40 +31,28 @@ function DepensesList(props)
30 31
         let oldest_depense = depenses[depenses.length - 1]
31 32
         let curDate = new Date(latest_depense.date);
32 33
         let oldestDate = new Date(oldest_depense.date);
33
-        console.log(curDate);
34
-        console.log(oldestDate);
35
-        // while(curDate > oldestDate)
36
-        // {
37
-        //     console.log(curDate);
38
-        //     curDate.setDate( curDate.getDate() - 1);
39
-        // }
40
-
41 34
     }
42 35
         depenses
43 36
             .forEach( function(depense){
44
-            console.log(depense.date)
37
+            // console.log(depense.date)
45 38
             content.push(
46
-                <DepenseListEntry key={"depense_list_depense"+depense.id} depense={depense}/>
39
+                <DepenseListEntry fetchDepenses={() => preventFetchDepenses(false)} key={"depense_list_depense"+depense.id} depense={depense}/>
47 40
             )
48 41
         });
49 42
     
50 43
 
51 44
     return(
52
-    <View>
53
-        {content}
54
-    </View>
45
+    <SafeAreaView>
46
+        <ScrollView>
47
+            {content}
48
+        </ScrollView>
49
+    </SafeAreaView >
55 50
     )
56 51
 }
57 52
 
58 53
 const depense_list_style = StyleSheet.create({
59
-    depense_list_entry: {
60
-        display: "flex",
61
-        flexDirection: "row",
62
-        justifyContent: "space-between",
63
-    //   flex: 1,
64
-    //   backgroundColor: '#00FF00',
65
-    //   alignItems: 'center',
66
-    //   justifyContent: 'center',
54
+    list_container: {
55
+        flex: 1
67 56
     }
68 57
   });
69 58
 

+ 4
- 2
db/query.js View File

@@ -9,10 +9,12 @@ export function sqlite_exec_query(inQuery, inCallback){
9 9
 }
10 10
 
11 11
 export function sqlite_exec_parameterised(inQuery, inParams, inCallback){
12
-    // console.log(inQuery);
12
+    //console.log(inQuery);
13 13
     db.transaction((tx) => {
14 14
         tx.executeSql(inQuery, inParams,
15 15
         (txobj, res) => inCallback(res),
16
-        (txobj, err) => {console.log("query.js error |");console.log(err);})
16
+        (txobj, err) => {
17
+            console.log("query.js error |");console.log(err);
18
+        })
17 19
     })
18 20
 }

Loading…
Cancel
Save