Advertisement
bayuanggara

react-date-range

Apr 6th, 2020
2,787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState } from 'react';
  2. import { DateRangePicker, createStaticRanges } from 'react-date-range';
  3. import {
  4.     addDays,
  5.     subDays,
  6.     endOfDay,
  7.     startOfDay,
  8.     startOfMonth,
  9.     endOfMonth,
  10.     addMonths,
  11.     startOfWeek,
  12.     endOfWeek,
  13.     startOfYear,
  14.     endOfYear,
  15.     addYears
  16. } from 'date-fns';
  17. import { Grid, Typography } from '@material-ui/core';
  18. import './style.css';
  19. import { Info, Receipt } from '@material-ui/icons';
  20. import CardInfo from '../common/cardInfo';
  21. import RegisteredChart from './charts/registeredChart';
  22.  
  23. const defineds = {
  24.     startOfWeek: startOfWeek(new Date()),
  25.     endOfWeek: endOfWeek(new Date()),
  26.     startOfLastWeek: startOfWeek(addDays(new Date(), -7)),
  27.     endOfLastWeek: endOfWeek(addDays(new Date(), -7)),
  28.     startOfToday: startOfDay(new Date()),
  29.     startOfLastSevenDay: startOfDay(addDays(new Date(), -7)),
  30.     startOfLastThirtyDay: startOfDay(addDays(new Date(), -30)),
  31.     startOfLastNintyDay: startOfDay(addDays(new Date(), -90)),
  32.     endOfToday: endOfDay(new Date()),
  33.     startOfYesterday: startOfDay(addDays(new Date(), -1)),
  34.     endOfYesterday: endOfDay(addDays(new Date(), -1)),
  35.     startOfMonth: startOfMonth(new Date()),
  36.     endOfMonth: endOfMonth(new Date()),
  37.     startOfLastMonth: startOfMonth(addMonths(new Date(), -1)),
  38.     endOfLastMonth: endOfMonth(addMonths(new Date(), -1)),
  39.     startOfYear: startOfYear(new Date()),
  40.     endOfYear: endOfYear(new Date()),
  41.     startOflastYear: startOfYear(addYears(new Date(), -1)),
  42.     endOflastYear: endOfYear(addYears(new Date(), -1))
  43. };
  44.  
  45. const sideBarOptions = () => {
  46.     const customDateObjects = [
  47.         {
  48.             label: 'Lifetime',
  49.             range: () => ({
  50.                 startDate: defineds.startOfToday,
  51.                 endDate: defineds.endOfToday
  52.             })
  53.         },
  54.         {
  55.             label: 'Today',
  56.             range: () => ({
  57.                 startDate: defineds.startOfToday,
  58.                 endDate: defineds.endOfToday
  59.             })
  60.         },
  61.         {
  62.             label: 'Last 7 Days',
  63.             range: () => ({
  64.                 startDate: defineds.startOfLastSevenDay,
  65.                 endDate: defineds.endOfToday
  66.             })
  67.         },
  68.         {
  69.             label: 'Last 30 Days',
  70.             range: () => ({
  71.                 startDate: defineds.startOfLastThirtyDay,
  72.                 endDate: defineds.endOfToday
  73.             })
  74.         },
  75.         {
  76.             label: 'Last 90 Days',
  77.             range: () => ({
  78.                 startDate: defineds.startOfLastNintyDay,
  79.                 endDate: defineds.endOfToday
  80.             })
  81.         },
  82.         {
  83.             label: 'This Week',
  84.             range: () => ({
  85.                 startDate: defineds.startOfWeek,
  86.                 endDate: defineds.endOfWeek
  87.             })
  88.         },
  89.         {
  90.             label: 'Last Week',
  91.             range: () => ({
  92.                 startDate: defineds.startOfLastWeek,
  93.                 endDate: defineds.endOfLastWeek
  94.             })
  95.         },
  96.         {
  97.             label: 'This Month',
  98.             range: () => ({
  99.                 startDate: defineds.startOfMonth,
  100.                 endDate: defineds.endOfMonth
  101.             })
  102.         },
  103.         {
  104.             label: 'Last Month',
  105.             range: () => ({
  106.                 startDate: defineds.startOfLastMonth,
  107.                 endDate: defineds.endOfLastMonth
  108.             })
  109.         },
  110.         {
  111.             label: 'This Year',
  112.             range: () => ({
  113.                 startDate: defineds.startOfYear,
  114.                 endDate: defineds.endOfYear
  115.             })
  116.         },
  117.         {
  118.             label: 'Last Year',
  119.             range: () => ({
  120.                 startDate: defineds.startOflastYear,
  121.                 endDate: defineds.endOflastYear
  122.             })
  123.         }
  124.     ];
  125.  
  126.     return customDateObjects;
  127. };
  128.  
  129. const ChartSection = ({ classes, handleClick, data, user }) => {
  130.     const [ state, setState ] = useState([
  131.         {
  132.             startDate: subDays(new Date(), 90),
  133.             endDate: new Date(),
  134.             key: 'selection'
  135.         }
  136.     ]);
  137.     const sideBar = sideBarOptions();
  138.  
  139.     const staticRanges = [
  140.         // ...defaultStaticRanges,
  141.         ...createStaticRanges(sideBar)
  142.     ];
  143.     return (
  144.         <Grid container direction="column">
  145.             <Grid item>
  146.                 <Typography color="primary" variant="h5" className={classes.sectionTitle}>
  147.                     Charts
  148.                 </Typography>
  149.             </Grid>
  150.             <Grid item className={classes.container}>
  151.                 <Grid container direction="column">
  152.                     <Grid style={{ padding: 8 }}>
  153.                         <DateRangePicker
  154.                             onChange={(item) => {
  155.                                 console.log('item', item);
  156.                                 setState([ item.selection ]);
  157.                             }}
  158.                             showSelectionPreview={true}
  159.                             moveRangeOnFirstSelection={false}
  160.                             months={2}
  161.                             ranges={state}
  162.                             direction="horizontal"
  163.                             staticRanges={staticRanges}
  164.                         />
  165.                     </Grid>
  166.                 </Grid>
  167.             </Grid>
  168.         </Grid>
  169.     );
  170. };
  171. export default ChartSection;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement