Skip to content

Commit 14860ff

Browse files
committed
finished making the createItem
1 parent 2f27b46 commit 14860ff

3 files changed

Lines changed: 55 additions & 27 deletions

File tree

client/src/components/ui/backend/organization_call_backend.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,12 @@ export const createItem = async (
8585
body: JSON.stringify(newData),
8686
});
8787

88-
// must return true when coding backend
89-
return await response.json();
88+
if (response.ok) return true;
89+
90+
// If it's not OK, let's see why
91+
const errorText = await response.text();
92+
console.error("Server Error Response:", errorText);
93+
return false;
9094
}
9195
};
9296

client/src/components/ui/card_organization_inventory_details_modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ interface Inventory_Details_Interface {
2727
name: string;
2828
details: string;
2929
categories?: string;
30-
availability?: string;
30+
availability?: boolean;
3131
organization?: string;
3232
collectionPoint: string;
3333
borrowerName?: string;

client/src/pages/organization_add_product.tsx

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useRouter } from "next/router";
33
import React, { useState } from "react";
44
import { useSWRConfig } from "swr/_internal";
55

6-
import { BASE_URL } from "@/components/ui/backend/organization_call_backend";
6+
import { BASE_INVENTORY_URL } from "@/components/ui/backend/organization_call_backend";
77
import {
88
createItemClean,
99
updateItemClean,
@@ -39,7 +39,7 @@ const Organization_Add_Product = () => {
3939
name: mode === "add" ? "" : parsedData?.name,
4040
details: mode === "add" ? "" : parsedData?.details,
4141
categories: mode === "add" ? "" : parsedData?.categories,
42-
availability: mode === "add" ? "" : parsedData?.availability,
42+
availability: mode === "add" ? true : parsedData?.availability,
4343

4444
// organization is kept away because we will fix it to the name of the organization that is logged in
4545
// organization: "",
@@ -76,35 +76,59 @@ const Organization_Add_Product = () => {
7676
const { mutate } = useSWRConfig(); // 1. Initiate at the TOP
7777

7878
const handleSave = async (e: React.FormEvent) => {
79-
// this is to prevent the page from emptying all the fields and refreshing
8079
e.preventDefault();
8180

82-
try {
83-
if (mode === "add" && parsedData) {
84-
// 2. Call the action (The "Messenger")
85-
await createItemClean(formData);
86-
87-
// 3. Tell SWR to refresh the Dashboard data
88-
// This tells SWR: "The data at BASE_URL is old, please go get the new list!"
89-
mutate([`${BASE_URL}`, "all"]);
81+
const dataToSubmit = { ...formData };
9082

91-
alert("Product Saved!");
92-
} else {
93-
// 2. Call the action (The "Messenger")
94-
await updateItemClean(formData);
83+
// We use "keyof Inventory_Details_Interface" to tell TS exactly what strings are allowed
84+
const dateTimeFields: (keyof Inventory_Details_Interface)[] = [
85+
"borrowedOn",
86+
"returnedOn",
87+
"dueOn",
88+
];
9589

96-
// 3. Tell SWR to refresh the Dashboard data
97-
// This tells SWR: "The data at BASE_URL is old, please go get the new list!"
98-
mutate([`${BASE_URL}`, "all"]);
90+
dateTimeFields.forEach((field) => {
91+
const value = dataToSubmit[field];
9992

100-
alert("Product updated!");
93+
if (!value || value === "") {
94+
dataToSubmit[field] = undefined; // Use undefined or null
95+
} else if (typeof value === "string" && !value.includes("T")) {
96+
// Use a type assertion to tell TS this is a valid assignment
97+
(dataToSubmit[field] as string) = `${value}T00:00:00Z`;
10198
}
99+
});
100+
101+
// 2. Handle Date fields (MUST NOT have time)
102+
// If it's empty, send null. If it has a value, leave it as YYYY-MM-DD
103+
if (!dataToSubmit.expiryDate || dataToSubmit.expiryDate === "") {
104+
dataToSubmit.expiryDate = undefined;
105+
}
102106

103-
// go back to dashboard
104-
// router.push('/organization_dashboard');
105-
router.back();
107+
try {
108+
// Ensure we are sending the right field names to Django
109+
// If your model uses dueOn, formData must have dueOn
110+
const success =
111+
mode === "add"
112+
? await createItemClean(dataToSubmit)
113+
: await updateItemClean(dataToSubmit);
114+
115+
if (success) {
116+
// 1. Refresh the main list
117+
// Use the exact string you used in your useSWR hook for the list
118+
mutate(`${BASE_INVENTORY_URL}`);
119+
120+
// 2. Refresh the statistics cards too!
121+
mutate(
122+
(key) => typeof key === "string" && key.includes("weekly_report"),
123+
);
124+
125+
alert(mode === "add" ? "Product Saved!" : "Product Updated!");
126+
router.back();
127+
} else {
128+
alert("Failed to save. Check console for details.");
129+
}
106130
} catch (error) {
107-
alert("Failed to save product: " + error);
131+
alert("Network Error: " + error);
108132
}
109133
};
110134

@@ -145,7 +169,7 @@ const Organization_Add_Product = () => {
145169
<input
146170
className="form-input"
147171
name="availability"
148-
value={formData.availability}
172+
value={"" + formData.availability}
149173
onChange={handleChange}
150174
required
151175
/>

0 commit comments

Comments
 (0)