Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

depenses_list.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import React, { useEffect } from 'react';
  2. import { StyleSheet, Text, View, TextInput, FlatList, SafeAreaView, ColorPropType } from 'react-native';
  3. import { ScrollView } from 'react-native-gesture-handler';
  4. import { sqlite_exec_query } from '../db/query';
  5. import DepenseListEntry from './depense_list_entry';
  6. import DayList from './list/day_list';
  7. var getDaysArray = function(s,e) {for(var a=[],d=new Date(s);d<=e;d.setDate(d.getDate()+1)){ a.push(new Date(d));}return a;};
  8. function DepensesList(props)
  9. {
  10. const [depenses, onGetDepenses] = React.useState([]);
  11. const [depensesFetched, preventFetchDepenses] = React.useState(false)
  12. let fetchDepenses = props.fetchDepenses || !depensesFetched
  13. useEffect( () => {
  14. sqlite_exec_query(`create table if not exists expense (id integer primary key not null, value real, description text, date integer);`,(res)=>{
  15. if(fetchDepenses)
  16. sqlite_exec_query("select * from expense order by date desc", (res) => {
  17. onGetDepenses(res.rows._array);
  18. preventFetchDepenses(true);
  19. props.depensesFetched();
  20. });
  21. });
  22. });
  23. let content = []
  24. if(depenses.length > 0)
  25. {
  26. // get latest and oldest depense
  27. let latest_depense = depenses[0];
  28. let oldest_depense = depenses[depenses.length - 1]
  29. let curDate = new Date(latest_depense.date);
  30. let oldestDate = new Date(oldest_depense.date);
  31. let days = getDaysArray(oldestDate, curDate ).reverse();
  32. let days_disp = {};
  33. days.forEach( (day) => {
  34. // get depenses that are on the current day
  35. var depenses_day = depenses.filter( obj => {return new Date(obj.date).getDate() == day.getDate()} )
  36. content.push(
  37. <DayList
  38. dailyObjective={props.dailyObjective}
  39. fetchDepenses={() => preventFetchDepenses(false)}
  40. key={"day_list_"+day.toDateString()}
  41. date={day}
  42. depenses={depenses_day}/>)
  43. });
  44. }
  45. return(
  46. <View>
  47. <ScrollView>
  48. {content}
  49. </ScrollView>
  50. </View>
  51. )
  52. }
  53. const depense_list_style = StyleSheet.create({
  54. list_container: {
  55. flex: 1
  56. }
  57. });
  58. export default DepensesList