-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* backend support * create react hook * Add delete button
- Loading branch information
1 parent
e1e1879
commit 718fe55
Showing
6 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
client/src/components/all-charges/documents/delete-document-button.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ActionIcon } from '@mantine/core'; | ||
import { useState } from 'react'; | ||
import { Trash } from 'tabler-icons-react'; | ||
|
||
import { useDeleteDocument } from '../../../hooks/use-delete-document'; | ||
import { ConfirmationModal } from '../../common'; | ||
|
||
interface Props { | ||
documentId: string; | ||
} | ||
|
||
export function DeleteDocumentButton({ documentId }: Props) { | ||
const [opened, setOpened] = useState(false); | ||
const { mutate } = useDeleteDocument(); | ||
|
||
function onDelete() { | ||
mutate({ | ||
documentId, | ||
}); | ||
} | ||
|
||
return ( | ||
<> | ||
<ConfirmationModal | ||
opened={opened} | ||
onClose={() => setOpened(false)} | ||
onConfirm={onDelete} | ||
title="Are you sure you want to delete this document?" | ||
/> | ||
<ActionIcon color="red" variant="hover" onClick={() => setOpened(true)}> | ||
<Trash size={20} /> | ||
</ActionIcon> | ||
</> | ||
); | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { gql } from 'graphql-tag'; | ||
|
||
import { | ||
DeleteDocumentMutation, | ||
DeleteDocumentMutationVariables, | ||
useDeleteDocumentMutation, | ||
} from '../__generated__/types'; | ||
|
||
gql` | ||
mutation DeleteDocument($documentId: ID!) { | ||
deleteDocument(documentId: $documentId) | ||
} | ||
`; | ||
|
||
export const useDeleteDocument = () => { | ||
// TODO: add authentication | ||
// TODO: add local data delete method after change | ||
|
||
const onError = async (e: unknown, { documentId }: DeleteDocumentMutationVariables) => { | ||
console.error(e); | ||
return new Error(`Error deleting document ID [${documentId}]: ${(e as Error)?.message}`); | ||
}; | ||
const onSuccess = async (data: DeleteDocumentMutation, { documentId }: DeleteDocumentMutationVariables) => { | ||
if (!data.deleteDocument) { | ||
throw new Error(`Error deleting document ID [${documentId}]`); | ||
} | ||
return data.deleteDocument; | ||
}; | ||
return useDeleteDocumentMutation({ | ||
onError, | ||
onSuccess, | ||
}); | ||
}; |
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
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
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