From b02d41649e4c2fbabe43e6c917b1dfaf22723a78 Mon Sep 17 00:00:00 2001 From: Benjamin POCHAT Date: Wed, 7 Aug 2024 07:44:33 +0200 Subject: [PATCH] generalize routing --- .../production/ProductionController.tsx | 33 ------- .../components/BeefProductionCard.tsx | 6 +- .../production/views/ProductionsList.tsx | 10 +-- .../beefProduction/BeefProductionCreator.tsx | 8 +- .../beefProduction/BeefProductionView.tsx | 9 +- .../src/domains/sale/components/SaleCard.tsx | 4 +- .../sale/components/SaleProductionSelector.js | 2 +- .../app/src/domains/sale/views/OrdersList.tsx | 21 ++--- .../app/src/domains/sale/views/SaleForm.tsx | 10 ++- .../app/src/domains/sale/views/SalesList.tsx | 18 +++- .../producer/ProducerRouterFactory.tsx | 90 ++++++++++++------- 11 files changed, 109 insertions(+), 102 deletions(-) delete mode 100644 frontend/app/src/domains/production/ProductionController.tsx diff --git a/frontend/app/src/domains/production/ProductionController.tsx b/frontend/app/src/domains/production/ProductionController.tsx deleted file mode 100644 index d64e93c..0000000 --- a/frontend/app/src/domains/production/ProductionController.tsx +++ /dev/null @@ -1,33 +0,0 @@ -import React from 'react' -import { useState } from 'react' -import BeefProductionCreator from './views/beefProduction/BeefProductionCreator.tsx' -import PackageLotsCreator from './views/PackageLotsCreator.tsx' -import ProductionsList from './views/ProductionsList.tsx' -import BeefProductionView from './views/beefProduction/BeefProductionView.tsx' -import AuthenticatedLayout from '../../layouts/producer/AuthenticatedLayout.tsx' - -export default function ProductionController({producer: producer}) { - - const BEEF_PRODUCTION_CREATION = 'BEEF_PRODUCTION_CREATION' - const BEEF_PRODUCTION_VIEW = 'BEEF_PRODUCTION_VIEW' - const PRODUCTIONS_LIST = 'PRODUCTIONS_LIST' - - const [currentAction, setCurrentAction] = useState(PRODUCTIONS_LIST) - const [context, setContext] = useState(undefined) - - return <>{getContent()} - - function getContent() { - switch (currentAction) { - case PRODUCTIONS_LIST: return setCurrentAction(BEEF_PRODUCTION_CREATION)} - viewBeefProductionCallback={(production) => { - setContext(production) - setCurrentAction(BEEF_PRODUCTION_VIEW)}}/> - case BEEF_PRODUCTION_CREATION: return {setCurrentAction(action)}} /> - case BEEF_PRODUCTION_VIEW: return { - setCurrentAction(PRODUCTIONS_LIST) - setContext(undefined)}}/> - } - } -} \ No newline at end of file diff --git a/frontend/app/src/domains/production/components/BeefProductionCard.tsx b/frontend/app/src/domains/production/components/BeefProductionCard.tsx index e73675c..d5e309b 100644 --- a/frontend/app/src/domains/production/components/BeefProductionCard.tsx +++ b/frontend/app/src/domains/production/components/BeefProductionCard.tsx @@ -8,8 +8,8 @@ import { useNavigate } from 'react-router-dom'; export default function BeefProductionCard({ production: production, - showActions: showActions, - clickCallback: clickCallback}) { + showActions: showActions, + onClick: onClick = undefined}) { const navigate = useNavigate() const [beefProduction, setBeefProduction] = useState(production) @@ -33,7 +33,7 @@ export default function BeefProductionCard({ return ( - navigate(`/beefProduction/${production.id}`)}> + navigate(`/beefProduction/${production.id}`))}> Abattage bovin diff --git a/frontend/app/src/domains/production/views/ProductionsList.tsx b/frontend/app/src/domains/production/views/ProductionsList.tsx index 569b7b3..55e61ec 100644 --- a/frontend/app/src/domains/production/views/ProductionsList.tsx +++ b/frontend/app/src/domains/production/views/ProductionsList.tsx @@ -9,12 +9,11 @@ import { Button, Typography } from "@mui/material" import ProductionCard from '../components/ProductionCard.tsx' import { useNavigate } from 'react-router-dom' -export default function ProductionsList({ - createBeefProductionCallback: createBeefProductionCallback, - viewBeefProductionCallback: viewBeefProductionCallback}) { +export default function ProductionsList() { const [productions, setProductions] = useState([]) const { keycloak, initialized } = useKeycloak() + const navigate = useNavigate() const apiBuilder = new ApiBuilder() useEffect(() => { @@ -41,7 +40,7 @@ export default function ProductionsList({
{getProductionCards()}
- + @@ -49,8 +48,7 @@ export default function ProductionsList({ return productions.map(production =>
+ showActions={true} >
) } diff --git a/frontend/app/src/domains/production/views/beefProduction/BeefProductionCreator.tsx b/frontend/app/src/domains/production/views/beefProduction/BeefProductionCreator.tsx index 957ece2..8225700 100644 --- a/frontend/app/src/domains/production/views/beefProduction/BeefProductionCreator.tsx +++ b/frontend/app/src/domains/production/views/beefProduction/BeefProductionCreator.tsx @@ -14,8 +14,9 @@ import CuttingPropertiesForm, { mapCuttingFormDataToBeefProduction } from "./for import { BeefProductionService } from "../../service/BeefProductionService.ts" import BeefProduction from "viandeendirect_eu/dist/model/BeefProduction.js" import PackageLot from "viandeendirect_eu/dist/model/PackageLot.js" +import { useNavigate } from "react-router-dom" -export default function BeefProductionCreator({ callback }) { +export default function BeefProductionCreator() { const BREEDING_PROPERTIES_STEP = 0 const SLAUGHTER_PROPERTIES_STEP = 1 @@ -23,6 +24,7 @@ export default function BeefProductionCreator({ callback }) { const PRODUCTS_STEP = 3 const { keycloak } = useKeycloak() + const navigate = useNavigate() const [ activeStep, setActiveStep ] = useState(BREEDING_PROPERTIES_STEP) const [ beefProduction, setBeefProduction] = useState({ productionType: "BeefProduction"}) const [ completedSteps, setCompletedSteps] = useState>([]) @@ -170,7 +172,7 @@ export default function BeefProductionCreator({ callback }) { console.error(error) } else { console.log('API called successfully. Returned data: ' + data) - callback('PRODUCTIONS_LIST') + navigate(-1) } }) }, keycloak) @@ -178,6 +180,6 @@ export default function BeefProductionCreator({ callback }) { } function cancel() { - callback('PRODUCTIONS_LIST') + navigate(-1) } } diff --git a/frontend/app/src/domains/production/views/beefProduction/BeefProductionView.tsx b/frontend/app/src/domains/production/views/beefProduction/BeefProductionView.tsx index c00363d..50a845f 100644 --- a/frontend/app/src/domains/production/views/beefProduction/BeefProductionView.tsx +++ b/frontend/app/src/domains/production/views/beefProduction/BeefProductionView.tsx @@ -10,7 +10,7 @@ import BeefProduction from "viandeendirect_eu/dist/model/BeefProduction.js" import PackageLotsCreator from '../PackageLotsCreator.tsx' import { ApiInvoker } from '../../../../api/ApiInvoker.ts' import { useKeycloak } from '@react-keycloak/web' -import { useParams } from 'react-router-dom' +import { useNavigate, useParams } from 'react-router-dom' export default function BeefProductionView() { @@ -21,10 +21,11 @@ export default function BeefProductionView() { const apiInvoker = new ApiInvoker() const { keycloak } = useKeycloak() + const navigate = useNavigate(); const [currentTab, setCurrentTab] = useState(BREEDING_PROPERTIES_TAB) const [readOnly, setReadOnly] = useState(true) - const [production, setProduction] = useState() + const [production, setProduction] = useState({}) let { beefProductionId } = useParams() @@ -37,8 +38,6 @@ export default function BeefProductionView() { console.error) }, [keycloak]) - - const [saveEnabled, setSaveEnabled] = useState(true) const [alerts, setAlerts] = useState(undefined) @@ -104,7 +103,7 @@ export default function BeefProductionView() { function getButtons() { if (readOnly) { return - + } diff --git a/frontend/app/src/domains/sale/components/SaleCard.tsx b/frontend/app/src/domains/sale/components/SaleCard.tsx index 6e6ac8d..a1a665c 100644 --- a/frontend/app/src/domains/sale/components/SaleCard.tsx +++ b/frontend/app/src/domains/sale/components/SaleCard.tsx @@ -7,6 +7,7 @@ import { ApiInvoker } from '../../../api/ApiInvoker.ts'; import { useKeycloak } from '@react-keycloak/web'; import Order from 'viandeendirect_eu/dist/model/Order' import Production from 'viandeendirect_eu/dist/model/Production' +import { useNavigate } from 'react-router-dom'; export default function SaleCard({ sale: sale, manageOrdersCallback: manageOrdersCallback}) { @@ -15,6 +16,7 @@ export default function SaleCard({ sale: sale, manageOrdersCallback: manageOrder const [orders, setOrders] = useState>([]) const [productions, setProductions] = useState>([]) const {keycloak} = useKeycloak() + const navigate = useNavigate() useEffect(() => { apiInvoker.callApiAuthenticatedly(keycloak, api => api.getSaleOrders, sale.id, setOrders, console.error) @@ -69,7 +71,7 @@ export default function SaleCard({ sale: sale, manageOrdersCallback: manageOrder - + diff --git a/frontend/app/src/domains/sale/components/SaleProductionSelector.js b/frontend/app/src/domains/sale/components/SaleProductionSelector.js index 25e636b..0f2e9ea 100644 --- a/frontend/app/src/domains/sale/components/SaleProductionSelector.js +++ b/frontend/app/src/domains/sale/components/SaleProductionSelector.js @@ -38,7 +38,7 @@ export default function SaleProductionSelector({selectProduction: selectProducti key={'production-card-' + production.id} production={production} showActions={false} - clickCallback={() => handleProductSelection(production)}> + onClick={() => handleProductSelection(production)}> }) diff --git a/frontend/app/src/domains/sale/views/OrdersList.tsx b/frontend/app/src/domains/sale/views/OrdersList.tsx index 9cf0533..0943d2c 100644 --- a/frontend/app/src/domains/sale/views/OrdersList.tsx +++ b/frontend/app/src/domains/sale/views/OrdersList.tsx @@ -8,21 +8,22 @@ import dayjs from 'dayjs' import { useKeycloak } from '@react-keycloak/web' import { ApiBuilder } from '../../../api/ApiBuilder.ts' -import Order from "viandeendirect_eu/dist/model/Order" +import Order from "viandeendirect_eu/dist/model/Order.js" +import Sale from "viandeendirect_eu/dist/model/Sale.js" import { OrderStatus, OrderStatusUtils } from '../../../enum/OrderStatus.ts'; +import { useNavigate } from 'react-router-dom'; -export default function OrdersList({ - sale: sale, - returnCallback: returnCallback, - viewOrderCallback: viewOrderCallback, - createOrderCallback: createOrderCallback}) { +export default function OrdersList(props) { const { keycloak, initialized } = useKeycloak() + const navigate = useNavigate() const apiBuilder = new ApiBuilder() + const [sale, setSale] = useState({id: props.saleId}) const [orders, setOrders] = useState([]) useEffect(() => { + //TODO : load sale from props.saleId loadOrders() }, [keycloak]) @@ -50,7 +51,7 @@ export default function OrdersList({ { field: 'actions', headerName: '', - renderCell: (param) => , + renderCell: (param) => , disableColumnMenu: true, disableReorder: true } @@ -72,7 +73,7 @@ export default function OrdersList({ } return <> - Commandes pour la vente du {dayjs(sale.deliveryStart).format('DD/MM/YYYY')} - {sale.deliveryAddressName} + Commandes pour la vente du {dayjs(sale?.deliveryStart).format('DD/MM/YYYY')} - {sale?.deliveryAddressName} - - + + } diff --git a/frontend/app/src/domains/sale/views/SaleForm.tsx b/frontend/app/src/domains/sale/views/SaleForm.tsx index 26b6b34..e59c7a1 100644 --- a/frontend/app/src/domains/sale/views/SaleForm.tsx +++ b/frontend/app/src/domains/sale/views/SaleForm.tsx @@ -10,6 +10,7 @@ import Sale from 'viandeendirect_eu/dist/model/Sale' import SaleProductionSelector from '../components/SaleProductionSelector.js' import 'dayjs/locale/fr'; import dayjs from 'dayjs' +import { useNavigate } from 'react-router-dom' const steps = ['Choisir une production', 'Définir le lieu et l\'heure', 'Choisir les produits mis en vente'] @@ -17,7 +18,7 @@ const steps = ['Choisir une production', 'Définir le lieu et l\'heure', 'Choisi * @param {Production} production * @returns */ -export default function SaleForm({producer: producer, returnCallback: returnCallback}) { +export default function SaleForm({producer: producer}) { const SELECT_PRODUCTION_STEP = 'SELECT_PRODUCTION_STEP' const SET_DELIVERY_DATE_STEP = 'SET_DELIVERY_DATE_STEP' @@ -25,6 +26,7 @@ export default function SaleForm({producer: producer, returnCallback: returnCall const CONFIRMATION_STEP = 'CONFIRMATION_STEP' const { keycloak, initialized } = useKeycloak() + const navigate = useNavigate() const [activeStep, setActiveStep] = useState(SELECT_PRODUCTION_STEP) const [sale, setSale] = useState(new Sale()) const [addresses, setAddresses] = useState([]) @@ -97,7 +99,7 @@ export default function SaleForm({producer: producer, returnCallback: returnCall
- +
@@ -195,7 +197,7 @@ export default function SaleForm({producer: producer, returnCallback: returnCall console.error(error) } else { console.log('API called successfully. Returned data: ' + data) - returnCallback() + navigate(-1) } }) }, keycloak) @@ -203,6 +205,6 @@ export default function SaleForm({producer: producer, returnCallback: returnCall } function cancel() { - returnCallback() + navigate(-1) } } \ No newline at end of file diff --git a/frontend/app/src/domains/sale/views/SalesList.tsx b/frontend/app/src/domains/sale/views/SalesList.tsx index 7850f56..c2d06f3 100644 --- a/frontend/app/src/domains/sale/views/SalesList.tsx +++ b/frontend/app/src/domains/sale/views/SalesList.tsx @@ -4,25 +4,35 @@ import { Typography, Button } from "@mui/material" import { useKeycloak } from '@react-keycloak/web' +import Producer from "viandeendirect_eu/dist/model/Producer.js"; import SaleCard from '../components/SaleCard.tsx' import { ApiInvoker } from '../../../api/ApiInvoker.ts' +import { useNavigate } from 'react-router-dom' +import { ProducerService } from '../../commons/service/ProducerService.ts' -export default function SalesList({producer: producer, manageSaleOrdersCallback: manageSaleOrdersCallback, createSaleCallback: createSaleCallback}) { +export default function SalesList() { const { keycloak } = useKeycloak() + const navigate = useNavigate() + const producerService = new ProducerService(keycloak) const [sales, setSales] = useState([]) + const [producer, setProducer] = useState({}) const apiInvoker = new ApiInvoker() useEffect(() => { - apiInvoker.callApiAuthenticatedly(keycloak, api => api.getProducerSales, producer.id, setSales, console.error) + producerService.loadProducer(producer => { + setProducer(producer) + apiInvoker.callApiAuthenticatedly(keycloak, api => api.getProducerSales, producer.id, setSales, console.error) + } + ) }, [keycloak, producer]) return <> Ventes
- {sales.map(sale => )} + {sales.map(sale => )}
- + } \ No newline at end of file diff --git a/frontend/app/src/layouts/producer/ProducerRouterFactory.tsx b/frontend/app/src/layouts/producer/ProducerRouterFactory.tsx index 01eed0d..801fef8 100644 --- a/frontend/app/src/layouts/producer/ProducerRouterFactory.tsx +++ b/frontend/app/src/layouts/producer/ProducerRouterFactory.tsx @@ -2,7 +2,6 @@ import React from "react"; import { createBrowserRouter, Navigate } from "react-router-dom"; import AuthenticatedLayout from "./AuthenticatedLayout.tsx"; -import ProductionController from "../../domains/production/ProductionController.tsx"; import CustomerController from "../../domains/customer/CustomerController.tsx"; import ProducerController from "../../domains/producer/ProducerController.tsx"; import SaleController from "../../domains/sale/SaleController.tsx"; @@ -10,6 +9,13 @@ import Dashboard from "../../domains/dashboard/views/Dashboard.tsx"; import AnonymousLayout from "./AnonymousLayout.tsx"; import NotAuthorizedForCustomers from "../../authentication/views/NotAuthorizedForCustomers.tsx"; import BeefProductionView from "../../domains/production/views/beefProduction/BeefProductionView.tsx"; +import BeefProductionCreator from "../../domains/production/views/beefProduction/BeefProductionCreator.tsx"; +import ProductionsList from "../../domains/production/views/ProductionsList.tsx"; +import SalesList from "../../domains/sale/views/SalesList.tsx"; +import SaleForm from "../../domains/sale/views/SaleForm.tsx"; +import OrdersList from "../../domains/sale/views/OrdersList.tsx"; +import OrderView from "../../domains/sale/views/OrderView.tsx"; +import ProducerOrderForm from "../../domains/sale/views/ProducerOrderForm.tsx"; export class ProducerRouterFactory { getRouter() { @@ -18,42 +24,62 @@ export class ProducerRouterFactory { path: "/", element: , children: [ - { - index: true, - element: - }, - { - path: "/dashboard", - element: - }, - { - path: "/productions", - element: - }, - { - path: "/sales", - element: - }, - { - path: "/customers", - element: - }, - { - path: "/account", - element: - }, - { - path: "/beefProduction/:beefProductionId", - element: - } - ] + { + index: true, + element: + }, + { + path: '/dashboard', + element: + }, + { + path: '/productions', + element: + }, + { + path: '/beefProduction/:beefProductionId', + element: + }, + { + path: '/beefProduction/creation', + element: + }, + { + path: '/sales', + element: + }, + { + path: '/sales/creation', + element: + }, + { + path: '/sale/:saleId/orders', + element: + }, + { + path: '/sale/:saleId/order/:orderId', + element: + }, + { + path: '/sale/:saleId/order/creation', + element: + }, + { + path: '/customers', + element: + }, + { + path: '/account', + element: + } + ] }, { - path: "/authentication", + path: '/authentication', element: }, { - path: "/unauthorized", + path: '/unauthorized', element: } ])