Skip to content

Commit

Permalink
refactor(use delete modal mutation): allow useMutation options and us…
Browse files Browse the repository at this point in the history
…e "onSuccess" option
  • Loading branch information
Mohammer5 committed Mar 19, 2024
1 parent f5a1070 commit af3a3d0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
19 changes: 7 additions & 12 deletions src/components/sectionList/listActions/DeleteAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ function ConfirmationDialog({
onDeleteSuccess: () => void
}) {
const schema = useSchemaFromHandle()
const deleteModelMutation = useDeleteModelMutation(schema)
const deleteModelMutation = useDeleteModelMutation(schema, {
onSuccess: () => {
showDeletionSuccess()
onDeleteSuccess()
},
})
const deleteModel = async () => {
await deleteModelMutation.mutateAsync({
id: modelId,
Expand All @@ -96,16 +101,6 @@ function ConfirmationDialog({
{ success: true }
)

const deleteAndClose = () =>
deleteModel()
.then(() => {
showDeletionSuccess()
onDeleteSuccess()
})
// We don't need to do anything on error except for catching it,
// we have all the information on the deleteModelMutation value
.catch(() => null)

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const errorReports = (deleteModelMutation.error as any)?.details?.response
?.errorReports
Expand Down Expand Up @@ -155,7 +150,7 @@ function ConfirmationDialog({
<Button
disabled={deleteModelMutation.isLoading}
destructive
onClick={deleteAndClose}
onClick={deleteModel}
>
{deleteModelMutation.isLoading && (
<span className={classes.deleteButtonLoadingIcon}>
Expand Down
3 changes: 2 additions & 1 deletion src/lib/models/useDeleteModelMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ type MutationFnArgs = {
messages?: string[]
}

export function useDeleteModelMutation(schema: Schema) {
export function useDeleteModelMutation(schema: Schema, options?: object) {
const engine = useDataEngine()

return useMutation({
...options,
mutationFn: (variables: MutationFnArgs) => {
return engine.mutate({
resource: schema.plural,
Expand Down

0 comments on commit af3a3d0

Please sign in to comment.