N-FIN-42: add default category selection to form

This commit is contained in:
Markus Thielker 2024-03-17 12:15:28 +01:00
parent 228aa983e0
commit 715ce17e1f
No known key found for this signature in database
4 changed files with 34 additions and 2 deletions

View file

@ -147,6 +147,7 @@ export default function EntityPageClientContent({entities, categories, onSubmit,
</DialogHeader>
<EntityForm
value={selectedEntity}
categories={categories}
onSubmit={handleSubmit}
className="grid grid-cols-1 md:grid-cols-2 gap-4 py-4"/>
</DialogContent>
@ -168,6 +169,7 @@ export default function EntityPageClientContent({entities, categories, onSubmit,
</DrawerHeader>
<EntityForm
value={selectedEntity}
categories={categories}
onSubmit={handleSubmit}
className="grid grid-cols-1 md:grid-cols-2 gap-4 py-4"/>
</DrawerContent>

View file

@ -12,11 +12,13 @@ import { useRouter } from 'next/navigation';
import { toast } from 'sonner';
import { sonnerContent } from '@/components/ui/sonner';
import { entityFormSchema } from '@/lib/form-schemas/entityFormSchema';
import { Entity, EntityType } from '@prisma/client';
import { Category, Entity, EntityType } from '@prisma/client';
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { AutoCompleteInput } from '@/components/ui/auto-complete-input';
export default function EntityForm({value, onSubmit, className}: {
export default function EntityForm({value, categories, onSubmit, className}: {
value: Entity | undefined,
categories: Category[],
onSubmit: (data: z.infer<typeof entityFormSchema>) => Promise<ActionResponse>
className?: string
}) {
@ -29,6 +31,7 @@ export default function EntityForm({value, onSubmit, className}: {
id: value?.id ?? undefined,
name: value?.name ?? '',
type: value?.type ?? EntityType.Entity,
defaultCategoryId: value?.defaultCategoryId ?? undefined,
},
});
@ -40,6 +43,13 @@ export default function EntityForm({value, onSubmit, className}: {
}
};
const categoriesMapped = categories?.map((category) => {
return {
label: category.name,
value: category.id,
};
}) ?? [];
return (
<Form {...form}>
<form autoComplete="off" onSubmit={form.handleSubmit(handleSubmit)}>
@ -94,6 +104,22 @@ export default function EntityForm({value, onSubmit, className}: {
</FormItem>
)}
/>
<FormField
control={form.control}
name="defaultCategoryId"
render={({field}) => (
<FormItem>
<FormLabel>Category</FormLabel>
<FormControl>
<AutoCompleteInput
placeholder="Select category"
items={categoriesMapped}
{...field} />
</FormControl>
<FormMessage/>
</FormItem>
)}
/>
</div>
<Button type="submit" className="w-full">{value?.id ? 'Update Entity' : 'Create Entity'}</Button>
</form>

View file

@ -9,6 +9,7 @@ export default async function entityCreateUpdate({
id,
name,
type,
defaultCategoryId,
}: z.infer<typeof entityFormSchema>): Promise<ActionResponse> {
'use server';
@ -32,6 +33,7 @@ export default async function entityCreateUpdate({
data: {
name: name,
type: type,
defaultCategoryId: defaultCategoryId,
},
},
);
@ -47,6 +49,7 @@ export default async function entityCreateUpdate({
userId: user.id,
name: name,
type: type,
defaultCategoryId: defaultCategoryId,
},
});

View file

@ -5,4 +5,5 @@ export const entityFormSchema = z.object({
id: z.number().positive().optional(),
name: z.string().min(1).max(32),
type: z.nativeEnum(EntityType),
defaultCategoryId: z.number().positive().optional(),
});