Fakultas Ilmu Komputer UI

Commit e237461a authored by wulanmantiri's avatar wulanmantiri

[REFACTOR] Fix infinite render for nutritionist comments

parent 81927ccb
Pipeline #84575 passed with stages
in 8 minutes and 36 seconds
......@@ -24,31 +24,25 @@ import {
dietReportSelectFields,
} from 'constants/weeklyReport';
interface ParamsDietReport {
id: number;
}
const DietReportForNutritionist: FC = () => {
const navigation = useNavigation();
const route = useRoute();
const userReport = route.params as UserReportResponse;
const [activeSlide, setActiveSlide] = useState(1);
const [commentId, setCommentId] = useState(0);
const { isLoading, data: commentResponse } = useApi(() =>
retrieveUserReportCommentByReportId(userReport.id),
);
const getInitialValues = () => {
let defaultValues = dietReportCommentInitialValues;
let defaultValues = { ...dietReportCommentInitialValues };
if (commentResponse && commentResponse.length > 0) {
const data = commentResponse[0];
Object.entries(data).map(([field, comment]) => {
const key = field as keyof NutritionistComment;
defaultValues[key] = comment;
});
setCommentId(data.id);
}
return defaultValues;
};
......@@ -62,18 +56,22 @@ const DietReportForNutritionist: FC = () => {
} = useForm({
initialValues: getInitialValues(),
validationSchema: generateValidationSchema(fieldValidations),
enableReinitialize: true,
onSubmit: async (values) => {
const payload: NutritionistCommentRequest = {
weekly_report: userReport.id,
...values,
};
const isUpdate = commentId > 0;
const response = isUpdate
const commentId =
commentResponse && commentResponse.length > 0
? commentResponse[0].id
: null;
const response = commentId
? await updateNutritionistCommentApi(payload, commentId)
: await createNutritionistCommentApi(payload);
if (response.success) {
isUpdate
commentId
? Toast.show({
type: 'success',
text1: 'Sukses mengubah komen',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment