import { Button, Stack, TextField } from "@mui/material"; import { LocalizationProvider, TimePicker } from "@mui/x-date-pickers"; import { AdapterLuxon } from "@mui/x-date-pickers/AdapterLuxon"; import { Meal } from "@/components/Nutrition/models/meal"; import { useAddMealQuery, useEditMealQuery } from "@/components/Nutrition/queries"; import { Form, Formik } from "formik"; import { DateTime } from "luxon"; import React from 'react'; import { useTranslation } from "react-i18next"; import * as yup from "yup"; interface MealFormProps { planId: string, meal?: Meal, closeFn?: () => void, } export const MealForm = ({ meal, planId, closeFn }: MealFormProps) => { const [t, i18n] = useTranslation(); const addMealQuery = useAddMealQuery(planId); const editMealQuery = useEditMealQuery(planId); const validationSchema = yup.object({ name: yup .string() .required() .max(25, t('forms.maxLength', { chars: '25' })) .min(3, t('forms.minLength', { chars: '3' })), time: yup .date() .required() }); return ( { if (!(values.time instanceof Date)) { // @ts-ignore - The result from the datepicker is a Luxon DateTime object, not a JS DateTime values.time = values.time.toJSDate(); } if (meal) { // Edit const newMeal = Meal.clone(meal, { name: values.name, time: values.time }); editMealQuery.mutate(newMeal); } else { // Add addMealQuery.mutate(new Meal({ planId: planId, name: values.name, time: values.time, })); } if (closeFn) { closeFn(); } }} > {formik => (
formik.setFieldValue('time', newValue ? newValue.toJSDate() : null)} /> {closeFn !== undefined && }
)}
); };