1+ // @author sylee212
12// src/hooks/organization_call_backend.ts
23// Change this URL to match your backend API endpoint
34
@@ -7,14 +8,23 @@ import { generateRandomMockInventoryDetails } from "@/mocks/Inventory_Details_In
78import { generateMockMember } from "@/mocks/Members_Details_Interface_Mocks" ;
89
910// tha main URL
10- export const BASE_URL = "http://localhost:8000/api/activities/" ;
11+ export const BASE_INVENTORY_URL = "http://localhost:8000/api/inventory/" ;
12+ export const WEEKLY_REPORT_BY_FIELD =
13+ "http://localhost:8000/api/inventory/weeklyReportByField/" ;
14+
15+ export interface django_count_response_interface {
16+ field : number ;
17+ thisWeek : number ;
18+ lastWeek : number ;
19+ difference : number ;
20+ }
1121
1222// change when backend is ready
13- const isDev : boolean = true ;
23+ const isDev : boolean = false ;
1424
1525// --- GET: Fetch all items ---
1626export const getItems = async (
17- filterType : string ,
27+ URL : string ,
1828) : Promise < Inventory_Details_Interface [ ] > => {
1929 if ( isDev ) {
2030 // Mock data for development
@@ -27,7 +37,8 @@ export const getItems = async (
2737 } else {
2838 // 1. Fetch from Django
2939 // fetch() is used to get the data from backend using URL
30- const response = await fetch ( `${ BASE_URL } ?type=${ filterType } ` ) ;
40+ // `${}` is place holders for code, anything inside the curly braces is code
41+ const response = await fetch ( `${ URL } ` ) ;
3142
3243 // 2. Check if the request was successful
3344 if ( ! response . ok ) throw new Error ( "Failed to fetch" ) ;
@@ -41,6 +52,22 @@ export const getItems = async (
4152 }
4253} ;
4354
55+ export const getWeeklyReportByField = async (
56+ URL : string ,
57+ ) : Promise < django_count_response_interface > => {
58+ const response = await fetch ( `${ URL } ` ) ;
59+
60+ // 2. Check if the request was successful
61+ if ( ! response . ok ) throw new Error ( "Failed to fetch" ) ;
62+
63+ // 3. Parse the JSON data
64+ // because fetcah returns a response, it isnt the data yet,
65+ // its just the response header,
66+ // you will need to use .json() to get the data
67+ // it converts raw bytes to Javascript objects
68+ return await response . json ( ) ; // Returns the list from Django
69+ } ;
70+
4471// --- POST: Create a new item ---
4572// .stringify, flattens the object
4673// headers: { 'Content-Type': 'application/json' } determine, how the data will be dealt with by the server
@@ -53,34 +80,43 @@ export const createItem = async (
5380 console . log ( "Mock create item called with data:" , newData ) ;
5481 return true ; // Simulate successful creation
5582 } else {
56- const response = await fetch ( BASE_URL , {
83+ const response = await fetch ( BASE_INVENTORY_URL , {
5784 method : "POST" ,
5885 headers : { "Content-Type" : "application/json" } ,
5986 body : JSON . stringify ( newData ) ,
6087 } ) ;
6188
62- // must return true when coding backend
63- return await response . json ( ) ;
89+ if ( response . ok ) return true ;
90+
91+ // If it's not OK, let's see why
92+ const errorText = await response . text ( ) ;
93+ console . error ( "Server Error Response:" , errorText ) ;
94+ return false ;
6495 }
6596} ;
6697
6798// --- PATCH/PUT: Update an existing item ---
6899export const updateItem = async (
69100 id : number ,
70101 newData : Inventory_Details_Interface ,
71- ) => {
72- // Django usually expects a trailing slash after the ID
73- const response = await fetch ( `${ BASE_URL } ${ id } /` , {
74- method : "PATCH" ,
102+ ) : Promise < boolean > => {
103+ const response = await fetch ( `${ BASE_INVENTORY_URL } ${ id } /` , {
104+ method : "PATCH" , // PATCH is better than PUT for Partial updates
75105 headers : { "Content-Type" : "application/json" } ,
76106 body : JSON . stringify ( newData ) ,
77107 } ) ;
78- return await response . json ( ) ;
108+
109+ if ( response . ok ) return true ;
110+
111+ // If it's not OK, let's see why
112+ const errorText = await response . text ( ) ;
113+ console . error ( "Server Error Response:" , errorText ) ;
114+ return false ;
79115} ;
80116
81117// --- DELETE: Remove an item ---
82118export const deleteItem = async ( id : number ) => {
83- const response = await fetch ( `${ BASE_URL } ${ id } /` , {
119+ const response = await fetch ( `${ BASE_INVENTORY_URL } ${ id } /` , {
84120 method : "DELETE" ,
85121 } ) ;
86122 // DELETE usually returns a 204 No Content status, so we don't always .json() it
@@ -89,6 +125,7 @@ export const deleteItem = async (id: number) => {
89125
90126// MEMBERS SECTION
91127
128+ // PENDING
92129export const getMembers = async (
93130 filterType : string ,
94131) : Promise < Member_Details_Interface [ ] > => {
@@ -103,7 +140,7 @@ export const getMembers = async (
103140 } else {
104141 // 1. Fetch from Django
105142 // fetch() is used to get the data from backend using URL
106- const response = await fetch ( `${ BASE_URL } ?type=${ filterType } ` ) ;
143+ const response = await fetch ( `${ BASE_INVENTORY_URL } ?type=${ filterType } ` ) ;
107144
108145 // 2. Check if the request was successful
109146 if ( ! response . ok ) throw new Error ( "Failed to fetch" ) ;
@@ -124,7 +161,7 @@ export const createMember = async (
124161 console . log ( "Mock create Member called with data:" , newData ) ;
125162 return true ; // Simulate successful creation
126163 } else {
127- const response = await fetch ( BASE_URL , {
164+ const response = await fetch ( BASE_INVENTORY_URL , {
128165 method : "POST" ,
129166 headers : { "Content-Type" : "application/json" } ,
130167 body : JSON . stringify ( newData ) ,
@@ -141,7 +178,7 @@ export const updateMember = async (
141178 newData : Member_Details_Interface ,
142179) => {
143180 // Django usually expects a trailing slash after the ID
144- const response = await fetch ( `${ BASE_URL } ${ id } /` , {
181+ const response = await fetch ( `${ BASE_INVENTORY_URL } ${ id } /` , {
145182 method : "PATCH" ,
146183 headers : { "Content-Type" : "application/json" } ,
147184 body : JSON . stringify ( newData ) ,
@@ -151,7 +188,7 @@ export const updateMember = async (
151188
152189// --- DELETE: Remove an Member ---
153190export const deleteMember = async ( id : number ) => {
154- const response = await fetch ( `${ BASE_URL } ${ id } /` , {
191+ const response = await fetch ( `${ BASE_INVENTORY_URL } ${ id } /` , {
155192 method : "DELETE" ,
156193 } ) ;
157194 // DELETE usually returns a 204 No Content status, so we don't always .json() it
0 commit comments