-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
suppress/unsuppress books on per-library basis
- Loading branch information
Showing
9 changed files
with
448 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import * as React from "react"; | ||
import { Button } from "library-simplified-reusable-components"; | ||
import { LinkData } from "../interfaces"; | ||
import ConfirmationModalWithOutcome, { | ||
SHOW_CONFIRMATION, | ||
SHOW_OUTCOME, | ||
ModalState, | ||
} from "./ConfirmationModalWithOutcome"; | ||
import ErrorMessage, { pdString } from "./ErrorMessage"; | ||
|
||
type BookDetailsEditorPerLibraryVisibilityProps = { | ||
link: LinkData; | ||
onConfirm: () => Promise<any>; | ||
onComplete: () => void; | ||
buttonContent: string; | ||
buttonTitle: string; | ||
className?: string; | ||
buttonDisabled: boolean; | ||
confirmationTitle?: string; | ||
confirmationBody?: React.ReactElement; | ||
confirmationButtonContent?: string; | ||
confirmationButtonTitle?: string; | ||
dismissOutcomeButtonContent?: string; | ||
dismissOutcomeButtonTitle?: string; | ||
defaultSuccessMessage?: React.ReactElement; | ||
defaultFailureMessage?: React.ReactElement; | ||
}; | ||
|
||
const BookDetailsEditorSuppression = ({ | ||
link, | ||
onConfirm, | ||
onComplete, | ||
buttonContent, | ||
buttonTitle = buttonContent, | ||
className = "", | ||
buttonDisabled, | ||
confirmationTitle = "Confirm", | ||
confirmationBody = <p>Are you sure?</p>, | ||
confirmationButtonContent = "Confirm", | ||
confirmationButtonTitle = "Confirm action.", | ||
defaultSuccessMessage = <p>Success!</p>, | ||
defaultFailureMessage = <p>Something went wrong.</p>, | ||
}: BookDetailsEditorPerLibraryVisibilityProps) => { | ||
const [modalState, setModalState] = React.useState<ModalState>(undefined); | ||
const [outcomeMessage, setOutcomeMessage] = React.useState< | ||
React.ReactElement | ||
>(null); | ||
const [confirmed, setConfirmed] = React.useState(false); | ||
|
||
const reset = () => { | ||
confirmed && onComplete(); | ||
setConfirmed(false); | ||
setModalState(undefined); | ||
setOutcomeMessage(null); | ||
}; | ||
const modalIsVisible = () => { | ||
return !!modalState; | ||
}; | ||
const showModal = () => { | ||
setModalState(SHOW_CONFIRMATION); | ||
}; | ||
const localOnConfirm = async () => { | ||
onConfirm() | ||
// @ts-expect-error - TODO: Fix type error, so TS knows that onConfirm() has an `unwrap()` method. | ||
.unwrap() | ||
.then((resolved) => { | ||
setOutcomeMessage( | ||
resolved?.message ? resolved.message : defaultSuccessMessage | ||
); | ||
}) | ||
.catch((error) => { | ||
let errorMessage: React.ReactElement; | ||
if (!error) { | ||
errorMessage = defaultFailureMessage; | ||
} else { | ||
let response: string; | ||
if (error.data?.detail && error.data?.title && error.data?.status) { | ||
response = `${pdString}: ${JSON.stringify(error.data)}`; | ||
} else { | ||
response = `<pre>${JSON.stringify(error, undefined, 2)}</pre>`; | ||
} | ||
errorMessage = ( | ||
<ErrorMessage | ||
error={{ url: link.href, status: error?.status, response }} | ||
/> | ||
); | ||
} | ||
setOutcomeMessage(errorMessage); | ||
}); | ||
setModalState(SHOW_OUTCOME); | ||
setConfirmed(true); | ||
}; | ||
|
||
return ( | ||
<> | ||
{!!link && ( | ||
<Button | ||
className={className} | ||
disabled={buttonDisabled || modalIsVisible()} | ||
content={buttonContent} | ||
title={buttonTitle} | ||
callback={showModal} | ||
/> | ||
)} | ||
{modalIsVisible() && ( | ||
<ConfirmationModalWithOutcome | ||
modalState={modalState} | ||
onClose={reset} | ||
onConfirm={localOnConfirm} | ||
onDismissOutcome={reset} | ||
confirmationTitle={confirmationTitle} | ||
confirmationBody={confirmationBody} | ||
confirmationButtonContent={confirmationButtonContent} | ||
confirmationButtonTitle={confirmationButtonTitle} | ||
outcomeTitle="Result" | ||
outcomeBody={outcomeMessage} | ||
/> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default BookDetailsEditorSuppression; |
Oops, something went wrong.