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

(feat):refresh feature with mutate in stock tables #75

Merged
merged 5 commits into from
Jan 17, 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
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ export const deleteStockModal = getAsyncLifecycle(
}
);

export const deleteUserScopeModal = getAsyncLifecycle(
() =>
import("./stock-user-role-scopes/delete-stock-user-scope-modal.component"),
{
featureName: "delete-stock-user-scope-modal",
moduleName,
}
);

export const importBulkStockItemsDialog = getAsyncLifecycle(
() =>
import(
Expand Down
4 changes: 4 additions & 0 deletions src/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
{
"name": "delete-stock-modal",
"component": "deleteStockModal"
},
{
"name": "delete-stock-user-scope-modal",
"component": "deleteUserScopeModal"
}
],
"pages": [
Expand Down
8 changes: 7 additions & 1 deletion src/stock-items/stock-items-table.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ const StockItemsTableComponent: React.FC<StockItemsTableProps> = () => {

return (
<>
<TabPanel>Drugs and other stock items managed by the system.</TabPanel>
<TabPanel>
{t(
"panelDescription",
"Drugs and other stock items managed by the system."
)}
</TabPanel>

<DataTable
rows={tableRows}
headers={tableHeaders}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,6 @@ const BaseOperationDetails: React.FC<BaseOperationDetailsProps> = ({
labelText={operation?.hasDestination ? "From:" : "From:"}
/>
)}
{/* {(!canEdit || lockSource) && operation?.hasSource && (
<TextInput
id="sourceUuidLbl"
value={model?.sourceName ?? ""}
readOnly={true}
labelText={operation?.hasDestination ? "From:" : "Location:"}
/>
)} */}

{canEdit && !lockDestination && operation?.hasDestination && (
<PartySelector
controllerName="destinationUuid"
Expand Down Expand Up @@ -324,14 +315,6 @@ const BaseOperationDetails: React.FC<BaseOperationDetailsProps> = ({
labelText={operation?.hasSource ? "To:" : "To:"}
/>
)}
{/* {(!canEdit || lockDestination) && operation?.hasDestination && (
<TextInput
id="destinationUuidLbl"
value={model?.destinationName ?? ""}
readOnly={true}
labelText={operation?.hasSource ? "To:" : "Location:"}
/>
)} */}

{canEdit && (
<UsersSelector
Expand Down
5 changes: 4 additions & 1 deletion src/stock-operations/stock-operations-table.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,10 @@ const StockOperations: React.FC<StockOperationsTableProps> = () => {

return (
<div className={styles.tableOverride}>
<TabPanel>Stock operations to track movement of stock.</TabPanel>
<TabPanel>
{t("panelDescription", "Stock operations to track movement of stock.")}
</TabPanel>

<div id="table-tool-bar">
<div></div>
<div className="right-filters"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
SelectItem,
} from "@carbon/react";
import React, { ChangeEvent, useCallback, useState } from "react";
import { handleMutate } from "../swr-revalidation";
import styles from "./add-stock-sources.scss";
import { useConceptById } from "../../stock-lookups/stock-lookups.resource";
import { STOCK_SOURCE_TYPE_CODED_CONCEPT_ID } from "../../constants";
Expand Down Expand Up @@ -65,6 +66,9 @@ const StockSourcesAddOrUpdate: React.FC<AddStockSourceProps> = ({ model }) => {
"Stock Source Added Successfully"
),
});

handleMutate("ws/rest/v1/stockmanagement/stocksource");

closeOverlay();
},
(error) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from "react";
import { handleMutate } from "../swr-revalidation";
import { Button, InlineLoading } from "@carbon/react";
import { useTranslation } from "react-i18next";
import { TrashCan } from "@carbon/react/icons";
Expand Down Expand Up @@ -27,6 +28,8 @@ const StockSourcesDeleteActionMenu: React.FC<
.then(
() => {
setDeletingSource(false);

handleMutate("ws/rest/v1/stockmanagement/stocksource");
showToast({
critical: true,
title: t("deletingSource", "Delete Source"),
Expand Down
7 changes: 6 additions & 1 deletion src/stock-sources/stock-sources-items-table.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ function StockSourcesItems() {

return (
<div className={styles.tableOverride}>
<TabPanel>List of partners who provide stock to the facility.</TabPanel>
<TabPanel>
{t(
"panelDescription",
"List of partners who provide stock to the facility."
)}
</TabPanel>
<div id="table-tool-bar">
<div></div>
<div className="right-filters"></div>
Expand Down
7 changes: 7 additions & 0 deletions src/stock-sources/swr-revalidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { mutate } from "swr";

export const handleMutate = (url: string) => {
mutate((key) => typeof key === "string" && key.startsWith(url), undefined, {
revalidate: true,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { debounce } from "lodash-es";
import { User } from "../../core/api/types/identity/User";
import { Role } from "../../core/api/types/identity/Role";
import { StockOperationType } from "../../core/api/types/stockOperation/StockOperationType";
import { handleMutate } from "../swr-revalidation";

const MinDate: Date = today();

Expand Down Expand Up @@ -247,6 +248,7 @@ const AddStockUserRoleScope: React.FC<AddStockUserRoleScopeProps> = ({

createOrUpdateUserRoleScope(formModel).then(
(res) => {
handleMutate("ws/rest/v1/stockmanagement/userrolescope");
showToast({
critical: true,
title: t("addUserRole", "Add User role"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from "react";
import { useTranslation } from "react-i18next";
import { Button, ModalHeader, ModalBody, ModalFooter } from "@carbon/react";
import styles from "../root.scss";

interface DeleteConfirmationProps {
uuid?: string;
close: () => void;
onConfirmation: () => void;
}

const DeleteConfirmation: React.FC<DeleteConfirmationProps> = ({
close,
onConfirmation,
uuid,
}) => {
const { t } = useTranslation();
const handleCancel = () => close();
const handleDelete = () => onConfirmation?.();

return (
<>
<ModalHeader closeModal={close} className={styles.productiveHeading03}>
{t("deleteStockUserScope", "Delete Stock User Scope")}?
</ModalHeader>
<ModalBody>
<p className={styles.bodyLong01}>
{t(
"deleteConfirmationText",
`Are you sure you want to delete this User Scope? This action can't be undone.`,
{ encounter: uuid }
)}
</p>
</ModalBody>
<ModalFooter>
<Button size="lg" kind="secondary" onClick={handleCancel}>
{t("cancel", "Cancel")}
</Button>
<Button autoFocus kind="danger" onClick={handleDelete} size="lg">
{t("delete", "Delete")}
</Button>
</ModalFooter>
</>
);
};

export default DeleteConfirmation;
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React, { useState } from "react";
import { Button, InlineLoading } from "@carbon/react";
import { useTranslation } from "react-i18next";
import { TrashCan } from "@carbon/react/icons";
import { deleteUserRoleScopes } from "../stock-user-role-scopes.resource";
import { showModal, showNotification, showToast } from "@openmrs/esm-framework";
import { handleMutate } from "../swr-revalidation";

interface StockUserScopDeleteActionMenuProps {
uuid: string;
}

const StockUserScopeDeleteActionMenu: React.FC<
StockUserScopDeleteActionMenuProps
> = ({ uuid }) => {
const { t } = useTranslation();

const [deletingUserScope, setDeletingUserScope] = useState(false);

const handleDeleteStockUserScope = React.useCallback(() => {
const close = showModal("delete-stock-user-scope-modal", {
close: () => close(),
uuid: uuid,
onConfirmation: () => {
const ids = [];
ids.push(uuid);
deleteUserRoleScopes(ids)
.then(
() => {
handleMutate("ws/rest/v1/stockmanagement/userrolescope");
setDeletingUserScope(false);
showToast({
critical: true,
title: t("deletingstockUserScope", "Delete Stock User Scope"),
kind: "success",
description: t(
"stockuserscopedeletedsuccessfully",
"Stock User Scope Deleted Successfully"
),
});
},
(error) => {
setDeletingUserScope(false);
showNotification({
title: t(
`errorDeletingUserScope', 'error deleting a user scope`
),
kind: "error",
critical: true,
description: error?.message,
});
}
)
.catch();
close();
},
});
}, [t, uuid]);

const deleteButton = (
<Button
kind="ghost"
size="md"
onClick={handleDeleteStockUserScope}
iconDescription={t("deleteUserScope", "Delete Stock User Scope")}
renderIcon={(props) => <TrashCan size={16} {...props} />}
/>
);

return deletingUserScope ? <InlineLoading /> : deleteButton;
};

export default StockUserScopeDeleteActionMenu;
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { URL_USER_ROLE_SCOPE } from "../stock-items/stock-items-table.component"
import AddStockUserRoleScopeActionButton from "./add-stock-user-role-scope-button.component";
import { formatDisplayDate } from "../core/utils/datetimeUtils";
import EditStockUserRoleActionsMenu from "./edit-stock-user-scope/edit-stock-user-scope-action-menu.component";
import StockUserScopeDeleteActionMenu from "./delete-stock-user-scope/delete-stock-user-scope.component";

function StockUserRoleScopesItems() {
const { t } = useTranslation();
Expand Down Expand Up @@ -88,7 +89,12 @@ function StockUserRoleScopesItems() {
enabled: userRoleScope?.enabled
? t("stockmanagement.yes", "Yes")
: t("stockmanagement.no", "No"),
actions: <EditStockUserRoleActionsMenu data={items[index]} />,
actions: (
<div style={{ display: "flex" }}>
<EditStockUserRoleActionsMenu data={items[index]} />
<StockUserScopeDeleteActionMenu uuid={items[index].uuid} />
</div>
),
};
});
}, [items, t]);
Expand All @@ -100,8 +106,10 @@ function StockUserRoleScopesItems() {
return (
<div className={styles.tableOverride}>
<TabPanel>
To access stock management features, users must have assigned roles
specifying location and stock operation type scopes.
{t(
"panelDescription",
"To access stock management features, users must have assigned roles specifying location and stock operation type scopes."
)}
</TabPanel>
<div id="table-tool-bar">
<div></div>
Expand Down
7 changes: 7 additions & 0 deletions src/stock-user-role-scopes/swr-revalidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { mutate } from "swr";

export const handleMutate = (url: string) => {
mutate((key) => typeof key === "string" && key.startsWith(url), undefined, {
revalidate: true,
});
};
Loading