-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract confirm-delete into more general confirm-action (#1927)
* abstract confirm-delete into more general confirm-action * fix double error toast on instance delete
- Loading branch information
1 parent
695d367
commit 4bfadc0
Showing
7 changed files
with
73 additions
and
50 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
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
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,42 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* Copyright Oxide Computer Company | ||
*/ | ||
|
||
import { classed } from '@oxide/util' | ||
|
||
import { useConfirmAction } from './confirm-action' | ||
|
||
// confirmAction was originally abstracted from confirmDelete. this preserves | ||
// the existing confirmDelete API by constructing a confirmAction from it | ||
|
||
type DeleteConfig = { | ||
/** Must be `mutateAsync`, otherwise we can't catch the error generically */ | ||
doDelete: () => Promise<unknown> | ||
/** | ||
* Label identifying the resource. Could be a name or something more elaborate | ||
* "the Admin role for user Harry Styles". If a string, the modal will | ||
* automatically give it a highlighted style. Otherwise it will be rendered | ||
* directly. | ||
*/ | ||
label: React.ReactNode | ||
} | ||
|
||
export const HL = classed.span`text-sans-semi-md text-default` | ||
|
||
export const confirmDelete = | ||
({ doDelete, label }: DeleteConfig) => | ||
() => { | ||
const displayLabel = typeof label === 'string' ? <HL>{label}</HL> : label | ||
useConfirmAction.setState({ | ||
actionConfig: { | ||
doAction: doDelete, | ||
modalContent: <p>Are you sure you want to delete {displayLabel}?</p>, | ||
errorTitle: 'Could not delete resource', | ||
modalTitle: 'Confirm delete', | ||
}, | ||
}) | ||
} |