-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/GODIET-60-energetic-calculate-create-page-to-ca…
…lculate-energi' into develop
- Loading branch information
Showing
31 changed files
with
1,126 additions
and
1 deletion.
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
83 changes: 83 additions & 0 deletions
83
.../CreateEnergyCalculation/components/EnergyCalculationForm/energy-calculation-form.hook.ts
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,83 @@ | ||
import React from 'react'; | ||
|
||
import { usePatient } from '@godiet-hooks/patient'; | ||
|
||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { differenceInCalendarYears } from 'date-fns'; | ||
import { useForm, useWatch } from 'react-hook-form'; | ||
|
||
import { EnergyCalculationFormProps } from './energy-calculation-form'; | ||
import { | ||
CreateEnergyCalculationSchema, | ||
defaultFormulaToCalculation, | ||
defaultInitialValues, | ||
} from './energy-calculation-form.schema'; | ||
import { | ||
FormulaKey, | ||
TCreateEnergyCalculationDTO, | ||
} from './energy-calculation-form.types'; | ||
|
||
export function useEnergyCalculationFormHook( | ||
props: EnergyCalculationFormProps | ||
) { | ||
const { initialValues, onSubmit } = props; | ||
|
||
const { patient } = usePatient(); | ||
|
||
const methods = useForm<TCreateEnergyCalculationDTO>({ | ||
resolver: zodResolver(CreateEnergyCalculationSchema), | ||
defaultValues: initialValues | ||
? CreateEnergyCalculationSchema.parse(initialValues) | ||
: defaultInitialValues, | ||
}); | ||
|
||
const { | ||
register, | ||
handleSubmit: hookFormSubmit, | ||
formState: { errors }, | ||
control, | ||
} = methods; | ||
|
||
const watch = useWatch({ | ||
control, | ||
}); | ||
|
||
const energyCalculation = React.useMemo(() => { | ||
if (!patient) return 0; | ||
|
||
const { gender, birthDate } = patient; | ||
|
||
const { activityLevel, formula, freeMass, height, weight } = watch; | ||
|
||
if ( | ||
!formula || | ||
!activityLevel || | ||
!Number(height) || | ||
!Number(freeMass) || | ||
!Number(weight) | ||
) | ||
return 0; | ||
|
||
const fnFormula = defaultFormulaToCalculation[formula as FormulaKey]; | ||
|
||
const age = differenceInCalendarYears(new Date(), birthDate); | ||
|
||
const result = fnFormula({ | ||
activityLevel: Number(activityLevel) || 0, | ||
freeMass: Number(freeMass) || 0, | ||
height: Number(height) || 0, | ||
weight: Number(weight) || 0, | ||
sex: gender, | ||
age, | ||
}); | ||
|
||
return Math.ceil(result); | ||
}, [patient, watch]); | ||
|
||
const handleSubmit = hookFormSubmit(async (data) => { | ||
if (energyCalculation === 0) return; | ||
await onSubmit({ ...data, energyCalculation }); | ||
}); | ||
|
||
return { errors, control, energyCalculation, handleSubmit, register }; | ||
} |
193 changes: 193 additions & 0 deletions
193
...reateEnergyCalculation/components/EnergyCalculationForm/energy-calculation-form.schema.ts
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,193 @@ | ||
import * as z from 'zod'; | ||
|
||
import { | ||
DefaultFormulaToCalculation, | ||
FormulaKey, | ||
TCreateEnergyCalculationDTO, | ||
} from './energy-calculation-form.types'; | ||
|
||
export const CreateEnergyCalculationSchema = z.object({ | ||
title: z.string().min(1, 'O título do cálculo é obrigatório'), | ||
weight: z | ||
.string() | ||
.refine((value) => !isNaN(Number(value)), { | ||
message: 'O peso deve ser um número válido', | ||
}) | ||
.refine((value) => Number(value) > 0, { | ||
message: 'O peso deve ser maior que 0', | ||
}) | ||
.or(z.undefined()) | ||
.or(z.number().pipe(z.coerce.string())), | ||
height: z | ||
.string() | ||
.refine((value) => !isNaN(Number(value)), { | ||
message: 'A altura deve ser um número válido', | ||
}) | ||
.refine((value) => Number(value) > 0, { | ||
message: 'A altura deve ser maior que 0', | ||
}) | ||
.or(z.undefined()) | ||
.or(z.number().pipe(z.coerce.string())), | ||
freeMass: z | ||
.string() | ||
.refine((value) => !isNaN(Number(value)), { | ||
message: 'A altura deve ser um número válido', | ||
}) | ||
.refine((value) => Number(value) > 0, { | ||
message: 'A altura deve ser maior que 0', | ||
}) | ||
.or(z.undefined()) | ||
.or(z.number().pipe(z.coerce.string())), | ||
activityLevel: z | ||
.string() | ||
.refine((value) => !isNaN(Number(value)), { | ||
message: 'O fator de atividade deve ser um número válido', | ||
}) | ||
.refine((value) => Number(value) >= 1, { | ||
message: 'A altura deve ser maior que 0', | ||
}) | ||
.refine((value) => Number(value) < 2, { | ||
message: 'A altura deve ser menor que 2', | ||
}) | ||
.or(z.undefined()) | ||
.or(z.number().min(1).pipe(z.coerce.string())), | ||
formula: z.string().or(z.undefined()), | ||
}); | ||
|
||
export const defaultInitialValues: TCreateEnergyCalculationDTO = { | ||
title: '', | ||
weight: undefined, | ||
height: undefined, | ||
freeMass: undefined, | ||
activityLevel: undefined, | ||
formula: undefined, | ||
}; | ||
|
||
type Options = { | ||
value: string; | ||
label: string; | ||
}; | ||
|
||
export const defaultOptionsToActivityLevel: Options[] = [ | ||
{ value: '1', label: '1 - Normal' }, | ||
{ value: '1.2', label: '1.2 - Sedentário' }, | ||
{ value: '1.375', label: '1.375 - Leve' }, | ||
{ value: '1.550', label: '1.550 - Moderada' }, | ||
{ value: '1.725', label: '1.725 - Intensa' }, | ||
{ value: '1.900', label: '1.900 - Muito intensa' }, | ||
]; | ||
|
||
export const defaultOptionsToFormula: { | ||
value: FormulaKey; | ||
label: string; | ||
}[] = [ | ||
{ value: 'harris-benedict-1984', label: 'Harris Benedict (1984)' }, // | ||
{ value: 'harris-benedict-1919', label: 'Harris Benedict (1919)' }, // | ||
{ value: 'fao-who-2004', label: 'FAO/WHO (2004)' }, // | ||
{ value: 'fao-who-infantil-2004', label: 'FAO/WHO - Infantil (2004)' }, // | ||
{ value: 'eer-iom-2005', label: 'EER/IOM (2005)' }, // | ||
{ value: 'eer-iom-infantil-2005', label: 'EER/IOM - Infantil(2005)' }, // | ||
{ value: 'katch-mcardle-1996', label: 'Katch-McArdle (1996)' }, // | ||
{ value: 'cunningham-1980', label: 'Cunningham (1980)' }, // | ||
{ value: 'mifflin-obesidade-1990', label: 'Mifflin - Obesidade (1990)' }, // | ||
{ value: 'mifflin-sobrepeso-1990', label: 'Mifflin - Sobrepeso (1990)' }, | ||
{ value: 'henry-rees-1991', label: 'Henry & Rees (1991)' }, // | ||
{ value: 'tinsley-por-peso-2018', label: 'Tinsley - Por peso (2018)' }, // | ||
{ value: 'tinsley-por-MLG-2018', label: 'Tinsley - Por MLG (2018)' }, // | ||
{ value: 'min-saude-gestante-2018', label: 'Min. saúde - gestante (2018)' }, | ||
{ value: 'schofield-infantil-1985', label: 'Schofield - Infantil (1985)' }, // | ||
]; | ||
|
||
export const defaultFormulaToCalculation: DefaultFormulaToCalculation = { | ||
'harris-benedict-1984': (data) => { | ||
if (data.sex === 'MASC') { | ||
return ( | ||
88.362 + 13.397 * data.weight + 4.799 * data.height - 5.677 * data.age | ||
); | ||
} | ||
return ( | ||
447.593 + 9.247 * data.weight + 3.098 * data.height - 4.33 * data.age | ||
); | ||
}, | ||
'harris-benedict-1919': (data) => { | ||
if (data.sex === 'MASC') { | ||
return 66.5 + 13.75 * data.weight + 5.003 * data.height - 6.8 * data.age; | ||
} | ||
|
||
return 655.1 + 9.563 * data.weight + 1.7 * data.height - 4.676 * data.age; | ||
}, | ||
'fao-who-2004': (data) => { | ||
if (data.sex === 'MASC') { | ||
return 15.057 * data.weight + 692.2; | ||
} | ||
|
||
return 14.818 * data.weight + 486.6; | ||
}, | ||
'fao-who-infantil-2004': (data) => { | ||
return 152 + 92 * data.weight; | ||
}, | ||
'eer-iom-2005': (data) => { | ||
if (data.sex === 'MASC') { | ||
662 - | ||
9.53 * data.age + | ||
data.activityLevel * | ||
(15.91 * data.weight + 539.6 * (data.height / 100)); | ||
} | ||
|
||
return ( | ||
354 - | ||
6.91 * data.age + | ||
data.activityLevel * (9.36 * data.weight + 726 * (data.height / 100)) | ||
); | ||
}, | ||
'eer-iom-infantil-2005': (data) => { | ||
if (data.sex === 'MASC') { | ||
return 17.686 * data.weight + 658.2; | ||
} | ||
|
||
return 13.384 * data.weight + 692.6; | ||
}, | ||
'katch-mcardle-1996': (data) => { | ||
return 370 + 9.82 * data.freeMass; | ||
}, | ||
'cunningham-1980': (data) => { | ||
return 500 + 22 * data.freeMass; | ||
}, | ||
'mifflin-obesidade-1990': (data) => { | ||
if (data.sex === 'MASC') { | ||
return 10 * data.weight + 6.25 * data.height - 5 * data.age + 5; | ||
} | ||
|
||
return 10 * data.weight + 6.25 * data.height - 5 * data.age - 161; | ||
}, | ||
'mifflin-sobrepeso-1990': (data) => { | ||
if (data.sex === 'MASC') { | ||
return 10 * data.weight + 6.25 * data.height - 5 * data.age + 5; | ||
} | ||
|
||
return 10 * data.weight + 6.25 * data.height - 5 * data.age - 161; | ||
}, | ||
'henry-rees-1991': (data) => { | ||
if (data.sex === 'MASC') { | ||
return (0.056 * data.weight + 2.8) * 239; | ||
} | ||
|
||
return (0.048 * data.weight + 2.562) * 239; | ||
}, | ||
'tinsley-por-peso-2018': (data) => { | ||
return 24.8 * data.weight + 10; | ||
}, | ||
'tinsley-por-MLG-2018': (data) => { | ||
return 25.9 * data.freeMass + 284; | ||
}, | ||
'min-saude-gestante-2018': (data) => { | ||
return 14.7 * data.weight + 496 + 300; | ||
}, | ||
'schofield-infantil-1985': (data) => { | ||
if (data.sex === 'MASC') { | ||
return 59.48 * data.weight - 30.33; | ||
} | ||
|
||
return 58.29 * data.weight - 31.05; | ||
}, | ||
}; |
29 changes: 29 additions & 0 deletions
29
...CreateEnergyCalculation/components/EnergyCalculationForm/energy-calculation-form.spec.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,29 @@ | ||
import { ReturnRenderType } from '@testing-react'; | ||
import { clearAllMocks } from '@testing-suit'; | ||
|
||
describe('EnergyCalculationForm', () => { | ||
beforeEach(() => { | ||
clearAllMocks(); | ||
}); | ||
|
||
describe('render', () => { | ||
let rendered: ReturnRenderType; | ||
|
||
beforeEach(() => { | ||
clearAllMocks(); | ||
}); | ||
|
||
afterEach(() => { | ||
rendered?.unmount(); | ||
}); | ||
|
||
it('Should render the view with default props', () => { | ||
// Arrange | ||
|
||
// Act | ||
|
||
// Assert | ||
expect(true).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.