From 52a0727e1e4aa8d2330e471ea0119203169eddc4 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 28 Sep 2024 23:29:45 +0930 Subject: [PATCH] feat(hook): Add apiFetch to collect data from api ref: #1 nofusscomputing/centurion_erp#248 nofusscomputing/centurion_erp#307 nofusscomputing/centurion_erp#319 --- .vscode/launch.json | 2 +- src/App.js | 25 ++++---- src/components/page/Navbar.jsx | 109 +++++++++++++-------------------- src/hooks/apiFetch.js | 63 +++++++++++++++++++ src/layout/Detail.jsx | 53 ++++++---------- src/layout/List.jsx | 48 ++++++--------- 6 files changed, 159 insertions(+), 141 deletions(-) create mode 100644 src/hooks/apiFetch.js diff --git a/.vscode/launch.json b/.vscode/launch.json index e6d1238..632f985 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "type": "chrome", "request": "launch", "name": "React", - "url": "http://localhost:3000/", + "url": "http://127.0.0.1:3000/", "webRoot": "${workspaceFolder}/src", "trace": true, "runtimeArgs": [ diff --git a/src/App.js b/src/App.js index 18c3fe7..602c284 100644 --- a/src/App.js +++ b/src/App.js @@ -6,15 +6,15 @@ import { createRoutesFromElements, } from 'react-router-dom' -import './index.css' - -import { ResponseException } from "./classes/Exceptions"; -import RootLayout from "./layout/Root"; +import './index.css' import Detail from "./layout/Detail"; +import ErrorPage from "./layout/Error"; +import { getCookie } from "./hooks/getCookie"; import List from "./layout/List"; +import { ResponseException } from "./classes/Exceptions"; +import RootLayout from "./layout/Root"; import Ticket from "./layout/Ticket"; -import ErrorPage from "./layout/Error"; @@ -86,17 +86,20 @@ export default App; const detailsLoader = async ({request, params}) => { - let url = 'http://localhost:8003/api/' + params.module + '/' + params.model + let loader = null - if( params.pk ) { + let url = 'http://127.0.0.1:8002/api/v2/' + params.module + '/' + params.model - url = url + '/' + params.pk + if( params.pk ) { - } + url = url + '/' + params.pk - let loader = null + } - const response = await fetch(url) + const response = await fetch(url, { + headers: {'X-CSRFToken': getCookie('csrftoken')}, + credentials: 'include' + }) .then( response => { diff --git a/src/components/page/Navbar.jsx b/src/components/page/Navbar.jsx index d724236..93ba895 100644 --- a/src/components/page/Navbar.jsx +++ b/src/components/page/Navbar.jsx @@ -1,7 +1,8 @@ -import { Link, useLocation } from "react-router-dom"; +import { Link, useLocation, useParams } from "react-router-dom"; import IconLoader from "../IconLoader"; import { useEffect, useState } from "react"; import { ResponseException } from "../../classes/Exceptions"; +import { apiFetch } from "../../hooks/apiFetch"; @@ -9,7 +10,9 @@ const Navbar = ({ nav_visible, }) => { - const [ menu_entries, SetMenuEntries ] = useState(null) + const params = useParams(); + + const [ navigation, SetNavigationEntries ] = useState(null) const [ nav_menu, setNavMenu ] = useState(null); @@ -19,31 +22,27 @@ const Navbar = ({ useEffect(() => { - fetch('http://localhost:8003/api/options/navigation') + apiFetch( + '', + (data) => { - .then(response => { + SetNavigationEntries(data.navigation) - if( ! response.ok ) { + if( params.module ){ - throw new ResponseException(response) + setNavMenu(params.module); } - return response.json() - - }) - - .then(data => { - - SetMenuEntries(data) - - }) + if( params.model ) { - .catch(err => { + setNavPage(String(params.module + '-' + params.model)) - throw Error(err) + } + }, + 'OPTIONS' + ) - }) },[]) @@ -52,25 +51,16 @@ const Navbar = ({ return (