Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

U4X-264 : Add bulk items import #37

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ export const deleteStockModal = getAsyncLifecycle(
}
);

export const importBulkStockItemsDialog = getAsyncLifecycle(
() =>
import(
"./stock-items/add-bulk-stock-item/stock-items-bulk-import.component"
),
options
);

export function startupApp() {
defineConfigSchema(moduleName, configSchema);
}
4 changes: 4 additions & 0 deletions src/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"name": "stock-operation-dialog",
"component": "stockOperationDialog"
},
{
"name": "import-bulk-stock-items",
"component": "importBulkStockItemsDialog"
},
{
"name": "delete-stock-modal",
"component": "deleteStockModal"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Button } from "@carbon/react";
import React, { useCallback } from "react";
import { useTranslation } from "react-i18next";
import { showModal } from "@openmrs/esm-framework";

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

const handleClick = useCallback(() => {
const dispose = showModal("import-bulk-stock-items", {
closeModal: () => dispose(),
});
}, []);

return (
<Button
kind="ghost"
onClick={handleClick}
iconDescription={t("import", "Import")}
>
{t("import", "Import")}
</Button>
);
};

export default AddStockItemsBulktImportActionButton;
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import {
Button,
ContentSwitcher,
Form,
ModalBody,
ModalFooter,
ModalHeader,
Select,
SelectItem,
Switch,
TextArea,
Grid,
Checkbox,
TextInput,
IconButton,
FileUploader,
} from "@carbon/react";
import { UploadStockItems } from "./stock-items-bulk-import.resource";
import { showNotification, showToast } from "@openmrs/esm-framework";

export interface ImportDialogPopupProps {
closeModal: () => void;
}

const ImportDialogPopup: React.FC<ImportDialogPopupProps> = ({
closeModal,
}) => {
const { t } = useTranslation();
const [selectedFile, setSelectedFile] = useState<any>();
const [hasHeader, setHasHeader] = useState(true);
const [fileNotSelected, setFileNotSelected] = useState(true);

const onConfirmUpload = () => {
if (!selectedFile) {
return;
}
const formData = new FormData();
formData.set("file", selectedFile);
formData.set("hasHeader", hasHeader ? "true" : "false");

UploadStockItems(formData).then(
(resp) => {
showToast({
critical: true,
title: t("rejectOrder", "Rejected Order"),
kind: "success",
description: t(
"successfullyrejected",
`You have successfully uploaded stock items`
),
});
closeModal();
},
(err) => {
showNotification({
title: t(
`errorUploadingItems', 'An error occured uploading stock items`
),
kind: "error",
critical: true,
description: err?.message,
});
}
);
};

const onFileChanged = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event?.target?.files?.[0];
if (file) {
setSelectedFile(file);
setFileNotSelected(false);
} else {
event.preventDefault();
}
};

const onFileDeleted = () => {
setFileNotSelected(true);
};

return (
<div>
<Form>
<ModalHeader
closeModal={closeModal}
title={t("importStockItems", "Import Stock Items")}
/>
<ModalBody>
<FileUploader
accept={[".csv"]}
multiple={false}
name={"file"}
buttonLabel="Select file"
labelDescription="Only .csv files at 2mb or less"
filenameStatus="edit"
labelTitle=""
size="small"
onChange={onFileChanged}
onDelete={onFileDeleted}
/>
</ModalBody>
<ModalFooter>
<Button kind="secondary" onClick={closeModal}>
{t("cancel", "Cancel")}
</Button>
<Button type="submit" onClick={onConfirmUpload}>
{t("uploadStockItems", "Upload StockItems")}
</Button>
</ModalFooter>
</Form>
</div>
);
};

export default ImportDialogPopup;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { openmrsFetch } from "@openmrs/esm-framework";

export async function UploadStockItems(body: any) {
const abortController = new AbortController();

return openmrsFetch(`/ws/rest/v1/stockmanagement/stockitemimport`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
signal: abortController.signal,
body: body,
});
}
Empty file.
3 changes: 2 additions & 1 deletion src/stock-items/stock-items-table.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import FilterStockItems from "./components/filter-stock-items/filter-stock-items
import { launchAddOrEditDialog } from "./stock-item.utils";
import { useStockItemsPages } from "./stock-items-table.resource";
import styles from "./stock-items-table.scss";
import AddStockItemsBulktImportActionButton from "./add-bulk-stock-item/add-stock-items-bulk-import-action-button.component";
import EditStockItemActionsMenu from "./edit-stock-item/edit-stock-item-action-menu.component";

interface StockItemsTableProps {
Expand Down Expand Up @@ -126,6 +127,7 @@ const StockItemsTableComponent: React.FC<StockItemsTableProps> = () => {
filterType={isDrug}
changeFilterType={setDrug}
/>
<AddStockItemsBulktImportActionButton />
<AddStockItemActionButton />
</TableToolbarContent>
</TableToolbar>
Expand Down Expand Up @@ -189,7 +191,6 @@ const StockItemsTableComponent: React.FC<StockItemsTableProps> = () => {
{t("checkFilters", "Check the filters above")}
</p>
</div>
<p className={styles.separator}>{t("or", "or")}</p>
</Tile>
</div>
) : null}
Expand Down
1 change: 0 additions & 1 deletion src/stock-operations/stock-operations-table.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ const StockOperations: React.FC<StockOperationsTableProps> = () => {
{t("checkFilters", "Check the filters above")}
</p>
</div>
<p className={styles.separator}>{t("or", "or")}</p>
</Tile>
</div>
) : null}
Expand Down
1 change: 0 additions & 1 deletion src/stock-sources/stock-sources-items-table.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ function StockSourcesItems() {
{t("checkFilters", "Check the filters above")}
</p>
</div>
<p className={styles.separator}>{t("or", "or")}</p>
</Tile>
</div>
) : null}
Expand Down
Loading