|
| 1 | +// src/hooks/organization_call_backend.ts |
| 2 | +// Change this URL to match your backend API endpoint |
| 3 | + |
| 4 | +import { Inventory_Details_Interface } from "@/components/ui/card_organization_inventory_details_modal"; |
| 5 | +import { Member_Details_Interface } from "@/components/ui/card_organization_member_details_modal"; |
| 6 | +import { generateRandomMockInventoryDetails } from "@/mocks/Inventory_Details_Interface_Mocks"; |
| 7 | +import { generateMockMember } from "@/mocks/Members_Details_Interface_Mocks"; |
| 8 | + |
| 9 | +// tha main URL |
| 10 | +export const BASE_URL = "http://localhost:8000/api/activities/"; |
| 11 | + |
| 12 | +// change when backend is ready |
| 13 | +const isDev: boolean = true; |
| 14 | + |
| 15 | +// --- GET: Fetch all items --- |
| 16 | +export const getItems = async ( |
| 17 | + filterType: string, |
| 18 | +): Promise<Inventory_Details_Interface[]> => { |
| 19 | + if (isDev) { |
| 20 | + // Mock data for development |
| 21 | + const mockData: Inventory_Details_Interface[] = []; |
| 22 | + for (let i = 0; i < 10; i++) { |
| 23 | + mockData.push(generateRandomMockInventoryDetails()); |
| 24 | + } |
| 25 | + console.log("Mock data generated:", mockData); |
| 26 | + return mockData; |
| 27 | + } else { |
| 28 | + // 1. Fetch from Django |
| 29 | + // fetch() is used to get the data from backend using URL |
| 30 | + const response = await fetch(`${BASE_URL}?type=${filterType}`); |
| 31 | + |
| 32 | + // 2. Check if the request was successful |
| 33 | + if (!response.ok) throw new Error("Failed to fetch"); |
| 34 | + |
| 35 | + // 3. Parse the JSON data |
| 36 | + // because fetcah returns a response, it isnt the data yet, |
| 37 | + // its just the response header, |
| 38 | + // you will need to use .json() to get the data |
| 39 | + // it converts raw bytes to Javascript objects |
| 40 | + return await response.json(); // Returns the list from Django |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +// --- POST: Create a new item --- |
| 45 | +// .stringify, flattens the object |
| 46 | +// headers: { 'Content-Type': 'application/json' } determine, how the data will be dealt with by the server |
| 47 | +// possible types: text/plain, text/html application/json, images/jpeg, application/x-www-form-urlencoded ( form data ), |
| 48 | +// if you dont have this, might get error 400 |
| 49 | +export const createItem = async ( |
| 50 | + newData: Inventory_Details_Interface, |
| 51 | +): Promise<boolean> => { |
| 52 | + if (isDev) { |
| 53 | + console.log("Mock create item called with data:", newData); |
| 54 | + return true; // Simulate successful creation |
| 55 | + } else { |
| 56 | + const response = await fetch(BASE_URL, { |
| 57 | + method: "POST", |
| 58 | + headers: { "Content-Type": "application/json" }, |
| 59 | + body: JSON.stringify(newData), |
| 60 | + }); |
| 61 | + |
| 62 | + // must return true when coding backend |
| 63 | + return await response.json(); |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +// --- PATCH/PUT: Update an existing item --- |
| 68 | +export const updateItem = async ( |
| 69 | + id: number, |
| 70 | + 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", |
| 75 | + headers: { "Content-Type": "application/json" }, |
| 76 | + body: JSON.stringify(newData), |
| 77 | + }); |
| 78 | + return await response.json(); |
| 79 | +}; |
| 80 | + |
| 81 | +// --- DELETE: Remove an item --- |
| 82 | +export const deleteItem = async (id: number) => { |
| 83 | + const response = await fetch(`${BASE_URL}${id}/`, { |
| 84 | + method: "DELETE", |
| 85 | + }); |
| 86 | + // DELETE usually returns a 204 No Content status, so we don't always .json() it |
| 87 | + return response.ok; |
| 88 | +}; |
| 89 | + |
| 90 | +// MEMBERS SECTION |
| 91 | + |
| 92 | +export const getMembers = async ( |
| 93 | + filterType: string, |
| 94 | +): Promise<Member_Details_Interface[]> => { |
| 95 | + if (isDev) { |
| 96 | + // Mock data for development |
| 97 | + const mockData: Member_Details_Interface[] = []; |
| 98 | + for (let i = 0; i < 10; i++) { |
| 99 | + mockData.push(generateMockMember()); |
| 100 | + } |
| 101 | + console.log("Mock data generated:", mockData); |
| 102 | + return mockData; |
| 103 | + } else { |
| 104 | + // 1. Fetch from Django |
| 105 | + // fetch() is used to get the data from backend using URL |
| 106 | + const response = await fetch(`${BASE_URL}?type=${filterType}`); |
| 107 | + |
| 108 | + // 2. Check if the request was successful |
| 109 | + if (!response.ok) throw new Error("Failed to fetch"); |
| 110 | + |
| 111 | + // 3. Parse the JSON data |
| 112 | + // because fetcah returns a response, it isnt the data yet, |
| 113 | + // its just the response header, |
| 114 | + // you will need to use .json() to get the data |
| 115 | + // it converts raw bytes to Javascript objects |
| 116 | + return await response.json(); // Returns the list from Django |
| 117 | + } |
| 118 | +}; |
| 119 | + |
| 120 | +export const createMember = async ( |
| 121 | + newData: Member_Details_Interface, |
| 122 | +): Promise<boolean> => { |
| 123 | + if (isDev) { |
| 124 | + console.log("Mock create Member called with data:", newData); |
| 125 | + return true; // Simulate successful creation |
| 126 | + } else { |
| 127 | + const response = await fetch(BASE_URL, { |
| 128 | + method: "POST", |
| 129 | + headers: { "Content-Type": "application/json" }, |
| 130 | + body: JSON.stringify(newData), |
| 131 | + }); |
| 132 | + |
| 133 | + // must return true when coding backend |
| 134 | + return await response.json(); |
| 135 | + } |
| 136 | +}; |
| 137 | + |
| 138 | +// --- PATCH/PUT: Update an existing Member --- |
| 139 | +export const updateMember = async ( |
| 140 | + id: number, |
| 141 | + newData: Member_Details_Interface, |
| 142 | +) => { |
| 143 | + // Django usually expects a trailing slash after the ID |
| 144 | + const response = await fetch(`${BASE_URL}${id}/`, { |
| 145 | + method: "PATCH", |
| 146 | + headers: { "Content-Type": "application/json" }, |
| 147 | + body: JSON.stringify(newData), |
| 148 | + }); |
| 149 | + return await response.json(); |
| 150 | +}; |
| 151 | + |
| 152 | +// --- DELETE: Remove an Member --- |
| 153 | +export const deleteMember = async (id: number) => { |
| 154 | + const response = await fetch(`${BASE_URL}${id}/`, { |
| 155 | + method: "DELETE", |
| 156 | + }); |
| 157 | + // DELETE usually returns a 204 No Content status, so we don't always .json() it |
| 158 | + return response.ok; |
| 159 | +}; |
0 commit comments