You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

add_depense.js 4.8KB

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