I actually did this as my first refactoring
I created function that accepts,
- state
- setState function
- relevant props from the class
But this became nightmare, as I had to wire every function invocation , accordingly (mostly for the third point)
but also I had to write extra function classConnector, but all this was not sweetCode
interface ConnectProps {
state?: any;
setState?: any;
payload?: any;
}
/** it is important to bind the execution context of
class as this to this file
this is hof, which helps with wiring function invocation with custom payload
*/
export function classConnector(this: any, func: any, payload?: any) {
return func({
state: this.state,
setState: this.setState,
payload,
});
}
export const onSubmitRFIRequest = ({
state,
setState,
payload,
}: ConnectProps) => {
const { raiseComplianceRFIRequest } = payload;
setState({
spinner: true,
action: StatusEnums.complianceStatus.RFI_RESPONSE_RAISED,
});
raiseComplianceRFIRequest(state.rfiRequestsTobeSent)
.then((res: any) => {
setState({ spinner: false });
if (res.type === 'ERROR') {
setState({
showNotificationPopUp: true,
notificationType: 'error',
});
} else {
setState({
showNotificationPopUp: true,
notificationType: 'success',
});
}
})
.catch(() => {
setState({ spinner: false });
setState({
showNotificationPopUp: true,
notificationType: 'error',
});
});
};
// and the extra prop drilling like this
<Component
onChangeRfiComment={onChangeRfiComment}
onChangeDocumentType={onChangeDocumentType}
onConfirmPopup={classConnector.call(
ComplianceInProcess,
onConfirmPopup
)}
resetRfiRequest={classConnector.call(
ComplianceInProcess,
resetRfiRequest
)}
openAccordion={classConnector.call(
ComplianceInProcess,
openAccordion
)}
onRaiseRFI={classConnector.call(
ComplianceInProcess,
onRaiseRFI
)}
handleDownloadFile={classConnector.call(
ComplianceInProcess,
handleDownloadFile
)} />