Untitled
unknown
plain_text
2 years ago
3.5 kB
14
Indexable
/**
* Callback function type for handling success.
*
* @callback RefetchCallback
* @returns {void} This callback does not return anything.
*/
/**
* @typedef {Object} DeleteInitiativeHook
* @property {function(value: InitiativeProps): void} handleOpenModal - Function to handle opening the confirmation modal.
* @property {Node} DeleteModals - React functional component for displaying a success modal after initiative deletion.
*/
/**
* Custom hook for managing the deletion of initiatives.
*
* @param {RefetchCallback} refetch - Refetch callback function.
* @returns {Object} An object containing functions and components related to initiative deletion.
*/
const useDeleteInitiative = (
refetch: RefetchCallback
): DeleteInitiativeHook => {
const {
currentState: confirmationModalState,
openModal: openConfirmationModal,
closeModal: closeConfirmationModal,
} = useModalState();
const {
currentState: successModalState,
openModal: openSuccessModal,
closeModal: closeSuccessModal,
} = useModalState();
const {
currentState: errorModalState,
openModal: openErrorModal,
closeModal: closeErrorModal,
} = useModalState();
const [selectedInitiativeId, setSelectedInitiativeId] = useState<string | void>();
const [errorMessage, setErrorMessage] = useState();
/**
* Callback function invoked when a GraphQL operation is successfully completed.
*
* @callback OnCompletedCallback
* @returns {void} This function does not return anything.
*/
const onCompleted = () => {
closeConfirmationModal();
openSuccessModal();
refetch();
};
/**
* Function to handle error events.
* @param {any} data - The data associated with the error event.
* @returns {void}
*/
const onError = (data: any) => {
setErrorMessage(data.message);
closeConfirmationModal();
openErrorModal();
};
const [deleteInitiative, { loading: deleteInitiativeLoading }] = useMutation(
config.gql.DeleteInitiativeMutation,
{ onCompleted, onError }
);
/**
* handleOpenModal - function to handle opening confirmation modal
*
* @param {string} value - value of clicked data row
* @return {void}
*/
const handleOpenModal = (value: InitiativeProps) => {
openConfirmationModal();
setSelectedInitiativeId(value.id);
};
/**
* DeleteModal component for displaying a confirmation, success and error modal for initiative deletion.
*
* @function
* @name DeleteModal
* @returns {React$Node} The React functional component for the confirmation, success and error modal.
*/
const DeleteModals = (): Node => (
<>
<ConfirmationModal
message="Are you sure want to delete?"
open={confirmationModalState}
onCancel={closeConfirmationModal}
buttonLoading={deleteInitiativeLoading}
onYes={() =>
deleteInitiative({
variables: { id: selectedInitiativeId },
notifyOnNetworkStatusChange: true,
})
}
/>
<SuccessModal
open={successModalState}
onClose={closeSuccessModal}
message="Successfully deleted an initiative"
/>
<ErrorModal
message={errorMessage}
open={errorModalState}
onClose={() => {
closeErrorModal();
setErrorMessage(undefined);
}}
/>
</>
);
return { handleOpenModal, DeleteModals };
};
export default useDeleteInitiative;Editor is loading...
Leave a Comment