Skip to content

Commit

Permalink
Page d'accueil producteur #35
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminpochat committed Sep 1, 2024
1 parent 2751ad0 commit a31d597
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 21 deletions.
18 changes: 9 additions & 9 deletions frontend/app/src/api/mock/MockApiProducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class MockApiProducers {
getProducerSales(): Array<Sale> {
const sale1: Sale = {
id: 1,
deliveryStart: new Date('2023-11-15T18:00:00'),
deliveryStop: new Date('2023-11-15T20:00:00'),
deliveryStart: new Date('2025-11-15T18:00:00'),
deliveryStop: new Date('2025-11-15T20:00:00'),
deliveryAddressName: 'ESL Rémilly',
deliveryAddressLine1: '1 rue De Gaulle',
deliveryAddressLine2: undefined,
Expand All @@ -44,7 +44,7 @@ export class MockApiProducers {
{
id: 1,
productionType: 'BeefProduction',
slaughterDate: new Date('2023-10-01T10:00:00'),
slaughterDate: new Date('2025-10-01T10:00:00'),
animalLiveWeight: 450,
animalType: 'BEEF_HEIFER',
animalIdentifier: '9876'
Expand Down Expand Up @@ -93,8 +93,8 @@ export class MockApiProducers {
}
const sale2: Sale = {
id: 2,
deliveryStart: new Date('2023-11-20T16:00:00'),
deliveryStop: new Date('2023-11-20T18:00:00'),
deliveryStart: new Date('2024-11-20T16:00:00'),
deliveryStop: new Date('2024-11-20T18:00:00'),
deliveryAddressName: 'Place de l\'Etoile',
deliveryAddressLine1: '1 place de l\'Etoile',
deliveryAddressLine2: 'Derrière l\'Arc de Triomphe',
Expand All @@ -104,7 +104,7 @@ export class MockApiProducers {
{
id: 2,
productionType: 'BeefProduction',
slaughterDate: new Date('2023-11-01T10:00:00'),
slaughterDate: new Date('2024-11-01T10:00:00'),
animalLiveWeight: 400,
animalType: 'BEEF_COW',
animalIdentifier: '0987'
Expand Down Expand Up @@ -168,8 +168,8 @@ export class MockApiProducers {
}
const sale3: Sale = {
id: 3,
deliveryStart: new Date('2024-01-15T16:00:00'),
deliveryStop: new Date('2024-01-15T18:00:00'),
deliveryStart: new Date('2025-01-15T16:00:00'),
deliveryStop: new Date('2025-01-15T18:00:00'),
deliveryAddressName: 'Place de l\'Etoile',
deliveryAddressLine1: '1 place de l\'Etoile',
deliveryAddressLine2: 'Derrière l\'Arc de Triomphe',
Expand All @@ -179,7 +179,7 @@ export class MockApiProducers {
{
id: 3,
productionType: 'BeefProduction',
slaughterDate: new Date('2024-01-01T10:00:00'),
slaughterDate: new Date('2025-01-01T10:00:00'),
animalLiveWeight: 400,
animalType: 'BEEF_COW',
animalIdentifier: '1234'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { Button, Typography } from "@mui/material"
import { Producer } from '@viandeendirect/api/dist/models/Producer.js'
import { useKeycloak } from '@react-keycloak/web'
import { ProducerService } from '../../commons/service/ProducerService.ts'
import { useNavigate } from 'react-router-dom'


function DashboardAccount() {

const { keycloak } = useKeycloak()
const producerService = new ProducerService(keycloak)
const [producer, setProducer] = useState<Producer>()
const navigate = useNavigate()

useEffect(() => {
producerService.loadProducer().then(loadedProducer => setProducer(loadedProducer))
Expand All @@ -18,7 +20,7 @@ function DashboardAccount() {

return <>
<div>Bienvenue {producer?.user.firstName}</div>
<Button onClick={() => window.open('./account', '_self')}>Gérer mon compte</Button>
<Button onClick={() => navigate('/account')}>Gérer mon compte</Button>
</>
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
import React, { useEffect, useState } from 'react'
import { Button, Typography } from "@mui/material"
import Producer from '@viandeendirect/api/dist/models/Producer.js'
import {Producer} from '@viandeendirect/api/dist/models/Producer.js'
import { useKeycloak } from '@react-keycloak/web'
import { AuthenticationService } from '../../../authentication/service/AuthenticationService.ts'
import { Production } from '@viandeendirect/api/dist/models/Production'
import { ApiBuilder } from '../../../api/ApiBuilder.ts'
import { ProducerService } from '../../commons/service/ProducerService.ts'
import ProductionCard from '../../production/components/ProductionCard.tsx'
import { ProductionService } from '../../production/service/ProductionService.ts'
import { useNavigate } from 'react-router-dom'


function DashboardProductions() {
const {keycloak} = useKeycloak()
const [nextProduction, setNextProduction] = useState<Production | undefined>(undefined)
const apiBuilder = new ApiBuilder()
const navigate = useNavigate()

return <><div>Mes prochaines productions</div>
<Button onClick={() => window.open('./productions', '_self')}>Gérer mes productions</Button>
useEffect(() => {
const loadNextProduction = async () => {
const producerService = new ProducerService(keycloak)
const producer: Producer = await producerService.loadProducer()
const api = await apiBuilder.getAuthenticatedApi(keycloak)
const producerProductions = await api.getProductions({})
const productionService = new ProductionService(keycloak)
const productionsWithEndDate = []
for (let production of producerProductions) {
const productionEnd = await productionService.getProductionEnd(production)
productionsWithEndDate.push({...production, productionEnd: productionEnd})
}
const nextProduction = productionsWithEndDate
.filter(production => production.productionEnd && production.productionEnd > new Date())
.sort((production1, production2) => (production1.productionEnd > production2.productionEnd) ? 1 : -1)[0]
setNextProduction(nextProduction)
}
loadNextProduction()
}, [keycloak])

function displayNextProductionCard(): React.ReactNode {
if (nextProduction) {
return <ProductionCard production={nextProduction}
showActions={true}
onClick={undefined} />
}
return <div>Aucune production programmée pour le moment.</div>
}
return <><div>Ma prochaine production</div>
{displayNextProductionCard()}
<Button onClick={() => navigate('/productions')}>Gérer mes productions</Button>
</>
}

Expand Down
43 changes: 39 additions & 4 deletions frontend/app/src/domains/dashboard/components/DashboardSales.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
import React, { useEffect, useState } from 'react'
import { Button, Typography } from "@mui/material"
import { useKeycloak } from '@react-keycloak/web'
import { Button } from "@mui/material"
import { Producer, Sale } from '@viandeendirect/api/dist/models'
import SaleCard from '../../sale/components/SaleCard.tsx'
import { ApiBuilder } from '../../../api/ApiBuilder.ts'
import { ProducerService } from '../../commons/service/ProducerService.ts'
import { useNavigate } from 'react-router-dom'


export default function DashboardSales() {

return <><div>Mes prochaines ventes</div>
<Button onClick={() => window.open('./sales', '_self')}>Gérer mes ventes</Button>
</>
const {keycloak} = useKeycloak()
const [nextSale, setNextSale] = useState<Sale | undefined>(undefined)
const apiBuilder = new ApiBuilder()
const navigate = useNavigate()

useEffect(() => {
const loadNextSale = async () => {
const producerService = new ProducerService(keycloak)
const producer: Producer = await producerService.loadProducer()
const api = await apiBuilder.getAuthenticatedApi(keycloak)
const producerSales = await api.getProducerSales({producerId: +producer.id})
const nextSale = producerSales
.filter(sale => sale.deliveryStart && sale.deliveryStart > new Date())
.sort((sale1, sale2) => (sale1.deliveryStart > sale2.deliveryStart) ? 1 : -1)[0]
setNextSale(nextSale)
}
loadNextSale()
}, [keycloak])

function displayNextSaleCard(): React.ReactNode {
if (nextSale) {
return <SaleCard sale={nextSale}/>
}
return <div>Aucune vente programmée pour le moment.</div>
}

return <><div>Ma prochaine vente</div>
<div className='card-list'>
{displayNextSaleCard()}
</div>
<Button onClick={() => navigate('/sales')}>Gérer mes ventes</Button>
</>
}
27 changes: 27 additions & 0 deletions frontend/app/src/domains/production/service/ProductionService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Keycloak from "keycloak-js"
import { Production } from "@viandeendirect/api/dist/models";
import { ApiBuilder } from "../../../api/ApiBuilder.ts";

export class ProductionService {
keycloak: Keycloak
apiBuilder: ApiBuilder

constructor(keycloak: Keycloak) {
this.keycloak = keycloak
this.apiBuilder = new ApiBuilder();
}


async getProductionEnd(production: Production): Promise<Date | undefined> {
const api = await this.apiBuilder.getAuthenticatedApi(this.keycloak)
switch (production.productionType) {
case 'BeefProduction':
const beefProduction = await api.getBeefProduction({beefProductionId: +production.id})
return beefProduction.cuttingDate
case 'HonneyProduction':
return undefined
default:
return undefined
}
}
}
7 changes: 4 additions & 3 deletions frontend/app/src/domains/production/views/ProductionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ export default function ProductionsList() {
function getProductionCards() {
return productions.map(production => <div className='card-clickable'>
<ProductionCard
production={production}
showActions={true} >
production={production}
showActions={true}
onClick={undefined} >
</ProductionCard>
</div>)
}
Expand All @@ -37,6 +38,6 @@ export default function ProductionsList() {
export async function loadProductionListData(keycloakClient): Promise<Array<Production>> {
const apiBuilder = new ApiBuilder()
const api = await apiBuilder.getAuthenticatedApi(keycloakClient)
const productions: Array<Production> = await api.getProductions()
const productions: Array<Production> = await api.getProductions({})
return productions
}

0 comments on commit a31d597

Please sign in to comment.