From cc7beeb982a7e26e1a232203e8a3fd971ef13c62 Mon Sep 17 00:00:00 2001 From: Benjamin POCHAT Date: Thu, 8 Aug 2024 12:49:40 +0000 Subject: [PATCH] overload async api in mock apis --- frontend/app/package-lock.json | 10 ++- frontend/app/src/api/mock/MockApi.ts | 68 ++++++++++--------- frontend/app/src/api/mock/MockApiAddresses.ts | 2 +- frontend/app/src/api/mock/MockApiCustomers.ts | 6 +- frontend/app/src/api/mock/MockApiProducers.ts | 41 ++++++----- .../app/src/api/mock/MockApiProductions.ts | 16 ++--- frontend/app/src/api/mock/MockApiSales.ts | 53 +++++++++++---- .../production/views/ProductionsList.tsx | 35 +++------- .../app/src/domains/sale/views/OrdersList.tsx | 45 ++---------- .../app/src/domains/sale/views/SalesList.tsx | 15 +++- .../producer/ProducerRouterFactory.tsx | 7 +- 11 files changed, 153 insertions(+), 145 deletions(-) diff --git a/frontend/app/package-lock.json b/frontend/app/package-lock.json index 4ee5892..19374b3 100644 --- a/frontend/app/package-lock.json +++ b/frontend/app/package-lock.json @@ -45,6 +45,12 @@ "typescript": "^5.2.2" } }, + "../gen": { + "version": "0.1.0", + "devDependencies": { + "typescript": "^4.0" + } + }, "node_modules/@aashutoshrathi/word-wrap": { "version": "1.2.6", "resolved": "https://registry.npmjs.org/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz", @@ -5429,8 +5435,8 @@ "integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==" }, "node_modules/@viandeendirect/api": { - "version": "0.1.0", - "resolved": "file:../gen" + "resolved": "../gen", + "link": true }, "node_modules/@webassemblyjs/ast": { "version": "1.11.6", diff --git a/frontend/app/src/api/mock/MockApi.ts b/frontend/app/src/api/mock/MockApi.ts index 8db232c..c5f6f24 100644 --- a/frontend/app/src/api/mock/MockApi.ts +++ b/frontend/app/src/api/mock/MockApi.ts @@ -12,68 +12,70 @@ export class MockApi { mockApiSales: MockApiSales = new MockApiSales() mockApiProducers: MockApiProducers = new MockApiProducers() - createBeefProduction(beefProduction, callback) { - callback() + createBeefProduction() { } - getProductions(options, callback) { - callback(undefined, this.mockApiProductions.getProductions()) + getProductions() { + return this.mockApiProductions.getProductions() } - getProductionPercentageSold(options, callback) { - callback(undefined, this.mockApiProductions.getProductionPercentageSold) + getProductionPercentageSold() { + return this.mockApiProductions.getProductionPercentageSold() } - getBeefProduction(id, callback) { - let mockApi = this - callback(undefined, mockApi.mockApiProductions.getBeefProduction()) + getBeefProduction() { + return this.mockApiProductions.getBeefProduction() } - getPackageTemplates(callback) { - callback(undefined, this.mockApiProductions.getPackageTemplates()) + getPackageTemplates() { + return this.mockApiProductions.getPackageTemplates() } - getAddresses(callback) { - callback(undefined, this.mockApiAddresses.getAddresses()) + getAddresses() { + return this.mockApiAddresses.getAddresses() } - createCustomer(customer, callback){ - callback(undefined, this.mockApiCustomers.createCustomer(customer)) + createCustomer(customer){ + return this.mockApiCustomers.createCustomer(customer) } - getCustomer(options, callback){ - callback(undefined, this.mockApiCustomers.getCustomer()) + getCustomer(){ + return this.mockApiCustomers.getCustomer() } - getProducerSales(options, callback) { - callback(undefined, this.mockApiProducers.getProducerSales()) + getProducerSales() { + return this.mockApiProducers.getProducerSales() } - getProducerCustomers(options, callback) { - callback(undefined, this.mockApiProducers.getCustomers()) + getProducerCustomers() { + return this.mockApiProducers.getCustomers() } - getSales(callback) { - callback(undefined, this.mockApiSales.getSales()) + getSales() { + return this.mockApiSales.getSales() } - getSaleOrders(options, callback) { - callback(undefined, this.mockApiSales.getSaleOrders()) + getSale() { + return this.mockApiSales.getSale() } - getSaleProductions(options, callback) { - callback(undefined, this.mockApiSales.getSaleProductions()) + getSaleOrders() { + return this.mockApiSales.getSaleOrders() } - getOrder(option, callback) { - callback(undefined, this.mockApiSales.getOrder()) + getSaleProductions() { + return this.mockApiSales.getSaleProductions() } - createOrder(order, callback) { - callback(undefined, this.mockApiSales.createOrder(order)) + getOrder() { + return this.mockApiSales.getOrder() } - getProducer(id, callback) { - callback(undefined, this.mockApiProducers.getProducer()) + createOrder(order) { + return this.mockApiSales.createOrder(order) + } + + getProducer() { + return this.mockApiProducers.getProducer() } } \ No newline at end of file diff --git a/frontend/app/src/api/mock/MockApiAddresses.ts b/frontend/app/src/api/mock/MockApiAddresses.ts index 0ed15b5..44bb899 100644 --- a/frontend/app/src/api/mock/MockApiAddresses.ts +++ b/frontend/app/src/api/mock/MockApiAddresses.ts @@ -1,4 +1,4 @@ -import Address from "@viandeendirect/api/dist/models/Address" +import {Address} from "@viandeendirect/api/dist/models/Address" export class MockApiAddresses { getAddresses(): Array
{ diff --git a/frontend/app/src/api/mock/MockApiCustomers.ts b/frontend/app/src/api/mock/MockApiCustomers.ts index f051083..9770426 100644 --- a/frontend/app/src/api/mock/MockApiCustomers.ts +++ b/frontend/app/src/api/mock/MockApiCustomers.ts @@ -1,8 +1,8 @@ -import Customer from "@viandeendirect/api/dist/models/Customer" +import {Customer} from "@viandeendirect/api/dist/models/Customer" export class MockApiCustomers { - getCustomer() { + getCustomer(): Customer { return { id: 1, user: { @@ -15,7 +15,7 @@ export class MockApiCustomers { //return undefined } - createCustomer(customer: Customer) { + createCustomer(customer: Customer): Customer { return {...customer, id: 999} } diff --git a/frontend/app/src/api/mock/MockApiProducers.ts b/frontend/app/src/api/mock/MockApiProducers.ts index ca68ef6..d76163d 100644 --- a/frontend/app/src/api/mock/MockApiProducers.ts +++ b/frontend/app/src/api/mock/MockApiProducers.ts @@ -1,7 +1,6 @@ -import Customer from "@viandeendirect/api/dist/models/Customer" -import Producer from "@viandeendirect/api/dist/models/Producer" -import ProducerStatus from "@viandeendirect/api/dist/models/ProducerStatus" -import Sale from "@viandeendirect/api/dist/models/Sale" +import {Customer} from "@viandeendirect/api/dist/models/Customer" +import {Producer} from "@viandeendirect/api/dist/models/Producer" +import {Sale} from "@viandeendirect/api/dist/models/Sale" export class MockApiProducers { @@ -23,10 +22,10 @@ export class MockApiProducers { } getProducerSales(): Array { - const sale1 = { + const sale1: Sale = { id: 1, - deliveryStart: '2023-11-15T18:00:00', - deliveryStop: '2023-11-15T20:00:00', + deliveryStart: new Date('2023-11-15T18:00:00'), + deliveryStop: new Date('2023-11-15T20:00:00'), deliveryAddressName: 'ESL Rémilly', deliveryAddressLine1: '1 rue De Gaulle', deliveryAddressLine2: undefined, @@ -36,7 +35,7 @@ export class MockApiProducers { { id: 1, productionType: 'BeefProduction', - slaughterDate: '2023-10-01T10:00:00', + slaughterDate: new Date('2023-10-01T10:00:00'), animalLiveWeight: 450, animalType: 'BEEF_HEIFER', animalIdentifier: '9876' @@ -44,6 +43,7 @@ export class MockApiProducers { ], orders: [ { + customer: {}, id: 11, items: [ { @@ -66,6 +66,7 @@ export class MockApiProducers { ] }, { + customer: {}, id: 12, items: [ { @@ -81,10 +82,10 @@ export class MockApiProducers { } ] } - const sale2 = { + const sale2: Sale = { id: 2, - deliveryStart: '2023-11-20T16:00:00', - deliveryStop: '2023-11-20T18:00:00', + deliveryStart: new Date('2023-11-20T16:00:00'), + deliveryStop: new Date('2023-11-20T18:00:00'), deliveryAddressName: 'Place de l\'Etoile', deliveryAddressLine1: '1 place de l\'Etoile', deliveryAddressLine2: 'Derrière l\'Arc de Triomphe', @@ -94,7 +95,7 @@ export class MockApiProducers { { id: 2, productionType: 'BeefProduction', - slaughterDate: '2023-11-01T10:00:00', + slaughterDate: new Date('2023-11-01T10:00:00'), animalLiveWeight: 400, animalType: 'BEEF_COW', animalIdentifier: '0987' @@ -102,6 +103,7 @@ export class MockApiProducers { ], orders: [ { + customer: {}, id: 21, items: [ { @@ -116,6 +118,7 @@ export class MockApiProducers { ] }, { + customer: {}, id: 22, items: [ { @@ -138,6 +141,7 @@ export class MockApiProducers { ] }, { + customer: {}, id: 23, items: [ { @@ -153,10 +157,10 @@ export class MockApiProducers { } ] } - const sale3 = { + const sale3: Sale = { id: 3, - deliveryStart: '2024-01-15T16:00:00', - deliveryStop: '2024-01-15T18:00:00', + deliveryStart: new Date('2024-01-15T16:00:00'), + deliveryStop: new Date('2024-01-15T18:00:00'), deliveryAddressName: 'Place de l\'Etoile', deliveryAddressLine1: '1 place de l\'Etoile', deliveryAddressLine2: 'Derrière l\'Arc de Triomphe', @@ -166,7 +170,7 @@ export class MockApiProducers { { id: 3, productionType: 'BeefProduction', - slaughterDate: '2024-01-01T10:00:00', + slaughterDate: new Date('2024-01-01T10:00:00'), animalLiveWeight: 400, animalType: 'BEEF_COW', animalIdentifier: '1234' @@ -174,6 +178,7 @@ export class MockApiProducers { ], orders: [ { + customer: {}, id: 31, items: [ { @@ -188,6 +193,7 @@ export class MockApiProducers { ] }, { + customer: {}, id: 32, items: [ { @@ -211,8 +217,9 @@ export class MockApiProducers { ] }, { + customer: {}, id: 33, - items: [, + items: [ { id: 331, unitPrice: 80, diff --git a/frontend/app/src/api/mock/MockApiProductions.ts b/frontend/app/src/api/mock/MockApiProductions.ts index b4ae6e1..34223d3 100644 --- a/frontend/app/src/api/mock/MockApiProductions.ts +++ b/frontend/app/src/api/mock/MockApiProductions.ts @@ -1,11 +1,11 @@ -import BeefProduction from "@viandeendirect/api/dist/models/BeefProduction" -import PackageTemplate from "@viandeendirect/api/dist/models/PackageTemplate" -import Production from "@viandeendirect/api/dist/models/Production" +import {BeefProduction} from "@viandeendirect/api/dist/models/BeefProduction" +import {PackageTemplate} from "@viandeendirect/api/dist/models/PackageTemplate" +import {Production} from "@viandeendirect/api/dist/models/Production" export class MockApiProductions { getProductions(): Array { - const production1 = { + const production1: BeefProduction = { id: 1, productionType: 'BeefProduction', producer: undefined, @@ -13,7 +13,7 @@ export class MockApiProductions { sales: undefined } - const production2 = { + const production2: BeefProduction = { id: 2, productionType: 'BeefProduction', producer: undefined, @@ -27,9 +27,9 @@ export class MockApiProductions { return { id: 1, animalIdentifier: '1234', - birthDate: '2022-10-01T10:00:00', - slaughterDate: '2023-10-01T10:00:00', - cuttingDate: '2024-10-01T10:00:00', + birthDate: new Date('2022-10-01T10:00:00'), + slaughterDate: new Date('2023-10-01T10:00:00'), + cuttingDate: new Date('2024-10-01T10:00:00'), warmCarcassWeight: 450, animalType: 'BEEF_COW', birthFarm: 'La ferme du puis', diff --git a/frontend/app/src/api/mock/MockApiSales.ts b/frontend/app/src/api/mock/MockApiSales.ts index c0fb0e8..14078d2 100644 --- a/frontend/app/src/api/mock/MockApiSales.ts +++ b/frontend/app/src/api/mock/MockApiSales.ts @@ -1,14 +1,37 @@ -import Order from "@viandeendirect/api/dist/models/Order"; -import Sale from "@viandeendirect/api/dist/models/Sale"; -import Production from "@viandeendirect/api/dist/models/Production"; +import {Order} from "@viandeendirect/api/dist/models/Order"; +import {Sale} from "@viandeendirect/api/dist/models/Sale"; +import { Production } from "@viandeendirect/api/dist/models/Production"; export class MockApiSales { + getSale(): Sale { + return { + id: 1, + deliveryStart: new Date('2023-11-15T18:00:00'), + deliveryStop: new Date('2023-11-15T20:00:00'), + deliveryAddressName: 'ESL Rémilly', + deliveryAddressLine1: '1 rue De Gaulle', + deliveryAddressLine2: undefined, + deliveryCity: 'Rémilly', + deliveryZipCode: '57580', + productions: [ + { + id: 1, + productionType: 'BeefProduction', + slaughterDate: new Date('2023-10-01T10:00:00'), + animalLiveWeight: 450, + animalType: 'BEEF_HEIFER', + animalIdentifier: '9876', + birthFarm: 'La ferme du puis' + } + ] + } + } getSales(): Array { - const sale1 = { + const sale1: Sale = { id: 1, - deliveryStart: '2023-11-15T18:00:00', - deliveryStop: '2023-11-15T20:00:00', + deliveryStart: new Date('2023-11-15T18:00:00'), + deliveryStop: new Date('2023-11-15T20:00:00'), deliveryAddressName: 'ESL Rémilly', deliveryAddressLine1: '1 rue De Gaulle', deliveryAddressLine2: undefined, @@ -18,7 +41,7 @@ export class MockApiSales { { id: 1, productionType: 'BeefProduction', - slaughterDate: '2023-10-01T10:00:00', + slaughterDate: new Date('2023-10-01T10:00:00'), animalLiveWeight: 450, animalType: 'BEEF_HEIFER', animalIdentifier: '9876', @@ -26,10 +49,10 @@ export class MockApiSales { } ] } - const sale2 = { + const sale2: Sale = { id: 2, - deliveryStart: '2023-11-20T16:00:00', - deliveryStop: '2023-11-20T18:00:00', + deliveryStart: new Date('2023-11-20T16:00:00'), + deliveryStop: new Date('2023-11-20T18:00:00'), deliveryAddressName: 'Place de l\'Etoile', deliveryAddressLine1: '1 place de l\'Etoile', deliveryAddressLine2: 'Derrière l\'Arc de Triomphe', @@ -39,7 +62,7 @@ export class MockApiSales { { id: 2, productionType: 'BeefProduction', - slaughterDate: '2023-11-01T10:00:00', + slaughterDate: new Date('2023-11-01T10:00:00'), animalLiveWeight: 400, animalType: 'BEEF_COW', animalIdentifier: '0987', @@ -47,10 +70,10 @@ export class MockApiSales { } ] } - const sale3 = { + const sale3: Sale = { id: 3, - deliveryStart: '2024-01-15T16:00:00', - deliveryStop: '2024-01-15T18:00:00', + deliveryStart: new Date('2024-01-15T16:00:00'), + deliveryStop: new Date('2024-01-15T18:00:00'), deliveryAddressName: 'Place de l\'Etoile', deliveryAddressLine1: '1 place de l\'Etoile', deliveryAddressLine2: 'Derrière l\'Arc de Triomphe', @@ -60,7 +83,7 @@ export class MockApiSales { { id: 3, productionType: 'BeefProduction', - slaughterDate: '2024-01-01T10:00:00', + slaughterDate: new Date('2024-01-01T10:00:00'), animalLiveWeight: 400, animalType: 'BEEF_COW', animalIdentifier: '1234', diff --git a/frontend/app/src/domains/production/views/ProductionsList.tsx b/frontend/app/src/domains/production/views/ProductionsList.tsx index 55e61ec..7ca200e 100644 --- a/frontend/app/src/domains/production/views/ProductionsList.tsx +++ b/frontend/app/src/domains/production/views/ProductionsList.tsx @@ -1,39 +1,19 @@ import React from 'react' -import { useEffect, useState } from 'react' -import { useKeycloak } from '@react-keycloak/web' import { ApiBuilder } from '../../../api/ApiBuilder.ts' import { Button, Typography } from "@mui/material" import ProductionCard from '../components/ProductionCard.tsx' -import { useNavigate } from 'react-router-dom' +import { useLoaderData, useNavigate } from 'react-router-dom' +import { Production } from '@viandeendirect/api/dist/models/Production' export default function ProductionsList() { - const [productions, setProductions] = useState([]) - const { keycloak, initialized } = useKeycloak() + const productions: Array = useLoaderData() + const navigate = useNavigate() - const apiBuilder = new ApiBuilder() - useEffect(() => { - loadProductions() - }, [keycloak]) - - function loadProductions() { - apiBuilder.getAuthenticatedApi(keycloak).then(api => { - apiBuilder.invokeAuthenticatedApi(() => { - api.getProductions({}, (error, data, response) => { - if (error) { - console.error(error) - } else { - console.log('api.getProductions called successfully. Returned data: ' + data) - setProductions(data) - } - }) - }, keycloak) - }) - } return <> Productions @@ -52,4 +32,11 @@ export default function ProductionsList() { ) } +} + +export async function loadProductionListData(keycloakClient): Promise> { + const apiBuilder = new ApiBuilder() + const api = await apiBuilder.getAuthenticatedApi(keycloakClient) + const productions: Array = await api.getProductions() + return productions } \ No newline at end of file diff --git a/frontend/app/src/domains/sale/views/OrdersList.tsx b/frontend/app/src/domains/sale/views/OrdersList.tsx index 3df6d20..25842c6 100644 --- a/frontend/app/src/domains/sale/views/OrdersList.tsx +++ b/frontend/app/src/domains/sale/views/OrdersList.tsx @@ -1,57 +1,26 @@ import React from 'react' -import { useEffect, useState } from 'react' +import { useState } from 'react' import { Button, ButtonGroup, Switch, Typography } from "@mui/material" import { DataGrid, GridRowsProp, GridColDef, GridToolbar } from '@mui/x-data-grid'; import dayjs from 'dayjs' -import { useKeycloak } from '@react-keycloak/web' import { ApiBuilder } from '../../../api/ApiBuilder.ts' import { Order } from "@viandeendirect/api/dist/models/Order" -import { Sale } from "@viandeendirect/api/dist/models/Sale" import { OrderStatus, OrderStatusUtils } from '../../../enum/OrderStatus.ts'; import { useLoaderData, useNavigate } from 'react-router-dom'; -import { DefaultApi } from '@viandeendirect/api/dist/apis/DefaultApi'; -import { Configuration } from '@viandeendirect/api/dist/runtime'; +import { Sale } from '@viandeendirect/api/dist/models/Sale'; export default function OrdersList() { const navigate = useNavigate() - const loaderData = useLoaderData() - const sale = loaderData.sale - const orders = loaderData.orders - - /* - const [sale, setSale] = useState({id: props.saleId}) - - const [orders, setOrders] = useState([]) - - useEffect(() => { - //TODO : load sale from props.saleId - loadOrders() - }, [keycloak]) - */ + const data = useLoaderData() + const sale = data.sale + const orders = data.orders const [abortedOrdersHidden, setAbortedOrdersHidden] = useState(true); - /* - function loadOrders() { - apiBuilder.getAuthenticatedApi(keycloak).then(api => { - apiBuilder.invokeAuthenticatedApi(() => { - api.getSaleOrders(sale.id, (error, data, response) => { - if (error) { - console.error(error) - } else { - console.log('api.getSaleOrders called successfully. Returned data: ' + data) - setOrders(data) - } - }) - }, keycloak) - }) - } - */ - const columns: GridColDef[] = [ { field: 'id', headerName: 'Référence', flex: 0.5, disableColumnMenu: true }, { field: 'customerName', headerName: 'Nom du client', flex: 1, disableColumnMenu: true }, @@ -107,10 +76,10 @@ export default function OrdersList() { } -export async function loadOrdersListData(saleId, keycloakClient) { +export async function loadOrdersListData(saleId: number, keycloakClient): Promise<{orders: Array, sale: Sale}> { const apiBuilder = new ApiBuilder() const api = await apiBuilder.getAuthenticatedApi(keycloakClient) - const orders = await api.getSaleOrders({saleId: saleId}) + const orders: Array = await api.getSaleOrders({saleId: saleId}) const sale = await api.getSale({saleId: saleId}) return {orders: orders, sale: sale} } diff --git a/frontend/app/src/domains/sale/views/SalesList.tsx b/frontend/app/src/domains/sale/views/SalesList.tsx index b20aaf6..0d9abbe 100644 --- a/frontend/app/src/domains/sale/views/SalesList.tsx +++ b/frontend/app/src/domains/sale/views/SalesList.tsx @@ -4,11 +4,15 @@ import { Typography, Button } from "@mui/material" import { useKeycloak } from '@react-keycloak/web' -import Producer from "@viandeendirect/api/dist/models/Producer.js"; +import {Producer} from "@viandeendirect/api/dist/models/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' +import { ApiBuilder } from '../../../api/ApiBuilder.ts'; +import { Sale } from '@viandeendirect/api/dist/models/Sale'; + +//TODO : finir la conversion export default function SalesList() { @@ -35,4 +39,13 @@ export default function SalesList() { +} + +export async function loadSalesListData(keycloakClient): Promise<{sales: Array, producer: Producer}> { + const apiBuilder = new ApiBuilder() + const api = await apiBuilder.getAuthenticatedApi(keycloakClient) + const sales: Array = await api.getSales() + const producerService = new ProducerService(keycloakClient) + const producer: Producer = await producerService.loadProducer() + return {sales: sales, producer: producer} } \ 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 b85781c..d0ebf83 100644 --- a/frontend/app/src/layouts/producer/ProducerRouterFactory.tsx +++ b/frontend/app/src/layouts/producer/ProducerRouterFactory.tsx @@ -10,7 +10,7 @@ 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 ProductionsList, { loadProductionListData } from "../../domains/production/views/ProductionsList.tsx"; import SalesList from "../../domains/sale/views/SalesList.tsx"; import SaleForm from "../../domains/sale/views/SaleForm.tsx"; import OrdersList, { loadOrdersListData } from "../../domains/sale/views/OrdersList.tsx"; @@ -34,7 +34,8 @@ export class ProducerRouterFactory { }, { path: '/productions', - element: + element: , + loader: async () => loadProductionListData(keycloakClient) }, { path: '/beefProduction/:beefProductionId', @@ -55,7 +56,7 @@ export class ProducerRouterFactory { { path: '/sale/:saleId/orders', element: , - loader: async ({params}) => loadOrdersListData(params.saleId, keycloakClient) + loader: async ({params}) => loadOrdersListData(+params.saleId, keycloakClient) }, { path: '/sale/:saleId/order/:orderId',