generated from openmrs/openmrs-esm-template-app
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stock) : U4X-289 Deleting stocksources (#47)
- Loading branch information
Showing
4 changed files
with
96 additions
and
69 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
47 changes: 25 additions & 22 deletions
47
src/stock-sources/edit-stock-source/edit-stock-source.component.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 |
---|---|---|
@@ -1,32 +1,35 @@ | ||
import { Button, Tooltip } from "@carbon/react"; | ||
import React, { useCallback } from "react"; | ||
import { Button } from "@carbon/react"; | ||
import { Edit } from "@carbon/react/icons"; | ||
import { interpolateUrl, navigate } from "@openmrs/esm-framework"; | ||
|
||
import React, { AnchorHTMLAttributes } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { launchOverlay } from "../../core/components/overlay/hook"; | ||
import StockSourcesAddOrUpdate from "../add-stock-sources/add-stock-sources.component"; | ||
import { StockSource } from "../../core/api/types/stockOperation/StockSource"; | ||
|
||
interface NameLinkProps extends AnchorHTMLAttributes<HTMLAnchorElement> { | ||
to: string; | ||
from: string; | ||
interface EditStockSourcesActionMenuProps { | ||
data?: StockSource; | ||
} | ||
|
||
const EditSourceActionsMenu: React.FC<NameLinkProps> = ({ from, to }) => { | ||
const EditStockSourceActionsMenu: React.FC<EditStockSourcesActionMenuProps> = ({ | ||
data, | ||
}) => { | ||
const { t } = useTranslation(); | ||
const handleNameClick = (event: MouseEvent, to: string) => { | ||
event.preventDefault(); | ||
navigate({ to }); | ||
localStorage.setItem("fromPage", from); | ||
}; | ||
const handleClick = useCallback(() => { | ||
launchOverlay( | ||
"Edit Stock Source", | ||
<StockSourcesAddOrUpdate model={data} /> | ||
); | ||
}, [data]); | ||
|
||
return ( | ||
<Tooltip align="bottom" label="Edit Patient"> | ||
<Button | ||
kind="ghost" | ||
onClick={(e) => handleNameClick(e, to)} | ||
href={interpolateUrl(to)} | ||
iconDescription={t("editSource", "Edit Source")} | ||
renderIcon={(props) => <Edit size={16} {...props} />} | ||
></Button> | ||
</Tooltip> | ||
<Button | ||
kind="ghost" | ||
size="md" | ||
onClick={() => handleClick()} | ||
iconDescription={t("editStockItem", "Edit Stock Item")} | ||
renderIcon={(props) => <Edit size={16} {...props} />} | ||
/> | ||
); | ||
}; | ||
export default EditSourceActionsMenu; | ||
export default EditStockSourceActionsMenu; |
76 changes: 60 additions & 16 deletions
76
src/stock-sources/stock-sources-delete/stock-sources-delete.component.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 |
---|---|---|
@@ -1,20 +1,64 @@ | ||
import React from "react"; | ||
import { Button, Form, ModalFooter, ModalHeader } from "@carbon/react"; | ||
import React, { useState } from "react"; | ||
import { Button, InlineLoading } from "@carbon/react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { TrashCan } from "@carbon/react/icons"; | ||
import { deleteStockSource } from "../stock-sources.resource"; | ||
import { showNotification, showToast } from "@openmrs/esm-framework"; | ||
|
||
const StockSourcesDelete: React.FC = () => { | ||
return ( | ||
<div> | ||
<Form> | ||
<ModalHeader /> | ||
<ModalFooter> | ||
<Button kind="secondary">Cancel</Button> | ||
<Button kind="danger" type="submit"> | ||
Delete | ||
</Button> | ||
</ModalFooter> | ||
</Form> | ||
</div> | ||
interface StockSourcesDeleteActionMenuProps { | ||
uuid: string; | ||
} | ||
|
||
const StockSourcesDeleteActionMenu: React.FC< | ||
StockSourcesDeleteActionMenuProps | ||
> = ({ uuid }) => { | ||
const { t } = useTranslation(); | ||
|
||
const [deletingSource, setDeletingSource] = useState(false); | ||
|
||
const handleClick = (e) => { | ||
e.preventDefault(); | ||
setDeletingSource(true); | ||
const ids = []; | ||
ids.push(uuid); | ||
deleteStockSource(ids) | ||
.then( | ||
() => { | ||
setDeletingSource(false); | ||
showToast({ | ||
critical: true, | ||
title: t("deletingSource", "Delete Source"), | ||
kind: "success", | ||
description: t( | ||
"stocksourcedeletedsuccessfully", | ||
"Stock Source Deleted Successfully" | ||
), | ||
}); | ||
}, | ||
(error) => { | ||
setDeletingSource(false); | ||
showNotification({ | ||
title: t(`errorDeletingSource', 'error deleting a source`), | ||
kind: "error", | ||
critical: true, | ||
description: error?.message, | ||
}); | ||
} | ||
) | ||
.catch(); | ||
}; | ||
|
||
const deleteButton = ( | ||
<Button | ||
kind="ghost" | ||
size="md" | ||
onClick={handleClick} | ||
iconDescription={t("deleteSource", "Delete Source")} | ||
renderIcon={(props) => <TrashCan size={16} {...props} />} | ||
/> | ||
); | ||
|
||
return deletingSource ? <InlineLoading /> : deleteButton; | ||
}; | ||
|
||
export default StockSourcesDelete; | ||
export default StockSourcesDeleteActionMenu; |
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