Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. const Summary = () => {
  2. const [timeSpan, setTimeSpan] = useState('Day');
  3. const [derivedData, setDerivedData] = useState({
  4. chartTimeSpan: 'Day',
  5. sales: [],
  6. totalSales: 0,
  7. orders: 0,
  8. logins: 0,
  9. });
  10.  
  11. const _fetchSummary = async (timeSpan) => {
  12. console.log(`_fetchSummary HIT : ${timeSpan}`);
  13. try {
  14. const res = await axios.get(`/summary/${timeSpan.toLowerCase()}`);
  15. const { loginCount, orderQty, sales, totalSales } = res.data;
  16. await setDerivedData({
  17. chartTimeSpan: timeSpan,
  18. sales,
  19. totalSales,
  20. orders: orderQty,
  21. logins: loginCount,
  22. });
  23. await setTimeSpan(timeSpan);
  24. console.log(timeSpan, loginCount, orderQty, sales, totalSales);
  25. } catch (err) {
  26. console.log(err);
  27. }
  28. };
  29.  
  30. const _switchTimeSpan = (newTimeSpan) => {
  31. console.log(`TimeSpan : ${timeSpan}`);
  32. console.log(`NewTimeSpan : ${newTimeSpan}`);
  33. if (timeSpan !== newTimeSpan) {
  34. _fetchSummary(newTimeSpan);
  35. }
  36. };
  37.  
  38. const { chartTimeSpan, sales, totalSales, orders, logins } = derivedData;
  39. console.log(derivedData);
  40. return (
  41. <>
  42. <TouchableOpacity onPress={() => _switchTimeSpan('Day')}>
  43. <Text style={dropDownItemStyle}>Day</Text>
  44. </TouchableOpacity>
  45. <TouchableOpacity onPress={() => _switchTimeSpan('Week')}>
  46. <Text style={dropDownItemStyle}>Week</Text>
  47. </TouchableOpacity>
  48. <TouchableOpacity onPress={() => _switchTimeSpan('Month')}>
  49. <Text style={dropDownItemStyle}>Month</Text>
  50. </TouchableOpacity>
  51. <TouchableOpacity onPress={() => _switchTimeSpan('Year')}>
  52. <Text style={dropDownItemStyle}>Year</Text>
  53. </TouchableOpacity>
  54. </>
  55. );
  56. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement