import { Button, FormControlLabel, FormGroup, FormHelperText, InputAdornment, Stack, Switch, TextField } from "@mui/material"; import Grid from '@mui/material/Grid'; import { DatePicker, LocalizationProvider } from "@mui/x-date-pickers"; import { AdapterLuxon } from "@mui/x-date-pickers/AdapterLuxon"; import { ENERGY_FACTOR } from "@/components/Nutrition/helpers/nutritionalValues"; import { NutritionalPlan } from "@/components/Nutrition/models/nutritionalPlan"; import { useAddNutritionalPlanQuery, useEditNutritionalPlanQuery } from "@/components/Nutrition/queries"; import { Form, Formik } from "formik"; import i18n from "@/i18n"; import { DateTime } from "luxon"; import React, { useState } from 'react'; import { useTranslation } from "react-i18next"; import { dateToYYYYMMDD, yyyymmddToDate } from "@/core/lib/date"; import * as yup from 'yup'; interface PlanFormProps { plan?: NutritionalPlan, closeFn?: () => void, } export const PlanForm = ({ plan, closeFn }: PlanFormProps) => { const [t] = useTranslation(); const addPlanQuery = useAddNutritionalPlanQuery(); const editPlanQuery = useEditNutritionalPlanQuery(plan?.id ?? ''); const [useGoals, setUseGoals] = useState(plan?.hasAnyGoals); const [startDateValue, setStartDateValue] = useState(plan ? DateTime.fromJSDate(plan.start) : DateTime.now); const [endDateValue, setEndDateValue] = useState(plan && plan?.end !== null ? DateTime.fromJSDate(plan!.end) : null); const validationSchema = yup.object({ description: yup .string() .required() .max(25, t('forms.maxLength', { chars: '25' })) .min(3, t('forms.minLength', { chars: '3' })), onlyLogging: yup .boolean(), goalEnergy: yup .number() .notRequired() .positive() .max(6000, t('forms.maxValue', { value: '6000kcal' })), goalProtein: yup .number() .notRequired() .positive() .max(500, t('forms.maxValue', { value: '500' })), goalCarbohydrates: yup .number() .notRequired() .positive() // TODO: allow 0 but not negative .max(750, t('forms.maxValue', { value: '750' })), goalFiber: yup .number() .notRequired() .positive() .max(500, t('forms.maxValue', { value: '500' })), goalFat: yup .number() .notRequired() .positive() .max(500, t('forms.maxValue', { value: '500' })), start: yup .date() .required(), end: yup .date() .nullable() .min( yup.ref('start'), t('forms.endBeforeStart') ) }); return ( ( { values.goalEnergy = values.goalEnergy ? values.goalEnergy : null; values.goalProtein = values.goalProtein ? values.goalProtein : null; values.goalCarbohydrates = values.goalCarbohydrates ? values.goalCarbohydrates : null; values.goalFiber = values.goalFiber ? values.goalFiber : null; values.goalFat = values.goalFat ? values.goalFat : null; if (!useGoals) { values.goalEnergy = null; values.goalProtein = null; values.goalCarbohydrates = null; values.goalFiber = null; values.goalFat = null; } const newPlan = new NutritionalPlan({ // the values are YYYY-MM-DD strings, parse them as local dates: // new Date() would interpret them as UTC midnight and shift the // day in timezones behind UTC start: yyyymmddToDate(values.start), end: values.end ? yyyymmddToDate(values.end) : null, description: values.description, onlyLogging: values.onlyLogging, goalEnergy: values.goalEnergy, goalProtein: values.goalProtein, goalCarbohydrates: values.goalCarbohydrates, goalFiber: values.goalFiber, goalFat: values.goalFat, }); if (plan) { newPlan.id = plan.id!; editPlanQuery.mutate(newPlan); } else { addPlanQuery.mutate(newPlan); } // if closeFn is defined, close the modal (this form does not have to be displayed in one) if (closeFn) { closeFn(); } }} > {formik => (
{ if (newValue) { formik.setFieldValue('start', dateToYYYYMMDD(newValue.toJSDate())); } setStartDateValue(newValue); }} /> { if (newValue) { formik.setFieldValue('end', dateToYYYYMMDD(newValue.toJSDate())); } setEndDateValue(newValue); }} /> } /> {/*TODO: implement the options like in the mobile app */} {/**/} {/* Goal Setting*/} {/* {*/} {/* }}*/} {/* >*/} {/* Based on my meals*/} {/* Set basic macros*/} {/* Set advanced macros*/} {/* */} {/**/} setUseGoals(!useGoals)} />} /> {t('nutrition.useGoalsHelpTextLong')} {useGoals && <> {t('nutrition.kcal')} }, htmlInput: { inputMode: 'decimal' } }} /> {formik.values.goalProtein !== null && formik.values.goalProtein !== undefined ? t('nutrition.valueEnergyKcal', { value: formik.values.goalProtein * ENERGY_FACTOR.protein }) : ''} , endAdornment: {t('nutrition.gramShort')} }, htmlInput: { inputMode: 'decimal' } }} /> {formik.values.goalCarbohydrates !== null && formik.values.goalCarbohydrates !== undefined ? t('nutrition.valueEnergyKcal', { value: formik.values.goalCarbohydrates * ENERGY_FACTOR.carbohydrates }) : ''} , endAdornment: {t('nutrition.gramShort')} }, htmlInput: { inputMode: 'decimal' } }} /> {formik.values.goalFat !== null && formik.values.goalFat !== undefined ? t('nutrition.valueEnergyKcal', { value: formik.values.goalFat * ENERGY_FACTOR.fat }) : ''} , endAdornment: {t('nutrition.gramShort')} }, htmlInput: { inputMode: 'decimal' } }} /> {t('nutrition.valueEnergyKcal', { value: 0 })} , endAdornment: {t('nutrition.gramShort')} }, htmlInput: { inputMode: 'decimal' } }} /> }
)}
) ); };