fix the difficulty section 'use client'; import { useForm, useFieldArray, Controller }…
- User
fix the difficulty section 'use client';
import { useForm, useFieldArray, Controller } from 'react-hook-form'; import { TextInput, Textarea, NumberInput, Switch, Button, MultiSelect, } from '@mantine/core'; import Link from 'next/link'; import { useInsertRecipe } from '@/hooks/useInsertRecipe'; import { useFetchAll } from '@/hooks/useFetchAll'; import { useRouter } from 'next/navigation'; import { uploadImages, insertRecipeCategories, } from '@/services/supabase_service'; import { Json } from '@/models/json'; import AuthenticatedPage from '@/components/authenticated_page/authenticated_page'; import { useState } from 'react';
interface Ingredient { name: string; amount: number; unit: string; }
interface DirectionStep { instruction: string; }
interface RecipeFormValues { user_id: string; name: string; description?: string; difficulty?: number; prep_time: number; cook_time?: number; servings?: number; shared?: boolean; ingredients: Ingredient[]; directions: DirectionStep[]; images: FileList | null; videos: null; categories: number[]; // Selected category IDs (for join table) }
function InsertRecipe() { const { register, handleSubmit, control, reset } = useForm({ defaultValues: { // Use a valid user_id from your auth.users table. user_id: process.env.NEXT_PUBLIC_TEST_USER_ID ?? '', name: '', description: '', difficulty: undefined, prep_time: 0, cook_time: 0, servings: 0, shared: false, ingredients: [{ name: '', amount: 0, unit: '' }], directions: [{ instruction: '' }], images: null, videos: null, categories: [], }, });
const { fields: ingredientFields, append: appendIngredient, remove: removeIngredient, } = useFieldArray({ control, name: 'ingredients', });
const { fields: directionFields, append: appendDirection, remove: removeDirection, } = useFieldArray({ control, name: 'directions', });
// Fetch categories using useFetchAll (table: 'categories') with pagination disabled. const { data: categoriesData, isLoading: categoriesLoading } = useFetchAll({ table: 'categories', usePagination: false, });
const categoryOptions = (categoriesData?.data as { id: number; name: string }[] | undefined)?.map( (cat) => ({ value: cat.id.toString(), label: cat.name, }), ) || [];
const { mutateAsync, isPending } = useInsertRecipe(); const router = useRouter();
const onSubmit = async (values: RecipeFormValues) => { // Process image uploads if files are selected. let imageUrls: string[] | null = null; if (values.images && values.images.length > 0) { const filesArray = Array.from(values.images); try { imageUrls = await uploadImages(filesArray); } catch (error) { alert('Image upload failed'); console.error(error); return; } }
code - Assistant
- User
what about the export functions on top qith enums
- Assistant