-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
153 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,67 @@ | ||
import { useState } from 'react' | ||
import { RefreshControl } from 'react-native' | ||
import { ScrollView, Text } from 'react-native' | ||
import { CategoryItem } from '@/components/category/category-item' | ||
import { AddNewButton } from '@/components/common/add-new-button' | ||
import { Skeleton } from '@/components/ui/skeleton' | ||
import { Text } from '@/components/ui/text' | ||
import { useCategories } from '@/queries/category' | ||
import { t } from '@lingui/macro' | ||
import { useLingui } from '@lingui/react' | ||
import { useRouter } from 'expo-router' | ||
import { SectionList } from 'react-native' | ||
|
||
export default function CategoriesScreen() { | ||
const [isLoading, setIsLoading] = useState(false) | ||
const { i18n } = useLingui() | ||
const router = useRouter() | ||
const { data: categories = [], isLoading, refetch } = useCategories() | ||
|
||
const refetch = () => { | ||
setIsLoading(true) | ||
setTimeout(() => setIsLoading(false), 2000) | ||
} | ||
const incomeCategories = categories.filter( | ||
(category) => category.type === 'INCOME', | ||
) | ||
const expenseCategories = categories.filter( | ||
(category) => category.type === 'EXPENSE', | ||
) | ||
|
||
const sections = [ | ||
{ key: 'INCOME', title: 'Incomes', data: incomeCategories }, | ||
{ key: 'EXPENSE', title: 'Expenses', data: expenseCategories }, | ||
] | ||
|
||
return ( | ||
<ScrollView | ||
refreshControl={ | ||
<RefreshControl refreshing={isLoading} onRefresh={refetch} /> | ||
} | ||
className="py-3 px-6 bg-card flex-1" | ||
> | ||
<Text className="text-muted-foreground">Expenses</Text> | ||
</ScrollView> | ||
<SectionList | ||
className="py-3 bg-card flex-1" | ||
refreshing={isLoading} | ||
onRefresh={refetch} | ||
sections={sections} | ||
keyExtractor={(item) => item.id} | ||
renderItem={({ item: category }) => <CategoryItem category={category} />} | ||
renderSectionHeader={({ section: { title } }) => ( | ||
<Text className="text-muted-foreground mx-6">{title}</Text> | ||
)} | ||
renderSectionFooter={({ section }) => ( | ||
<> | ||
{!section.data.length && | ||
(isLoading ? ( | ||
<> | ||
<Skeleton className="mx-6 mb-5 mt-3 h-4 rounded-full" /> | ||
<Skeleton className="mx-6 mb-5 mt-3 h-4 rounded-full" /> | ||
<Skeleton className="mx-6 mb-5 mt-3 h-4 rounded-full" /> | ||
</> | ||
) : ( | ||
<Text className="font-sans text-muted-foreground text-center mt-6 mb-9"> | ||
{t(i18n)`empty`} | ||
</Text> | ||
))} | ||
<AddNewButton | ||
label={t(i18n)`New ${section.key.toLowerCase()}`} | ||
onPress={() => | ||
router.push({ | ||
pathname: '/categories/new-category', | ||
params: { type: section.key }, | ||
}) | ||
} | ||
className="mb-6" | ||
/> | ||
</> | ||
)} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { Category } from '@6pm/validation' | ||
import { Link } from 'expo-router' | ||
import type { FC } from 'react' | ||
import GenericIcon from '../common/generic-icon' | ||
import { MenuItem } from '../common/menu-item' | ||
|
||
type CategoryItemProps = { | ||
category: Category | ||
} | ||
|
||
export const CategoryItem: FC<CategoryItemProps> = ({ category }) => { | ||
return ( | ||
<Link | ||
asChild | ||
push | ||
href={{ | ||
pathname: '/categories/[categoryId]', | ||
params: { categoryId: category.id }, | ||
}} | ||
> | ||
<MenuItem | ||
label={category.name} | ||
icon={() => ( | ||
<GenericIcon | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
name={category.icon as any} | ||
className="size-6 text-foreground" | ||
/> | ||
)} | ||
/> | ||
</Link> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { getHonoClient } from '@/lib/client' | ||
import { CategorySchema } from '@6pm/validation' | ||
import { createQueryKeys } from '@lukemorales/query-key-factory' | ||
import { useQuery } from '@tanstack/react-query' | ||
|
||
export const categoryQueries = createQueryKeys('category', { | ||
list: () => ({ | ||
queryKey: [{}], | ||
queryFn: async () => { | ||
const hc = await getHonoClient() | ||
const res = await hc.v1.categories.$get() | ||
if (!res.ok) { | ||
throw new Error(await res.text()) | ||
} | ||
|
||
const items = await res.json() | ||
return items.map((item) => CategorySchema.parse(item)) | ||
}, | ||
}), | ||
}) | ||
|
||
export function useCategories() { | ||
return useQuery(categoryQueries.list()) | ||
} |