-
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.
feat(mobile): add create category screen
- Loading branch information
Showing
9 changed files
with
276 additions
and
29 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
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,23 @@ | ||
import { useState } from 'react' | ||
import { RefreshControl } from 'react-native' | ||
import { ScrollView, Text } from 'react-native' | ||
|
||
export default function CategoriesScreen() { | ||
const [isLoading, setIsLoading] = useState(false) | ||
|
||
const refetch = () => { | ||
setIsLoading(true) | ||
setTimeout(() => setIsLoading(false), 2000) | ||
} | ||
|
||
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> | ||
) | ||
} |
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 { CategoryForm } from '@/components/category/category-form' | ||
import { createCategory } from '@/mutations/category' | ||
import { useMutation } from '@tanstack/react-query' | ||
import { useRouter } from 'expo-router' | ||
import { Alert, View } from 'react-native' | ||
|
||
export default function CreateCategoryScreen() { | ||
const router = useRouter() | ||
const { mutateAsync } = useMutation({ | ||
mutationFn: createCategory, | ||
onError(error) { | ||
Alert.alert(error.message) | ||
}, | ||
onSuccess() { | ||
router.back() | ||
}, | ||
}) | ||
|
||
return ( | ||
<View className="py-3 px-6 bg-card h-screen"> | ||
<CategoryForm onSubmit={mutateAsync} /> | ||
</View> | ||
) | ||
} |
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,87 @@ | ||
import { type CategoryFormValues, zCategoryFormValues } from '@6pm/validation' | ||
import { zodResolver } from '@hookform/resolvers/zod' | ||
import { t } from '@lingui/macro' | ||
import { useLingui } from '@lingui/react' | ||
import { useRef } from 'react' | ||
import { Controller, FormProvider, useForm } from 'react-hook-form' | ||
import { View } from 'react-native' | ||
import type { TextInput } from 'react-native' | ||
import { InputField } from '../form-fields/input-field' | ||
import { SubmitButton } from '../form-fields/submit-button' | ||
import { Tabs, TabsList, TabsTrigger } from '../ui/tabs' | ||
import { Text } from '../ui/text' | ||
import { SelectCategoryIconField } from './select-category-icon-field' | ||
|
||
type CategoryFormProps = { | ||
onSubmit: (data: CategoryFormValues) => void | ||
defaultValues?: CategoryFormValues | ||
} | ||
|
||
export const CategoryForm = ({ | ||
onSubmit, | ||
defaultValues, | ||
}: CategoryFormProps) => { | ||
const { i18n } = useLingui() | ||
const nameInputRef = useRef<TextInput>(null) | ||
|
||
const categoryForm = useForm<CategoryFormValues>({ | ||
resolver: zodResolver(zCategoryFormValues), | ||
defaultValues: { | ||
name: '', | ||
type: 'EXPENSE', | ||
icon: 'CreditCard', | ||
...defaultValues, | ||
}, | ||
}) | ||
|
||
return ( | ||
<FormProvider {...categoryForm}> | ||
<View className="flex flex-1 gap-4"> | ||
<InputField | ||
ref={nameInputRef} | ||
name="name" | ||
label={t(i18n)`Name`} | ||
placeholder={t(i18n)`Category name`} | ||
autoCapitalize="none" | ||
autoFocus={!defaultValues} | ||
className="!pl-[62px]" | ||
leftSection={ | ||
<SelectCategoryIconField | ||
onSelect={() => nameInputRef.current?.focus()} | ||
/> | ||
} | ||
/> | ||
|
||
<Text className="font-medium">{t(i18n)`Type`}</Text> | ||
<Controller | ||
control={categoryForm.control} | ||
name="type" | ||
render={({ field }) => ( | ||
<Tabs | ||
value={field.value} | ||
className="-mt-3" | ||
onValueChange={field.onChange} | ||
> | ||
<TabsList> | ||
<TabsTrigger value="EXPENSE"> | ||
<Text>{t(i18n)`Expense`}</Text> | ||
</TabsTrigger> | ||
<TabsTrigger value="INCOME"> | ||
<Text>{t(i18n)`Income`}</Text> | ||
</TabsTrigger> | ||
</TabsList> | ||
</Tabs> | ||
)} | ||
/> | ||
|
||
<SubmitButton | ||
onPress={categoryForm.handleSubmit(onSubmit)} | ||
disabled={categoryForm.formState.isLoading} | ||
className="mt-4" | ||
> | ||
<Text>{t(i18n)`Save`}</Text> | ||
</SubmitButton> | ||
</View> | ||
</FormProvider> | ||
) | ||
} |
62 changes: 62 additions & 0 deletions
62
apps/mobile/components/category/select-category-icon-field.tsx
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,62 @@ | ||
import { BottomSheetBackdrop, BottomSheetModal } from '@gorhom/bottom-sheet' | ||
import { useRef } from 'react' | ||
|
||
import { WALLET_ICONS } from '@/lib/icons/wallet-icons' | ||
import { useController } from 'react-hook-form' | ||
import { Keyboard } from 'react-native' | ||
import GenericIcon from '../common/generic-icon' | ||
import { IconGridSheet } from '../common/icon-grid-sheet' | ||
import { Button } from '../ui/button' | ||
|
||
export function SelectCategoryIconField({ | ||
onSelect, | ||
}: { | ||
onSelect?: (currency: string) => void | ||
}) { | ||
const sheetRef = useRef<BottomSheetModal>(null) | ||
const { | ||
field: { onChange, onBlur, value }, | ||
// fieldState, | ||
} = useController({ name: 'icon' }) | ||
|
||
return ( | ||
<> | ||
<Button | ||
variant="ghost" | ||
onPress={() => { | ||
Keyboard.dismiss() | ||
sheetRef.current?.present() | ||
}} | ||
className="!border-r !h-11 !py-0 !px-0 !w-16 border-input rounded-r-none" | ||
> | ||
<GenericIcon name={value} className="size-6 text-primary" /> | ||
</Button> | ||
<BottomSheetModal | ||
ref={sheetRef} | ||
index={0} | ||
enableDynamicSizing | ||
enablePanDownToClose | ||
keyboardBehavior="extend" | ||
backdropComponent={(props) => ( | ||
<BottomSheetBackdrop | ||
{...props} | ||
appearsOnIndex={0} | ||
disappearsOnIndex={-1} | ||
enableTouchThrough | ||
/> | ||
)} | ||
> | ||
<IconGridSheet | ||
icons={WALLET_ICONS} | ||
value={value} | ||
onSelect={(icon) => { | ||
onChange(icon) | ||
sheetRef.current?.close() | ||
onBlur() | ||
onSelect?.(icon) | ||
}} | ||
/> | ||
</BottomSheetModal> | ||
</> | ||
) | ||
} |
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,16 @@ | ||
import { getHonoClient } from '@/lib/client' | ||
import { type CategoryFormValues, CategorySchema } from '@6pm/validation' | ||
|
||
export async function createCategory(data: CategoryFormValues) { | ||
const hc = await getHonoClient() | ||
const result = await hc.v1.categories.$post({ | ||
json: data, | ||
}) | ||
|
||
if (result.ok) { | ||
const category = CategorySchema.parse(await result.json()) | ||
return category | ||
} | ||
|
||
return result | ||
} |
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