Skip to content

Commit

Permalink
(Ft)Add ability to update stock sources (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
jecihjoy authored Dec 12, 2023
1 parent cd60bda commit 7bdd754
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/stock-sources/add-stock-source-button.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Button } from "@carbon/react";
import React, { useCallback } from "react";
import { useTranslation } from "react-i18next";
import { launchOverlay } from "../core/components/overlay/hook";
import AddStockSourceItem from "./add-stock-sources/add-stock-sources.component";
import StockSourcesAddOrUpdate from "./add-stock-sources/add-stock-sources.component";

const AddStockSourceActionButton: React.FC = () => {
const { t } = useTranslation();

const handleClick = useCallback(() => {
launchOverlay("Add Stock Source", <AddStockSourceItem />);
launchOverlay("Add Stock Source", <StockSourcesAddOrUpdate />);
}, []);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import { showNotification, showToast } from "@openmrs/esm-framework";
import { useTranslation } from "react-i18next";
import { closeOverlay } from "../../core/components/overlay/hook";

const StockSourcesAddOrCreate: React.FC = () => {
interface AddStockSourceProps {
model?: StockSource;
}

const StockSourcesAddOrUpdate: React.FC<AddStockSourceProps> = ({ model }) => {
const { t } = useTranslation();

// get stock sources
Expand All @@ -27,10 +31,12 @@ const StockSourcesAddOrCreate: React.FC = () => {
const [formModel, setFormModel] = useState<StockSource>();

const onNameChanged = (evt: React.ChangeEvent<HTMLInputElement>): void => {
model ? (model.name = evt.target.value) : "";
setFormModel({ ...formModel, name: evt.target.value });
};

const onAcronymChanged = (evt: React.ChangeEvent<HTMLInputElement>): void => {
model ? (model.acronym = evt.target.value) : "";
setFormModel({ ...formModel, acronym: evt.target.value });
};

Expand All @@ -44,6 +50,9 @@ const StockSourcesAddOrCreate: React.FC = () => {
const onFormSubmit = useCallback(
(event) => {
event.preventDefault();
if (model) {
formModel.uuid = model.uuid;
}
createOrUpdateStockSource(formModel)
.then(
() => {
Expand Down Expand Up @@ -83,6 +92,7 @@ const StockSourcesAddOrCreate: React.FC = () => {
labelText={t("fullName", "Full Name")}
size="md"
onChange={onNameChanged}
value={model?.name}
placeholder="e.g National Medical Stores"
/>
</section>
Expand All @@ -93,6 +103,7 @@ const StockSourcesAddOrCreate: React.FC = () => {
size="md"
placeholder="e.g NMS"
onChange={onAcronymChanged}
value={model?.acronym}
labelText={t("acronym", "Acronym/Code")}
/>
</section>
Expand Down Expand Up @@ -134,4 +145,4 @@ const StockSourcesAddOrCreate: React.FC = () => {
);
};

export default StockSourcesAddOrCreate;
export default StockSourcesAddOrUpdate;
31 changes: 29 additions & 2 deletions src/stock-sources/stock-sources-items-table.component.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useMemo } from "react";

import React, { useCallback, useMemo } from "react";
import {
DataTable,
Pagination,
Expand All @@ -17,14 +16,19 @@ import {
TableToolbarMenu,
DataTableSkeleton,
TableToolbarSearch,
Button,
Tooltip,
} from "@carbon/react";
import { isDesktop } from "@openmrs/esm-framework";
import { Edit } from "@carbon/react/icons";
import useStockSourcesPage from "./stock-sources-items-table.resource";
import { ResourceRepresentation } from "../core/api/api";
import AddStockSourceActionButton from "./add-stock-source-button.component";
import StockSourcesFilter from "./stock-sources-filter/stock-sources-filter.component";
import styles from "./stock-sources.scss";
import { useTranslation } from "react-i18next";
import { launchOverlay } from "../core/components/overlay/hook";
import StockSourcesAddOrUpdate from "./add-stock-sources/add-stock-sources.component";

function StockSourcesItems() {
const { t } = useTranslation();
Expand All @@ -45,6 +49,29 @@ function StockSourcesItems() {
totalCount: true,
});

const handleClick = useCallback((data: any) => {
launchOverlay(
"Edit Stock Source",
<StockSourcesAddOrUpdate model={data} />
);
}, []);

if (items) {
items.forEach((row) => {
row["actions"] = (
<Tooltip align="bottom" label="Edit Stock Item">
<Button
kind="ghost"
size="md"
onClick={() => handleClick(row)}
iconDescription={t("editStockItem", "Edit Stock Item")}
renderIcon={(props) => <Edit size={16} {...props} />}
></Button>
</Tooltip>
);
});
}

const tableRows = useMemo(() => {
return items?.map((entry) => {
return {
Expand Down
1 change: 1 addition & 0 deletions src/stock-sources/stock-sources-items-table.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default function useStockSourcesPage(filter: StockOperationFilter) {
{
id: 4,
header: "actions",
key: "actions",
},
],
[]
Expand Down

0 comments on commit 7bdd754

Please sign in to comment.