Skip to content

Commit

Permalink
feat(home page): add warning text when categories changed
Browse files Browse the repository at this point in the history
  • Loading branch information
uigywnkiub committed Aug 8, 2024
1 parent c1c71ee commit 30da7b4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
12 changes: 12 additions & 0 deletions app/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,15 @@ export const deepCompareArrays = (array1: any[], array2: any[]): boolean => {

return true // If all elements are deeply equal, the arrays are identical.
}

export const pluralize = (
count: number,
singular: string,
plural: string,
): string => {
const pluralRules = new Intl.PluralRules('en-US')
const pluralCategory = pluralRules.select(count)
return pluralCategory === 'one' || pluralCategory === 'zero' || count === 0
? singular
: plural
}
40 changes: 28 additions & 12 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PiWarningOctagonFill } from 'react-icons/pi'

import type { Metadata } from 'next'

import DEFAULT_CATEGORIES from '@/public/data/default-categories.json'
Expand All @@ -11,19 +13,24 @@ import {

import {
createTransaction,
getBalance,
getCachedAllTransactions,
getCachedAuthSession,
getCurrency,
getTransactionLimit,
getTransactions,
getCachedBalance,
getCachedCurrency,
getCachedTransactionLimit,
getCachedTransactions,
resetCategories,
} from './lib/actions'
import type {
TGroupedTransactions,
TTotalsTransaction,
TTransaction,
} from './lib/types'
import { formatDate, getTransactionsWithChangedCategory } from './lib/utils'
import {
formatDate,
getTransactionsWithChangedCategory,
pluralize,
} from './lib/utils'
import BalanceLine from './ui/balance-line'
import InfoBadge from './ui/info-badge'
import NoTransactionsPlug from './ui/no-transaction-text'
Expand Down Expand Up @@ -51,14 +58,14 @@ export default async function Home({
const page = Number(searchParams?.[SEARCH_PARAM.PAGE]) || 1
const userTransactionLimit = query
? Infinity
: await getTransactionLimit(userId)
: await getCachedTransactionLimit(userId)
const limit = userTransactionLimit || DEFAULT_TRANSACTION_LIMIT
const offset = (page - 1) * limit
const [balance, currency, { transactions, totalEntries, totalPages }] =
await Promise.all([
getBalance(userId),
getCurrency(userId),
getTransactions(userId, offset, limit),
getCachedBalance(userId),
getCachedCurrency(userId),
getCachedTransactions(userId, offset, limit),
])

const haveCategories = transactions.every((t) => t.categories)
Expand All @@ -75,8 +82,11 @@ export default async function Home({
userCategories,
)

const transactionsWithChangedCategory =
getTransactionsWithChangedCategory(transactions)
const transactionsWithChangedCategory = getTransactionsWithChangedCategory(
await getCachedAllTransactions(userId),
)
const countTransactionsWithChangedCategory =
transactionsWithChangedCategory.length

const filteredTransactionsByQuery = transactions.filter((t) => {
return t.description.toLowerCase().includes(query.toLowerCase())
Expand Down Expand Up @@ -160,14 +170,20 @@ export default async function Home({
transactionsWithChangedCategory={transactionsWithChangedCategory}
currency={currency}
/>
<div className='mt-4'>
<div className='mx-auto mt-4 max-w-3xl'>
{!query && (
<PaginationList
totalPages={totalPages}
totalEntries={totalEntries}
limit={limit}
/>
)}
{countTransactionsWithChangedCategory > 0 && (
<p className='mt-4 text-center text-sm text-warning'>
<PiWarningOctagonFill className='inline animate-pulse' />{' '}
{`You still have ${countTransactionsWithChangedCategory} ${pluralize(countTransactionsWithChangedCategory, 'transaction', 'transactions')} with the old category.`}
</p>
)}
</div>
</>
)
Expand Down
2 changes: 1 addition & 1 deletion app/ui/transaction-form-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function TransactionFormEdit({ transaction }: TProps) {
<Badge
content=''
shape='rectangle'
color='danger'
color='warning'
variant='solid'
size='sm'
isDot
Expand Down
4 changes: 2 additions & 2 deletions app/ui/transaction-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Time: ${formatTime(createdAt)}`
<Badge
content=''
shape='rectangle'
color='danger'
color='warning'
variant='solid'
size='sm'
isDot
Expand Down Expand Up @@ -209,7 +209,7 @@ Time: ${formatTime(createdAt)}`
<Badge
content=''
shape='rectangle'
color='danger'
color='warning'
variant='solid'
size='sm'
isDot
Expand Down

0 comments on commit 30da7b4

Please sign in to comment.