Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
fix: fix bug with timezones and group generation
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Mar 6, 2019
1 parent aa0a352 commit 29c817a
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 16 deletions.
3 changes: 2 additions & 1 deletion back/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
process.env.TZ = 'UTC'

import { NestFactory } from '@nestjs/core'

import { setupCors } from '@back/addons/setupCors'
import { setupLogger } from '@back/addons/setupLogger'
import { setupSwagger } from '@back/addons/setupSwagger'
import { AppModule } from '@back/app.module'
import { TelegramBot } from 'nest-telegram'
import { setupTelegram } from './addons/setupTelegram'

async function bootstrap() {
Expand Down
4 changes: 2 additions & 2 deletions back/src/utils/infrastructure/dateGroups/createMonthGroups.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addMonths, startOfMonth } from 'date-fns'
import { addMonths, startOfMonth, subMonths } from 'date-fns'

import { DateRange } from '../dto/DateRange'
import { DateGroup } from './DateGroup'
Expand All @@ -8,7 +8,7 @@ export const createMonthGroups = ({ from, to }: DateRange): DateGroup[] => {
const groups = []

let now = startOfMonth(from)
while (now < to) {
while (now < subMonths(to, 1)) {
const next = addMonths(now, 1)
const nowYear = now.getFullYear()
groups.push({
Expand Down
4 changes: 2 additions & 2 deletions back/src/utils/infrastructure/dateGroups/createWeekGroups.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addWeeks, getISOWeek, startOfWeek } from 'date-fns'
import { addWeeks, getISOWeek, startOfWeek, subWeeks } from 'date-fns'

import { DateRange } from '../dto/DateRange'
import { DateGroup } from './DateGroup'
Expand All @@ -8,7 +8,7 @@ export const createWeekGroups = ({ from, to }: DateRange): DateGroup[] => {
const groups = []

let now = startOfWeek(from)
while (now < to) {
while (now < subWeeks(to, 1)) {
const next = addWeeks(now, 1)
const nowYear = now.getFullYear()
groups.push({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const createYearGroups = ({ from, to }: DateRange): DateGroup[] => {
const firstYear = from.getFullYear()
const lastYear = to.getFullYear()

const years = range(firstYear, lastYear + 1)
const years = range(firstYear, lastYear)

return years.map(year => ({
title: year.toString(),
Expand Down
7 changes: 4 additions & 3 deletions front/src/features/app/features/history/History.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { Period } from '@front/ui/components/form/period'
import { Loader } from '@front/ui/components/layout/loader'
import { GroupBy } from '@shared/enum/GroupBy'
import { ControlHeader } from '@front/ui/components/controls/control-header'
import { useActualDateRange } from '@front/ui/hooks/useActualDateRange'
import { wantUTC } from '@front/helpers/wantUTC'

import { Incomes } from './components/Incomes'
import { Outcomes } from './components/Outcomes'
import * as styles from './History.css'
import { useActualDateRange } from '@front/ui/hooks/useActualDateRange'

interface Props {
className?: string
Expand All @@ -29,8 +30,8 @@ export const History = ({ className }: Props) => {
const { from, setFrom, to, setTo, actualFrom, actualTo } = useActualDateRange(
new Date(),
new Date(),
startOfMonth,
endOfMonth,
wantUTC(startOfMonth),
wantUTC(endOfMonth),
)

const history = useMemoState(
Expand Down
5 changes: 3 additions & 2 deletions front/src/features/app/features/stats/Stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { GroupBy } from '@shared/enum/GroupBy'
import { ControlHeader } from '@front/ui/components/controls/control-header'
import { CurrencySwitch } from '@front/ui/components/controls/currency-switch'
import { useActualDateRange } from '@front/ui/hooks/useActualDateRange'
import { wantUTC } from '@front/helpers/wantUTC'

const groupBy = GroupBy.Year

Expand All @@ -31,8 +32,8 @@ export const Stats = ({ className }: Props) => {
const { from, setFrom, to, setTo, actualFrom, actualTo } = useActualDateRange(
firstTransactionDate,
new Date(),
startOfYear,
endOfYear,
wantUTC(startOfYear),
wantUTC(endOfYear),
)

const [currency, setCurrency] = useState(Currency.USD)
Expand Down
3 changes: 2 additions & 1 deletion front/src/features/statistics/features/monthly/Monthly.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { GroupBy } from '@shared/enum/GroupBy'
import { ControlHeader } from '@front/ui/components/controls/control-header'
import { YearPicker } from '@front/ui/components/form/year-picker'
import { getFirstTransactionDate } from '@front/domain/money/selectors/getFirstTransactionDate'
import { wantUTC } from '@front/helpers/wantUTC'

const groupBy = GroupBy.Month

Expand All @@ -34,7 +35,7 @@ export const Monthly = ({ className, currency }: Props) => {
const [from, to] = useMemo(() => {
const date = parse(`${year}-01-01`)

return [startOfYear(date), endOfYear(date)]
return [wantUTC(startOfYear)(date), wantUTC(endOfYear)(date)]
}, [year])

const stats = useMemoState(() => getStats(from, to, groupBy, currency), [
Expand Down
5 changes: 3 additions & 2 deletions front/src/features/statistics/features/yearly/Yearly.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Currency } from '@shared/enum/Currency'
import { GroupBy } from '@shared/enum/GroupBy'
import { ControlHeader } from '@front/ui/components/controls/control-header'
import { useMemoState } from '@front/domain/store'
import { wantUTC } from '@front/helpers/wantUTC'

const groupBy = GroupBy.Year

Expand All @@ -29,10 +30,10 @@ export const Yearly = ({ className, currency }: Props) => {
const dispatch = useThunk()
const isSmall = useMedia({ maxWidth: 768 })

const from = useMemo(() => startOfYear(firstTransactionDate), [
const from = useMemo(() => wantUTC(startOfYear)(firstTransactionDate), [
firstTransactionDate,
])
const to = useMemo(() => endOfYear(new Date()), [])
const to = useMemo(() => wantUTC(endOfYear)(new Date()), [])

const stats = useMemoState(() => getStats(from, to, groupBy, currency), [
from,
Expand Down
10 changes: 10 additions & 0 deletions front/src/helpers/wantUTC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { addHours } from 'date-fns'

export const wantUTC = <T extends []>(f: (d: Date, ...args: T) => Date) => (
date: Date,
...args: T
) => {
const newDate = f(date, ...args)

return addHours(newDate, -newDate.getTimezoneOffset() / 60)
}
3 changes: 1 addition & 2 deletions front/src/ui/hooks/useActualDateRange.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState } from 'react'
import { useMemo } from 'react'
import { useState, useMemo } from 'react'

export const useActualDateRange = (
initialFrom: Date,
Expand Down

0 comments on commit 29c817a

Please sign in to comment.