Skip to content

Commit

Permalink
fix(mobile): fix wrong start day of week and remove date-fns (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
lamvh authored Sep 21, 2024
1 parent acdd24c commit 8fa2032
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
6 changes: 3 additions & 3 deletions apps/mobile/app/(app)/(tabs)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { useTransactionStore } from '@/stores/transaction/store'
import { dayjsExtended } from '@6pm/utilities'
import { t } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import { format } from 'date-fns/format'
import { groupBy, mapValues, orderBy, sumBy } from 'lodash-es'
import { useMemo, useState } from 'react'
import { SectionList, View } from 'react-native'
Expand Down Expand Up @@ -76,8 +75,9 @@ export default function HomeScreen() {
}

const transactionsGroupByDate = useMemo(() => {
const groupedByDay = groupBy(transactions, (transaction) =>
format(new Date(transaction.date), 'yyyy-MM-dd'),
const groupedByDay = groupBy(
transactions,
(transaction) => dayjsExtended(transaction.date).format('YYYY-MM-DD'),
)

const sectionDict = mapValues(groupedByDay, (transactions, key) => ({
Expand Down
4 changes: 2 additions & 2 deletions apps/mobile/app/(app)/budget/[budgetId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { Button } from '@/components/ui/button'
import { Text } from '@/components/ui/text'
import { formatDateShort } from '@/lib/date'
import { useBudget, useBudgetPeriodStats } from '@/stores/budget/hooks'
import { dayjsExtended } from '@6pm/utilities'
import type { TransactionPopulated } from '@6pm/validation'
import { format } from 'date-fns'
import { Link, useLocalSearchParams, useNavigation } from 'expo-router'
import { groupBy, map, mapValues, orderBy, sortBy, sumBy } from 'lodash-es'
import { SettingsIcon } from 'lucide-react-native'
Expand Down Expand Up @@ -63,7 +63,7 @@ export default function BudgetDetailScreen() {

const transactionsGroupByDate = useMemo(() => {
const groupedByDay = groupBy(transactions, (transaction) =>
format(new Date(transaction.date), 'yyyy-MM-dd'),
dayjsExtended(transaction.date).format('YYYY-MM-DD'),
)

const sectionDict = mapValues(groupedByDay, (transactions, key) => ({
Expand Down
17 changes: 12 additions & 5 deletions apps/mobile/components/common/date-range-picker.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { formatDateShort } from '@/lib/date'
import { cn, sleep } from '@/lib/utils'
import { dayjsExtended } from '@6pm/utilities'
import { type BottomSheetModal, BottomSheetView } from '@gorhom/bottom-sheet'
import { t } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import DateTimePicker from '@react-native-community/datetimepicker'
import { addDays } from 'date-fns/addDays'
import { subDays } from 'date-fns/subDays'
import * as Haptics from 'expo-haptics'
import { ArrowRightIcon } from 'lucide-react-native'
import { useRef, useState } from 'react'
Expand Down Expand Up @@ -125,13 +124,17 @@ export function DateRangePicker({
await sleep(500)
onChange?.([date, toDate])
if (!toDate) {
onChange?.([date, addDays(date, 1)])
onChange?.([date, dayjsExtended(date).add(1, 'day').toDate()])
sheetToRef.current?.present()
} else {
onChange?.([date, toDate])
}
}}
maximumDate={toDate ? subDays(toDate, 1) : maximumDate}
maximumDate={
toDate
? dayjsExtended(toDate).subtract(1, 'day').toDate()
: maximumDate
}
minimumDate={minimumDate}
/>
</BottomSheetView>
Expand All @@ -151,7 +154,11 @@ export function DateRangePicker({
onChange?.([fromDate, date])
}}
maximumDate={maximumDate}
minimumDate={fromDate ? addDays(fromDate, 1) : minimumDate}
minimumDate={
fromDate
? dayjsExtended(fromDate).add(1, 'day').toDate()
: minimumDate
}
/>
</BottomSheetView>
</BottomSheet>
Expand Down
4 changes: 2 additions & 2 deletions apps/mobile/components/home/wallet-statistics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export function WalletStatistics({
const timeRange = useMemo(() => {
if (view === HomeView.SpentThisWeek || view === HomeView.RevenueThisWeek) {
return {
from: dayjsExtended().startOf('week').toDate(),
to: dayjsExtended().endOf('week').toDate(),
from: dayjsExtended().startOf('isoWeek').toDate(),
to: dayjsExtended().endOf('isoWeek').toDate(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/utilities/src/date/dayjs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dayjsExtended from 'dayjs'
import quarterOfYear from 'dayjs/plugin/quarterOfYear'
import isoWeek from 'dayjs/plugin/isoWeek'

dayjsExtended.extend(quarterOfYear)
dayjsExtended.extend(isoWeek)

export { dayjsExtended }

0 comments on commit 8fa2032

Please sign in to comment.