Skip to content

Commit

Permalink
exposed fields to public
Browse files Browse the repository at this point in the history
  • Loading branch information
leopoldch committed Mar 15, 2024
1 parent 0642b3b commit e1a63bb
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 23 deletions.
26 changes: 24 additions & 2 deletions backend/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
Expand Down Expand Up @@ -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,)
Expand Down
3 changes: 2 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ function App() {
<Route path="/" element={<Site />} />
<Route path="legals" element={<MentionLegals />} />
<Route path="login" element={<Login />} />
<Route path="/inventory" element={<ProtectedRoute element={<Inventory/>}/>}/>
<Route path="/inventory" element={<Inventory/>}/>
{/*<Route path="/inventory" element={<ProtectedRoute element={<Inventory/>}/>}/>*/}
<Route path="*" element={<NoSite />} />
</Routes>
</BrowserRouter>
Expand Down
17 changes: 17 additions & 0 deletions src/components/LoginButton.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<Button onClick={handleClick} color='green'>
Login
</Button>
);
}

export default LoginButton;
7 changes: 6 additions & 1 deletion src/components/LogoutButton.jsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}
Expand Down
16 changes: 14 additions & 2 deletions src/components/Matos.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -28,6 +34,9 @@ function Matos(){
<h1>Gestion du matériel</h1>
{/* menu*/}
<Menu secondary>

{isAuthenticated?
<>
<MenuItem
name='inventaire'
active={activeItem === 'inventaire'}
Expand All @@ -38,9 +47,12 @@ function Matos(){
active={activeItem === 'historique'}
onClick={handleItemClick}
/>
</>
:null}
</Menu>
<div>
<LogoutButton/>
{isAuthenticated?<LogoutButton/>:<LoginButton/>
}
</div>

</div>
Expand Down
1 change: 0 additions & 1 deletion src/components/ModalAdd.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ function ModalAdd(props) {
/>
</div>
</ModalContent>

</Modal>
</>
);
Expand Down
13 changes: 11 additions & 2 deletions src/components/Stats.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>

Expand All @@ -18,14 +22,19 @@ function Stats(props) {
<StatisticValue>{props.qte === null ? 0 : props.qte}</StatisticValue>
<StatisticLabel>Quantité totale</StatisticLabel>
</Statistic>
<Statistic>
{isAuthenticated?
<>
<Statistic>
<StatisticValue>{props.total_price === null ? 0 : props.total_price}</StatisticValue>
<StatisticLabel>Prix total</StatisticLabel>
</Statistic>
<Statistic>
<StatisticValue>{props.power === null ? 0 : props.total_power}</StatisticValue>
<StatisticLabel>Puissance totale</StatisticLabel>
</Statistic>
</>
: null}


</div>
);
Expand Down
28 changes: 18 additions & 10 deletions src/components/TableData.jsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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([]);
Expand All @@ -49,6 +53,10 @@ function TableData() {
const [supp, setSuppr] = useState('Date de suppression');


console.log(isAuthenticated)
console.log(userId)



useEffect(() => {
if (isLargeScreen) {
Expand Down Expand Up @@ -560,10 +568,10 @@ const handleDeselectButton = () => {
/>
<Button icon={"delete"} onClick={() => handleResetFilterState()} />
</div>
{showDeleted || userId === 2 ? null:
{!showDeleted && userId !== 2 && isAuthenticated?
<div className={""}>
<ModalAdd submission={handleSubmission} />
</div>}
</div>:null}
</div>
<Button content='Export' onClick={exportToExcel} />
</div>
Expand All @@ -573,7 +581,7 @@ const handleDeselectButton = () => {
<Table striped sortable fixed unstackable>
<TableHeader>
<TableRow>
{showDeleted ? null : <TableHeaderCell>{selection}</TableHeaderCell> }
{!showDeleted && isAuthenticated ? <TableHeaderCell>{selection}</TableHeaderCell> : null}

<TableHeaderCell sorted={column === 'name' ? direction : null} onClick={() => dispatch({ type: 'CHANGE_SORT', column: 'name' })}>{ref}</TableHeaderCell>
{isLargeScreen &&
Expand All @@ -592,17 +600,17 @@ const handleDeselectButton = () => {

null
}
{!showDeleted && userId!==2 &&isLargeScreen ? <>
{!showDeleted && userId !== 2 && isLargeScreen ? <>
<TableHeaderCell sorted={column === 'modification_date' ? direction : null} onClick={() => dispatch({ type: 'CHANGE_SORT', column: 'modification_date' })}>Date de modification</TableHeaderCell>
</> :
null
}
{!showDeleted && userId!==2 ? <>
{ (!showDeleted && userId !== 2 && isAuthenticated) ? <>
<TableHeaderCell>{action}</TableHeaderCell>
</> :
null
}
{!showDeleted && userId===2 &&isLargeScreen ? <>
{!showDeleted && userId === 2 && isLargeScreen ? <>
<TableHeaderCell sorted={column === 'modification_date' ? direction : null} onClick={() => dispatch({ type: 'CHANGE_SORT', column: 'modification_date' })}>Date de modification</TableHeaderCell>

</> :
Expand All @@ -614,9 +622,9 @@ const handleDeselectButton = () => {
<TableBody>
{sort_state.data.map((item, index) => (
<TableRow key={index}>
{showDeleted ? null : <TableCell><Checkbox
{!showDeleted && isAuthenticated ? <TableCell><Checkbox
checked={!!checkedItems[item.id]}
onChange={() => handleCheckboxChange(item.id)} /></TableCell> }
onChange={() => handleCheckboxChange(item.id)} /></TableCell> : null}
<TableCell>{item.name}</TableCell>
{isLargeScreen && <>
<TableCell>{item.brand}</TableCell>
Expand All @@ -637,15 +645,15 @@ const handleDeselectButton = () => {
<TableCell>{convertDateFormat(item.modification_date)}</TableCell>
</> : null
}
{!showDeleted && userId !==2 ?
{!showDeleted && userId !==2 && isAuthenticated ?
<>
<TableCell>
<ModalEdit submission={handleSubmissionEdit} item_id={item.id} reason={item.modification_reason} state={item.state} power={item.power} name={item.name} brand={item.brand} type={item.type} description={item.description} price={item.price} quantity={item.quantity} date={item.creation} />
<ModalDelete submission={handleSubmission} item_id={item.id} reason={item.modification_reason} state={item.state} power={item.power} name={item.name} brand={item.brand} type={item.type} description={item.description} price={item.price} quantity={item.quantity} date={item.creation} />
</TableCell>
</> : null
}
{!showDeleted && userId ===2 &&isLargeScreen ? <>
{!showDeleted && userId ===2 && isLargeScreen ? <>
<TableCell>{convertDateFormat(item.modification_date)}</TableCell>
</> : null
}
Expand Down
4 changes: 0 additions & 4 deletions src/pages/Inventory.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ function Inventory(){
<Matos/>
</div>
</body>



<Footer/>

</>
);
}
Expand Down

0 comments on commit e1a63bb

Please sign in to comment.