@@ -3,7 +3,7 @@ import { useRouter } from "next/router";
33import React , { useState } from "react" ;
44import { 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" ;
77import {
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