Refactor chart filter

pull/229/head
artemkolodko 2 years ago
parent 39a881da4d
commit fc688bae3f
  1. 19
      src/pages/ChartsPage/DailyChartPage.tsx
  2. 18
      src/pages/ChartsPage/utils.ts

@ -7,7 +7,6 @@ import {MetricsDailyItem} from "../../types";
import {
getDetailedChartOptions,
getChartData,
getLimitByFilterOption,
downloadMetricsCSV
} from './utils'
import {ChartFilter, ChartOption} from "./ChartFilter";
@ -64,6 +63,19 @@ export interface DailyChartPageProps {
renderMinValue: (value: string, date: string) => string
}
const chartItemFilter = (item: MetricsDailyItem, option: ChartOption) => {
const date = dayjs(item.date)
const current = dayjs()
switch(option) {
case ChartOption.month: return current.diff(date, 'month') <= 1
case ChartOption.month3: return current.diff(date, 'month') <= 3
case ChartOption.year: return current.diff(date, 'month') <= 12
case ChartOption.ytd: return current.year() === date.year()
default:
return true
}
}
export const DailyChartPage = (props: DailyChartPageProps) => {
const themeMode = useThemeMode();
@ -76,10 +88,9 @@ export const DailyChartPage = (props: DailyChartPageProps) => {
const [maxValue, setMaxValue] = useState<MetricsDailyItem>({value: '0', date: ''})
const applyFilter = (cachedData: MetricsDailyItem[]) => {
const limit = getLimitByFilterOption(filterOption)
const data = cachedData.slice(-limit)
const data = cachedData.filter((item) => chartItemFilter(item, filterOption))
const sortedData = [...data].sort((a, b) => +a.value - +b.value)
setItems(cachedData.slice(-limit))
setItems(data)
setMinValue(sortedData[0])
setMaxValue(sortedData[sortedData.length - 1])
}

@ -161,24 +161,6 @@ export const enrichResponse = (items: MetricsDailyItem[]) => {
}))
}
export const getLimitByFilterOption = (option: ChartOption) => {
switch(option) {
case ChartOption.month: return 30
case ChartOption.month3: return 30 * 3
case ChartOption.year: {
return 365
}
case ChartOption.ytd: {
const date1 = dayjs()
const date2 = dayjs().startOf('year')
return date1.diff(date2, 'day')
}
case ChartOption.all: return 2000
default:
return 1000
}
}
const downloadBlob = (content: any, filename: string) => {
const blob = new Blob([content], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);

Loading…
Cancel
Save