UPDATE: Code Sandbox link - https://codesandbox.io/s/goofy-dubinsky-4ui5v?file=/src/SearchRow.js
There is a use-case where we have a search results table and there are some rows that have a button to audit that row data for business purposes. Now when the user clicks on that button we need to prompt the user for confirmation and we are stuck on how to trigger this.
WHAT TO DO IN TODO PART (SearchRow.js)?
Here is the main SearchResultsPage.js
return (
{results.map((searchRow) => {
<SearchRow content={searchRow.content} showSaveButton={searchRow.content.id === 10} />
});
);
SearchRow.js
function SearchRow({content, showSaveButton}) {
const getConfirmation = (event) => {
// TODO: Call Modal.js, if user presses yes then call the saveProductData function, otherwise return
}
const saveProductData = async () => {
await axios.post("url", content);
}
<div>
<p>{content.title}</p>
{showSaveButton &&
<Button variant="primary" onClick={(e) => getConfirmation(e)} />
}
</div>
}
Finally Modal.js
function Modal({
heading,
message,
buttonSuccess = "Yes",
buttonFailure = "No",
showModal = false,
}) {
const [show, setShow] = useState(showModal);
return (
<BootstrapModal show={show} onHide={setShow(false)}>
<BootstrapModal.Header closeButton>
<BootstrapModal.Title>{heading}</BootstrapModal.Title>
</BootstrapModal.Header>
<BootstrapModal.Body>{message}</BootstrapModal.Body>
<BootstrapModal.Footer>
<Button variant="secondary" onClick={setShow(false)}>
{buttonSuccess}
</Button>
<Button variant="primary" onClick={setShow(false)}>
{buttonFailure}
</Button>
</BootstrapModal.Footer>
</BootstrapModal>
);
}
Can someone please help on how to achieve this or if there is any other better approach or any suggestions?
Thanks in advance :)