o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
refactor this code: [code]
- User
refactor this code:
js - Assistant
refactor this code:
import { FormProvider, useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import PropTypes from 'prop-types';
import { t } from 'i18next';
import ComponentDisabler from '../ComponentDisabler';
import { Button } from 'antd';
import modalConfirm from 'src/utils/modalConfirm';
import { useSelector } from 'react-redux';
import { useEffect } from 'react';
const FormButtons = ({
additionalButtons,
isCancelHidden,
onCancel,
submitDisabled,
isSubmitting,
}) => {
return (
<div
data-testid="buttons-wrapper"
className={`flex justify-end ${
selectedLang === 'ar' ? 'gap-4 ' : ''
}px-3`}
>
{additionalButtons}
{!isCancelHidden && (
<Button
danger
className="mr-5"
onClick={() => {
modalConfirm(t('confirmationDialogue.cancelMessage'), onCancel);
}}
>
{t('submit.cancel')}
</Button>
)}
<ComponentDisabler
componentRender={() => (
<Button
type="primary"
ghost
disabled={submitDisabled || isSubmitting}
htmlType="submit"
className="border-green-600 text-green-600 hover:border-green-500 hover:text-green-500 disabled:opacity-30"
>
{t('submit.submit')}
</Button>
)}
/>
</div>
);
};
const Form = ({
children,
onSubmit,
submitDisabled,
formDisabled,
isUpdateForm,
defaultValues,
validationSchema,
onCancel,
buttonsOnTop,
additionalButtons,
isCancelHidden,
watchForDefaultValuesChanges,
onDefaultValuesChanges,
isSubmitting,
}) => {
const selectedLang = useSelector(
(state) => state?.selectedLanguage?.selectedLanguage,
);
const formMethods = useForm({
defaultValues,
resolver: yupResolver(validationSchema),
});
const { handleSubmit } = formMethods;
const submit = (e) => {
handleSubmit(onSubmit)(e);
e.stopPropagation();
};
useEffect(() => {
if (watchForDefaultValuesChanges) {
onDefaultValuesChanges?.({
valueSetter: formMethods.setValue,
});
}
}, [watchForDefaultValuesChanges]);
return (
<form onSubmit={submit} className="p-5">
<FormProvider {...{ ...formMethods, formDisabled, isUpdateForm }}>
{buttonsOnTop ? null : children}
<FormButtons
additionalButtons={additionalButtons}
isCancelHidden={isCancelHidden}
onCancel={onCancel}
submitDisabled={submitDisabled}
isSubmitting={isSubmitting}
/>
{buttonsOnTop ? children : null}
</FormProvider>
</form>
);
};
Form.propTypes = {
children: PropTypes.any,
formDisabled: PropTypes.bool,
submitDisabled: PropTypes.bool,
onSubmit: PropTypes.func,
onCancel: PropTypes.func,
defaultValues: PropTypes.object,
validationSchema: PropTypes.object,
watchForDefaultValuesChanges: PropTypes.bool,
onDefaultValuesChanges: PropTypes.func,
isUpdateForm: PropTypes.bool,
isSubmitting: PropTypes.bool,
};
Certainly! Here's a refactored version of your code with several improvements for better readability, maintainability, and performance:
selectedLang Access: Passed selectedLang as a prop to FormButtons to ensure it's available within the component.React.memo for FormButtons to prevent unnecessary re-renders.PropTypes definitions for better type safety.Here is the refactored code:
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { useForm, FormProvider } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import { useSelector } from 'react-redux';
import { Button } from 'antd';
import { t } from 'i18next';
import ComponentDisabler from '../ComponentDisabler';
import modalConfirm from 'src/utils/modalConfirm';
/**
* FormButtons Component
*/
const FormButtons = React.memo(
({
additionalButtons,
isCancelHidden,
onCancel,
submitDisabled,
isSubmitting,
selectedLang,
}) => {
const handleCancel = () => {
modalConfirm(t('confirmationDialogue.cancelMessage'), onCancel);
};
return (
<div
data-testid="buttons-wrapper"
className={`flex justify-end ${
selectedLang === 'ar' ? 'gap-4 ' : ''
}px-3`}
>
{additionalButtons}
{!isCancelHidden && (
<Button danger className="mr-5" onClick={handleCancel}>
{t('submit.cancel')}
</Button>
)}
<ComponentDisabler
componentRender={() => (
<Button
type="primary"
ghost
disabled={submitDisabled || isSubmitting}
htmlType="submit"
className="border-green-600 text-green-600 hover:border-green-500 hover:text-green-500 disabled:opacity-30"
>
{t('submit.submit')}
</Button>
)}
/>
</div>
);
}
);
FormButtons.propTypes = {
additionalButtons: PropTypes.node,
isCancelHidden: PropTypes.bool,
onCancel: PropTypes.func,
submitDisabled: PropTypes.bool,
isSubmitting: PropTypes.bool,
selectedLang: PropTypes.string.isRequired,
};
/**
* Main Form Component
*/
const Form = ({
children,
onSubmit,
submitDisabled = false,
formDisabled = false,
isUpdateForm = false,
defaultValues = {},
validationSchema,
onCancel,
buttonsOnTop = false,
additionalButtons,
isCancelHidden = false,
watchForDefaultValuesChanges = false,
onDefaultValuesChanges,
isSubmitting = false,
}) => {
const selectedLang = useSelector(
(state) => state?.selectedLanguage?.selectedLanguage
);
const formMethods = useForm({
defaultValues,
resolver: yupResolver(validationSchema),
});
const { handleSubmit, setValue } = formMethods;
const handleFormSubmit = (data, event) => {
if (event) event.stopPropagation();
handleSubmit(onSubmit)(data, event);
};
useEffect(() => {
if (watchForDefaultValuesChanges && typeof onDefaultValuesChanges === 'function') {
onDefaultValuesChanges({ setValue });
}
}, [watchForDefaultValuesChanges, onDefaultValuesChanges, setValue]);
return (
<form onSubmit={handleFormSubmit} className="p-5">
<FormProvider value={{ ...formMethods, formDisabled, isUpdateForm }}>
{!buttonsOnTop && children}
<FormButtons
additionalButtons={additionalButtons}
isCancelHidden={isCancelHidden}
onCancel={onCancel}
submitDisabled={submitDisabled}
isSubmitting={isSubmitting}
selectedLang={selectedLang}
/>
{buttonsOnTop && children}
</FormProvider>
</form>
);
};
Form.propTypes = {
children: PropTypes.node,
onSubmit: PropTypes.func.isRequired,
submitDisabled: PropTypes.bool,
formDisabled: PropTypes.bool,
isUpdateForm: PropTypes.bool,
defaultValues: PropTypes.object,
validationSchema: PropTypes.object.isRequired,
onCancel: PropTypes.func,
buttonsOnTop: PropTypes.bool,
additionalButtons: PropTypes.node,
isCancelHidden: PropTypes.bool,
watchForDefaultValuesChanges: PropTypes.bool,
onDefaultValuesChanges: PropTypes.func,
isSubmitting: PropTypes.bool,
};
export default Form;
selectedLang Prop in FormButtons:
selectedLang was used inside FormButtons but wasn't passed as a prop, leading to potential undefined errors.selectedLang as a required prop to FormButtons to ensure it has access to the language state.Memoization with React.memo:
FormButtons is wrapped with React.memo to prevent unnecessary re-renders when its props haven't changed.Destructured Form Methods:
handleSubmit and setValue directly from formMethods for cleaner code.Event Handling in handleFormSubmit:
Enhanced PropTypes:
PropTypes from any and object to more specific types like node for additionalButtons and made selectedLang required.Default Prop Values:
Cleaned Up useEffect:
onDefaultValuesChanges is a function before invoking it.Class Name Concatenation:
gap-4 class based on the selected language.Separate handleCancel Function:
validationSchema provided to yupResolver is correctly defined to prevent runtime errors.Form and FormButtons components, especially the conditional rendering logic.This refactored code should be more maintainable, easier to read, and free from the issue related to the selectedLang variable. Feel free to adjust any styles or logic to better fit your specific use case!
Not yet included in a published release.