Created
March 23, 2022 12:49
-
-
Save borislavstoychev/31a487a7dc383ec0f886ba2883601536 to your computer and use it in GitHub Desktop.
dellModal
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
import React from 'react' | |
import { useMutation, useQueryClient } from 'react-query' | |
import { observer } from 'mobx-react' | |
import { AxiosError, AxiosResponse } from 'axios' | |
import { useTranslation } from 'next-i18next' | |
import { ApiErrors } from 'service/apiErrors' | |
import { ModalStore } from 'stores/dashboard/ModalStore' | |
import { AlertStore } from 'stores/AlertStore' | |
import DeleteDialog from 'components/admin/DeleteDialog' | |
type Props = { | |
mutationFn: (id: string) => Promise<AxiosResponse> | |
translate: string | |
endpoint: string | |
} | |
export default observer(function DeleteModal({ mutationFn, translate, endpoint }: Props) { | |
const { hideDelete, selectedRecord } = ModalStore | |
const { t } = useTranslation(translate) | |
const queryClient = useQueryClient() | |
const deleteMutation = useMutation<AxiosResponse, AxiosError<ApiErrors>, string>({ | |
mutationFn, | |
onError: () => AlertStore.show(t('alerts.error'), 'error'), | |
onSuccess: () => { | |
hideDelete() | |
AlertStore.show(t('alerts.delete'), 'warning') | |
queryClient.invalidateQueries(endpoint) | |
}, | |
}) | |
function deleteHandler() { | |
deleteMutation.mutate(selectedRecord.id) | |
} | |
return <DeleteDialog deleteHandler={deleteHandler} /> | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment