From e1a63bbb3c82760a20bce5067684d398f44c9297 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 15 Mar 2024 13:58:36 +0100 Subject: [PATCH] exposed fields to public --- backend/app/views.py | 26 ++++++++++++++++++++++++-- src/App.js | 3 ++- src/components/LoginButton.jsx | 17 +++++++++++++++++ src/components/LogoutButton.jsx | 7 ++++++- src/components/Matos.jsx | 16 ++++++++++++++-- src/components/ModalAdd.jsx | 1 - src/components/Stats.jsx | 13 +++++++++++-- src/components/TableData.jsx | 28 ++++++++++++++++++---------- src/pages/Inventory.jsx | 4 ---- 9 files changed, 92 insertions(+), 23 deletions(-) create mode 100644 src/components/LoginButton.jsx diff --git a/backend/app/views.py b/backend/app/views.py index dbde820..ce7c9c1 100644 --- a/backend/app/views.py +++ b/backend/app/views.py @@ -108,7 +108,7 @@ def get(self, request): class ItemView(APIView): authentication_classes = (CsrfExemptSessionAuthentication,) - def get(self, request): + """ def get(self, request): auth_header = request.META.get('HTTP_AUTHORIZATION') if auth_header and auth_header.startswith('Token '): token_key = auth_header[6:] @@ -140,7 +140,29 @@ def get(self, request): except AuthToken.DoesNotExist: raise AuthenticationFailed('Invalid token') else: - raise AuthenticationFailed('Authentication credentials were not provided') + raise AuthenticationFailed('Authentication credentials were not provided')""" + + def get(self, request): + output = [ + { + "id": item.id, + "name": item.name, + "brand": item.brand, + "price": item.price, + "power": item.power, + "type": item.type, + "state": item.state, + "quantity": item.quantity, + "description" : item.description, + "modification_reason": item.modification_reason, + "creation": item.creation, + "removed": item.removed, + "modification_date": item.modification_date, + } + for item in Item.objects.all() + ] + return Response(output) + def post(self, request): authentication_classes = (CsrfExemptSessionAuthentication,) diff --git a/src/App.js b/src/App.js index 0dc8fb8..7108dc0 100644 --- a/src/App.js +++ b/src/App.js @@ -20,7 +20,8 @@ function App() { } /> } /> } /> - }/>}/> + }/> + {/*}/>}/>*/} } /> diff --git a/src/components/LoginButton.jsx b/src/components/LoginButton.jsx new file mode 100644 index 0000000..ee05ca0 --- /dev/null +++ b/src/components/LoginButton.jsx @@ -0,0 +1,17 @@ +import { useHref } from 'react-router-dom'; +import { Button } from 'semantic-ui-react'; + +function LoginButton() { + + function handleClick(){ + window.location.href = '/login'; + } + + return ( + + ); +} + +export default LoginButton; diff --git a/src/components/LogoutButton.jsx b/src/components/LogoutButton.jsx index e2c0976..8c79dca 100644 --- a/src/components/LogoutButton.jsx +++ b/src/components/LogoutButton.jsx @@ -1,9 +1,12 @@ import { Button } from 'semantic-ui-react'; import { useContext } from "react"; import { AuthContext } from "../context/AuthContext"; +import { UserContext } from "../context/UserContext"; + function LogoutButton() { const { setIsAuthenticated } = useContext(AuthContext); + const {setUserId} = useContext(UserContext); const handleLogout = async () => { try { @@ -23,8 +26,10 @@ function LogoutButton() { } // Supprimer le token du local storage localStorage.removeItem('token'); + localStorage.removeItem('user_id'); + setUserId(-1); setIsAuthenticated(false); - window.location.href = '/login'; + // window.location.href = '/login'; } catch (error) { console.error('Erreur lors de la déconnexion:', error); } diff --git a/src/components/Matos.jsx b/src/components/Matos.jsx index 3bd7319..5d20757 100644 --- a/src/components/Matos.jsx +++ b/src/components/Matos.jsx @@ -2,13 +2,19 @@ import { motion } from "framer-motion" import TableData from "./TableData"; import '../style/Matos.css' import LogoutButton from "./LogoutButton"; +import LoginButton from "./LoginButton"; import {MenuItem, Menu, MenuMenu} from "semantic-ui-react"; -import {useEffect, useState} from "react"; +import {useEffect, useState, useContext} from "react"; import HistoryTable from "./History"; +import {AuthContext} from "../context/AuthContext"; function Matos(){ + + const {isAuthenticated} = useContext(AuthContext); + + const [activeItem, setActiveItem] = useState(() => { const savedActiveItem = localStorage.getItem('activeItem'); return savedActiveItem || 'inventaire'; @@ -28,6 +34,9 @@ function Matos(){

Gestion du matériel

{/* menu*/} + + {isAuthenticated? + <> + + :null}
- + {isAuthenticated?: + }
diff --git a/src/components/ModalAdd.jsx b/src/components/ModalAdd.jsx index 84934fb..eb83749 100644 --- a/src/components/ModalAdd.jsx +++ b/src/components/ModalAdd.jsx @@ -53,7 +53,6 @@ function ModalAdd(props) { /> - ); diff --git a/src/components/Stats.jsx b/src/components/Stats.jsx index d3d71cf..510411e 100644 --- a/src/components/Stats.jsx +++ b/src/components/Stats.jsx @@ -1,12 +1,16 @@ -import React from 'react'; +import {React,useContext} from 'react'; import { StatisticValue, StatisticLabel, StatisticGroup, Statistic, } from 'semantic-ui-react'; +import {AuthContext} from "../context/AuthContext"; function Stats(props) { + const {isAuthenticated} = useContext(AuthContext); + + return (
@@ -18,7 +22,9 @@ function Stats(props) { {props.qte === null ? 0 : props.qte} Quantité totale - + {isAuthenticated? + <> + {props.total_price === null ? 0 : props.total_price} Prix total @@ -26,6 +32,9 @@ function Stats(props) { {props.power === null ? 0 : props.total_power} Puissance totale + + : null} +
); diff --git a/src/components/TableData.jsx b/src/components/TableData.jsx index b3b5329..31c6520 100644 --- a/src/components/TableData.jsx +++ b/src/components/TableData.jsx @@ -1,5 +1,6 @@ import React, {useCallback, useEffect, useRef, useState, useContext} from 'react'; import {UserContext} from "../context/UserContext"; +import {AuthContext} from "../context/AuthContext"; import * as XLSX from 'xlsx'; import { Search, @@ -21,8 +22,11 @@ import ModalSuccess from "./ModalSuccess"; import ModalFailed from "./ModalFailed"; import TableSelectedItems from "./TableSelectedItems"; + + function TableData() { const {userId} = useContext(UserContext); + const {isAuthenticated} = useContext(AuthContext); const [state, setState] = useState({ isLoading: false, results: [], value: '' }); const [tableData, setTableData] = useState([]); const [filteredData, setFilteredData] = useState([]); @@ -49,6 +53,10 @@ function TableData() { const [supp, setSuppr] = useState('Date de suppression'); + console.log(isAuthenticated) + console.log(userId) + + useEffect(() => { if (isLargeScreen) { @@ -560,10 +568,10 @@ const handleDeselectButton = () => { />