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

Fix edit stock operation not updating #79

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const AddStockOperation: React.FC<AddStockOperationProps> = (props) => {
const tabs: TabItem[] = [
{
name: isEditing
? `${props.model.operationTypeName} Details`
? `${props.operation.name} Details`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think conditional values are both the same

: `${props.operation.name} Details`,
component: (
<BaseOperationDetails
Expand Down Expand Up @@ -102,7 +102,7 @@ const AddStockOperation: React.FC<AddStockOperationProps> = (props) => {
requiresDispatchAcknowledgement={false}
actions={{
onSave: async (model) => {
// TODO: Update
model["uuid"] = props?.operation?.uuid;
await addOrEditStockOperation(
model,
props.isEditing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,11 @@ import { getStockOperationUniqueId } from "../stock-operation.utils";
import { useTranslation } from "react-i18next";
import { useFieldArray, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import {
baseStockOperationSchema,
stockItemTableSchema,
stockOperationItemsSchema,
useValidationSchema,
} from "./validationSchema";
import { useValidationSchema } from "./validationSchema";
import StockItemsAdditionRow from "./stock-items-addition-row.component";
import { Add, ArrowRight } from "@carbon/react/icons";
import styles from "./stock-items-addition.component.scss";
import { errorAlert } from "../../core/utils/alert";
import { z } from "zod";
import { useStockOperationContext } from "./stock-operation-context/useStockOperationContext";

interface StockItemsAdditionProps {
Expand All @@ -57,6 +51,7 @@ const StockItemsAddition: React.FC<StockItemsAdditionProps> = ({
model,
onSave,
operation,
isEditing,
}) => {
const { t } = useTranslation();
const { operationType } = operation ?? {};
Expand All @@ -70,12 +65,19 @@ const StockItemsAddition: React.FC<StockItemsAdditionProps> = ({
);
return;
}
if (isEditing) {
const existingItems = model.stockOperationItems || [];
item.stockItems.forEach((newItem, index) => {
if (existingItems[index]) {
newItem.uuid = existingItems[index].uuid;
}
});
}

// const data = Object.assign(model, item);
model.stockOperationItems = item.stockItems;
await onSave?.(model);
};

const {
handleSubmit,
control,
Expand Down
4 changes: 4 additions & 0 deletions src/stock-operations/stock-operation.utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export const addOrEditStockOperation = async (
if (operation.operationType === "requisition") {
delete payload.destinationName;
}

if (operation.operationType === "stockissue") {
delete payload.requisitionStockOperationUuid;
}
const response: FetchResponse<StockOperationDTO> = await (isEditing
? updateStockOperation
: createStockOperation)(payload);
Expand Down
69 changes: 34 additions & 35 deletions src/stock-operations/stock-operations-table.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,6 @@ interface StockOperationsTableProps {

const StockOperations: React.FC<StockOperationsTableProps> = () => {
const { t } = useTranslation();
const operation: StockOperationType = useMemo(
() => ({
uuid: "",
name: "",
description: "",
operationType: "",
hasSource: false,
sourceType: "Location",
hasDestination: false,
destinationType: "Location",
hasRecipient: false,
recipientRequired: false,
availableWhenReserved: false,
allowExpiredBatchNumbers: false,
stockOperationTypeLocationScopes: [],
creator: undefined,
dateCreated: undefined,
changedBy: undefined,
dateChanged: undefined,
dateVoided: undefined,
voidedBy: undefined,
voidReason: "",
voided: false,
}),
[]
);

const {
items,
tableHeaders,
Expand Down Expand Up @@ -238,19 +211,45 @@ const StockOperations: React.FC<StockOperationsTableProps> = () => {
kind="ghost"
renderIcon={Edit}
onClick={() => {
launchAddOrEditDialog(
items[index],
true,
operation,
operations,
false
);
if (items[index]) {
// Compose the operation based on the selected stock item
const composedOperation: StockOperationType = {
uuid: items[index].uuid,
name: items[index].operationTypeName,
description: "",
operationType: items[index].operationType,
hasSource: false,
sourceType: "Location",
hasDestination: false,
destinationType: "Location",
hasRecipient: false,
recipientRequired: false,
availableWhenReserved: false,
allowExpiredBatchNumbers: false,
stockOperationTypeLocationScopes: [],
creator: undefined,
dateCreated: undefined,
changedBy: undefined,
dateChanged: undefined,
dateVoided: undefined,
voidedBy: undefined,
voidReason: "",
voided: false,
};
launchAddOrEditDialog(
items[index],
true,
composedOperation,
operations,
false
);
}
}}
/>
</Tooltip>
),
}));
}, [items, operation, operations]);
}, [items, operations]);

if (isLoading) {
return (
Expand Down
Loading