-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add endpoint and button to delete contexts (#511)
* add endpoint and button to delete contexts --------- Co-authored-by: Mailn Nifeli Snieske <[email protected]>
- Loading branch information
1 parent
81a4639
commit c61b200
Showing
6 changed files
with
181 additions
and
13 deletions.
There are no files selected for viewing
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
11 changes: 11 additions & 0 deletions
11
backend/src/main/resources/db/migration/V12__replace_fk_constraints_for_contexts.sql
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,11 @@ | ||
ALTER TABLE answers | ||
DROP CONSTRAINT fk_answers_contexts, | ||
ADD CONSTRAINT fk_answers_contexts | ||
FOREIGN KEY (context_id) REFERENCES contexts(id) | ||
ON DELETE CASCADE; | ||
|
||
ALTER TABLE comments | ||
DROP CONSTRAINT fk_comments_contexts, | ||
ADD CONSTRAINT fk_comments_contexts | ||
FOREIGN KEY (context_id) REFERENCES contexts(id) | ||
ON DELETE CASCADE; |
66 changes: 66 additions & 0 deletions
66
frontend/beCompliant/src/components/DeleteContextModal.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,66 @@ | ||
import { | ||
Button, | ||
HStack, | ||
Modal, | ||
ModalBody, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader, | ||
ModalOverlay, | ||
Stack, | ||
Text, | ||
} from '@kvib/react'; | ||
import { useDeleteContext } from '../hooks/useDeleteContext'; | ||
|
||
type Props = { | ||
onOpen: () => void; | ||
onClose: () => void; | ||
isOpen: boolean; | ||
contextId: string; | ||
teamId: string; | ||
}; | ||
export function DeleteContextModal({ | ||
onClose, | ||
isOpen, | ||
contextId, | ||
teamId, | ||
}: Props) { | ||
const { mutate: deleteContext, isPending: isLoading } = useDeleteContext( | ||
contextId, | ||
teamId, | ||
onClose | ||
); | ||
|
||
return ( | ||
<Modal onClose={onClose} isOpen={isOpen} isCentered> | ||
<ModalOverlay /> | ||
<ModalContent> | ||
<ModalHeader>Slett skjemautfylling</ModalHeader> | ||
<ModalBody> | ||
<Stack> | ||
<Text size="sm"> | ||
Er du sikker på at du vil slette skjemautfyllingen? | ||
</Text> | ||
</Stack> | ||
</ModalBody> | ||
<ModalFooter> | ||
<HStack justifyContent="end"> | ||
<Button variant="tertiary" colorScheme="blue" onClick={onClose}> | ||
Avbryt | ||
</Button> | ||
<Button | ||
aria-label="Slett utfylling" | ||
variant="primary" | ||
colorScheme="red" | ||
leftIcon="delete" | ||
onClick={() => deleteContext()} | ||
isLoading={isLoading} | ||
> | ||
Slett skjemautfylling | ||
</Button> | ||
</HStack> | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
); | ||
} |
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,43 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
import { apiConfig } from '../api/apiConfig'; | ||
import { useToast } from '@kvib/react'; | ||
import { axiosFetch } from '../api/Fetch'; | ||
|
||
export function useDeleteContext( | ||
contextId: string, | ||
teamId: string, | ||
onSuccess: () => void | ||
) { | ||
const URL = apiConfig.contexts.byId.url(contextId); | ||
const queryClient = useQueryClient(); | ||
const toast = useToast(); | ||
|
||
return useMutation({ | ||
mutationKey: apiConfig.contexts.byId.queryKey(contextId), | ||
mutationFn: () => { | ||
return axiosFetch({ | ||
url: URL, | ||
method: 'DELETE', | ||
}); | ||
}, | ||
onSuccess: async () => { | ||
await queryClient.invalidateQueries({ | ||
queryKey: apiConfig.contexts.forTeam.queryKey(teamId), | ||
}); | ||
onSuccess(); | ||
}, | ||
onError: () => { | ||
const toastId = 'delete-context-error'; | ||
if (!toast.isActive(toastId)) { | ||
toast({ | ||
id: toastId, | ||
title: 'Å nei!', | ||
description: 'Det har skjedd en feil. Prøv på nytt', | ||
status: 'error', | ||
duration: 5000, | ||
isClosable: true, | ||
}); | ||
} | ||
}, | ||
}); | ||
} |
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