Skip to content

Commit 74eba4a

Browse files
committed
successfully made the frontend called the backend in the dashboard
1 parent e2cbea7 commit 74eba4a

3 files changed

Lines changed: 41 additions & 24 deletions

File tree

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import { generateRandomMockInventoryDetails } from "@/mocks/Inventory_Details_In
77
import { generateMockMember } from "@/mocks/Members_Details_Interface_Mocks";
88

99
// tha main URL
10-
export const BASE_URL = "http://localhost:8000/api/activities/";
10+
export const BASE_INVENTORY_URL = "http://localhost:8000/api/inventory/";
1111

1212
// change when backend is ready
13-
const isDev: boolean = true;
13+
const isDev: boolean = false;
1414

1515
// --- GET: Fetch all items ---
1616
export const getItems = async (
17-
filterType: string,
17+
URL: string,
1818
): Promise<Inventory_Details_Interface[]> => {
1919
if (isDev) {
2020
// Mock data for development
@@ -27,7 +27,8 @@ export const getItems = async (
2727
} else {
2828
// 1. Fetch from Django
2929
// fetch() is used to get the data from backend using URL
30-
const response = await fetch(`${BASE_URL}?type=${filterType}`);
30+
// `${}` is place holders for code, anything inside the curly braces is code
31+
const response = await fetch(`${URL}`);
3132

3233
// 2. Check if the request was successful
3334
if (!response.ok) throw new Error("Failed to fetch");
@@ -53,7 +54,7 @@ export const createItem = async (
5354
console.log("Mock create item called with data:", newData);
5455
return true; // Simulate successful creation
5556
} else {
56-
const response = await fetch(BASE_URL, {
57+
const response = await fetch(BASE_INVENTORY_URL, {
5758
method: "POST",
5859
headers: { "Content-Type": "application/json" },
5960
body: JSON.stringify(newData),
@@ -70,7 +71,7 @@ export const updateItem = async (
7071
newData: Inventory_Details_Interface,
7172
) => {
7273
// Django usually expects a trailing slash after the ID
73-
const response = await fetch(`${BASE_URL}${id}/`, {
74+
const response = await fetch(`${BASE_INVENTORY_URL}${id}/`, {
7475
method: "PATCH",
7576
headers: { "Content-Type": "application/json" },
7677
body: JSON.stringify(newData),
@@ -80,7 +81,7 @@ export const updateItem = async (
8081

8182
// --- DELETE: Remove an item ---
8283
export const deleteItem = async (id: number) => {
83-
const response = await fetch(`${BASE_URL}${id}/`, {
84+
const response = await fetch(`${BASE_INVENTORY_URL}${id}/`, {
8485
method: "DELETE",
8586
});
8687
// DELETE usually returns a 204 No Content status, so we don't always .json() it
@@ -103,7 +104,7 @@ export const getMembers = async (
103104
} else {
104105
// 1. Fetch from Django
105106
// fetch() is used to get the data from backend using URL
106-
const response = await fetch(`${BASE_URL}?type=${filterType}`);
107+
const response = await fetch(`${BASE_INVENTORY_URL}?type=${filterType}`);
107108

108109
// 2. Check if the request was successful
109110
if (!response.ok) throw new Error("Failed to fetch");
@@ -124,7 +125,7 @@ export const createMember = async (
124125
console.log("Mock create Member called with data:", newData);
125126
return true; // Simulate successful creation
126127
} else {
127-
const response = await fetch(BASE_URL, {
128+
const response = await fetch(BASE_INVENTORY_URL, {
128129
method: "POST",
129130
headers: { "Content-Type": "application/json" },
130131
body: JSON.stringify(newData),
@@ -141,7 +142,7 @@ export const updateMember = async (
141142
newData: Member_Details_Interface,
142143
) => {
143144
// Django usually expects a trailing slash after the ID
144-
const response = await fetch(`${BASE_URL}${id}/`, {
145+
const response = await fetch(`${BASE_INVENTORY_URL}${id}/`, {
145146
method: "PATCH",
146147
headers: { "Content-Type": "application/json" },
147148
body: JSON.stringify(newData),
@@ -151,7 +152,7 @@ export const updateMember = async (
151152

152153
// --- DELETE: Remove an Member ---
153154
export const deleteMember = async (id: number) => {
154-
const response = await fetch(`${BASE_URL}${id}/`, {
155+
const response = await fetch(`${BASE_INVENTORY_URL}${id}/`, {
155156
method: "DELETE",
156157
});
157158
// DELETE usually returns a 204 No Content status, so we don't always .json() it

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// src/hooks/organization_clean_backend_calls.ts
2-
import SWR, { KeyedMutator } from "swr";
2+
import useSWR, { KeyedMutator } from "swr";
33

44
import { Member_Details_Interface } from "@/components/ui/card_organization_member_details_modal";
55

66
import type { Inventory_Details_Interface } from "../card_organization_inventory_details_modal";
77
import {
8-
BASE_URL,
8+
BASE_INVENTORY_URL,
99
createItem,
1010
createMember,
1111
deleteItem,
@@ -54,17 +54,15 @@ Remember, this will only be invoked once, when its mounted, if you want to call
5454
// 1. Define a simple fetcher function (standard for SWR)
5555
// const fetcher = (url: string) => fetch(url).then(res => res.json());
5656

57-
// this is the one that will work with the clean function from api call
58-
const fetcher = () => getItems("all");
59-
6057
/*
6158
Even if we are calling mocks, we need to SWR
6259
6360
filterType: d to add to backend url call
6461
isDev: true if we want to mock data
6562
*/
6663
export const useOrganizationBackendGetItems = (
67-
filterType: string,
64+
category: string,
65+
sortBy: string,
6866
): organization_clean_backend_calls_return_interface => {
6967
// 2. SWR handles the state, the effect, and the async logic
7068
// SWR(key, fetcher, options)
@@ -116,9 +114,27 @@ export const useOrganizationBackendGetItems = (
116114
// mutate? what is that, so lets say we do polling every 30seconds and
117115
// ther is an inventory update that happened in between the 30seconds
118116
// SWR will immediately
119-
const { data, error, isLoading, mutate } = SWR(
120-
[`${BASE_URL}`, filterType], // The "Key" (Unique identifier)
121-
fetcher, // The "Fetcher" (Your function)
117+
118+
// generate the URL
119+
// 1. GENERATE THE URL STRING
120+
// Instead of complex if/else, we use URLSearchParams
121+
const params = new URLSearchParams();
122+
if (category && category !== "") params.append("categories", category); // Django expects 'categories'
123+
if (sortBy && sortBy !== "") params.append("ordering", sortBy); // Django expects 'ordering'
124+
125+
// If params exist, add '?' and the params, otherwise just the base URL
126+
const queryString = params.toString();
127+
128+
// 2. Fix the URL construction
129+
// We need backticks ` ` to use ${}
130+
// We need to add 'items/' because the router is registered there
131+
const finalUrl = queryString
132+
? `${BASE_INVENTORY_URL}?${queryString}`
133+
: `${BASE_INVENTORY_URL}`;
134+
135+
const { data, error, isLoading, mutate } = useSWR(
136+
finalUrl, // The "Key" (Unique identifier)
137+
getItems, // The "Fetcher" (Your function)
122138
{
123139
refreshInterval: 30, // Poll every 30 seconds 30000ms
124140
revalidateOnFocus: true, // Refresh when r clicks back into the tab
@@ -184,8 +200,8 @@ isDev: true if we want to mock data
184200
export const useOrganizationBackendGetMembers = (
185201
filterType: string,
186202
): organization_clean_backend_calls_return_get_members_interface => {
187-
const { data, error, isLoading, mutate } = SWR(
188-
[`${BASE_URL}`, filterType], // The "Key" (Unique identifier)
203+
const { data, error, isLoading, mutate } = useSWR(
204+
[`${BASE_INVENTORY_URL}`, filterType], // The "Key" (Unique identifier)
189205
fetcher2, // The "Fetcher" (Your function)
190206
{
191207
refreshInterval: 30, // Poll every 30 seconds 30000ms
@@ -246,7 +262,7 @@ export const deleteMemberClean = async (memberId: number): Promise<boolean> => {
246262

247263
// // to SWR for PUT
248264
// const { data, error, isLoading, mutate } = SWR(
249-
// [`${BASE_URL}`, "newItem"],
265+
// [`${BASE_INVENTORY_URL}`, "newItem"],
250266
// () => createItem(itemData),
251267
// {
252268
// revalidateOnFocus: true,

client/src/pages/organization_dashboard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const Organization_Dashboard = () => {
2525
error,
2626
refresh,
2727
}: organization_clean_backend_calls_return_interface =
28-
useOrganizationBackendGetItems("all");
28+
useOrganizationBackendGetItems("", "");
2929
console.log("Data from useOrganizationBackendGetItems:", data);
3030
console.log("Loading state:", loading);
3131
console.log("Error state:", error);

0 commit comments

Comments
 (0)