Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Préavis – Dans le cas de zones de capture différentes, permettre d'ajouter plusieurs fois la même espèce #3642

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import type { ManualPriorNotificationFormValues } from './types'

import PurposeCode = PriorNotification.PurposeCode

export const BLUEFIN_TUNA_EXTENDED_SPECY_CODES = ['BF1', 'BF2', 'BF3']

const FISHING_CATCH_VALIDATION_SCHEMA: ObjectSchema<PriorNotification.FormDataFishingCatch> = object({
faoArea: string().when('$hasGlobalFaoArea', {
is: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FormikNumberInput } from '@mtes-mct/monitor-ui'
import styled from 'styled-components'

import { InputWithUnit, SubRow } from './styles'
import { BLUEFIN_TUNA_EXTENDED_SPECY_CODES } from '../../constants'
import { BLUEFIN_TUNA_EXTENDED_SPECY_CODES } from '../../../../constants'

import type { PriorNotification } from '@features/PriorNotification/PriorNotification.types'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useGetSpeciesQuery } from '@api/specy'
import { FieldsetGroupSpinner } from '@features/Mission/components/MissionForm/shared/FieldsetGroup'
import { BLUEFIN_TUNA_EXTENDED_SPECY_CODES } from '@features/PriorNotification/constants'
import { useGetFaoAreasAsOptions } from '@hooks/useGetFaoAreasAsOptions'
import { useGetSpeciesAsOptions } from '@hooks/useGetSpeciesAsOptions'
import { CustomSearch, FormikNumberInput, FormikSelect, Select, SingleTag } from '@mtes-mct/monitor-ui'
Expand All @@ -12,7 +13,6 @@ import styled from 'styled-components'
import { FormikExtraField } from './FormikExtraField'
import { InputWithUnit, SubRow } from './styles'
import { getFishingsCatchesValidationError } from './utils'
import { BLUEFIN_TUNA_EXTENDED_SPECY_CODES } from '../../constants'
import { getFishingsCatchesInitialValues } from '../../utils'

import type { Specy } from '../../../../../../domain/types/specy'
Expand Down Expand Up @@ -52,11 +52,13 @@ export function FormikFishingCatchesMultiSelect({ isReadOnly }: FormikFishingCat
)

const add = (nextSpecy: Specy | undefined) => {
const specyIsAlreadyInCatches = values.fishingCatches?.find(
fishingCatch => fishingCatch.specyCode === nextSpecy?.code
)
if (specyIsAlreadyInCatches) {
return
if (values.hasGlobalFaoArea) {
const isSpecyCodeAlreadyInCatches = values.fishingCatches?.find(
fishingCatch => fishingCatch.specyCode === nextSpecy?.code
)
if (isSpecyCodeAlreadyInCatches) {
return
}
}

const specyOption = speciesAsOptions?.find(({ value }) => value.code === nextSpecy?.code)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BLUEFIN_TUNA_EXTENDED_SPECY_CODES } from '@features/PriorNotification/constants'
import { pick } from 'lodash'

import { BLUEFIN_TUNA_EXTENDED_SPECY_CODES, INITIAL_FORM_VALUES } from './constants'
import { INITIAL_FORM_VALUES } from './constants'
import { PriorNotification } from '../../PriorNotification.types'

import type { ManualPriorNotificationFormValues } from './types'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { COUNTRIES_AS_ALPHA3_OPTIONS } from '@constants/index'
import { useGetFleetSegmentsAsOptions } from '@features/FleetSegment/hooks/useGetFleetSegmentsAsOptions'
import { BLUEFIN_TUNA_EXTENDED_SPECY_CODES, BLUEFIN_TUNA_SPECY_CODE } from '@features/PriorNotification/constants'
import { useGetPriorNotificationTypesAsOptions } from '@features/PriorNotification/hooks/useGetPriorNotificationTypesAsOptions'
import { useGetGearsAsTreeOptions } from '@hooks/useGetGearsAsTreeOptions'
import { useGetPortsAsTreeOptions } from '@hooks/useGetPortsAsTreeOptions'
Expand All @@ -20,6 +21,7 @@ import {
useNewWindow
} from '@mtes-mct/monitor-ui'
import { assertNotNullish } from '@utils/assertNotNullish'
import { uniq } from 'lodash'
import { useCallback } from 'react'
import styled from 'styled-components'

Expand Down Expand Up @@ -110,7 +112,11 @@ export function FilterBar() {
}

const updateSpecyCodes = (nextSpecyCodes: string[] | undefined) => {
dispatch(priorNotificationActions.setListFilterValues({ specyCodes: nextSpecyCodes }))
const normalizedNextSpecyCodes = nextSpecyCodes?.includes(BLUEFIN_TUNA_SPECY_CODE)
? uniq([...nextSpecyCodes, ...BLUEFIN_TUNA_EXTENDED_SPECY_CODES])
: nextSpecyCodes

dispatch(priorNotificationActions.setListFilterValues({ specyCodes: normalizedNextSpecyCodes }))
}

const updateStatuses = (nextStatuses: FilterStatus[] | undefined) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { useMainAppDispatch } from '@hooks/useMainAppDispatch'
import { customDayjs, Icon, TableWithSelectableRows, Tag, THEME } from '@mtes-mct/monitor-ui'
import { flexRender, type Row as RowType } from '@tanstack/react-table'
import { useIsSuperUser } from 'auth/hooks/useIsSuperUser'
import { orderBy } from 'lodash'
import styled from 'styled-components'

import { FixedTag, None } from './styles'
import { getColorsFromState, getExpandableRowCellCustomStyle } from './utils'
import { displayOnboardFishingSpecies, getColorsFromState, getExpandableRowCellCustomStyle } from './utils'
import { PriorNotification } from '../../PriorNotification.types'
import { openManualPriorNotificationForm } from '../../useCases/openManualPriorNotificationForm'
import { openPriorNotificationCard } from '../../useCases/openPriorNotificationCard'
Expand All @@ -20,7 +19,6 @@ export function Row({ row }: RowProps) {
const isSuperUser = useIsSuperUser()

const priorNotification = row.original
const firstFiveOnBoardCatchesByWeight = orderBy(priorNotification.onBoardCatches, ['weight'], ['desc']).slice(0, 5)

const openCard = () => {
if (priorNotification.isManuallyCreated) {
Expand Down Expand Up @@ -161,14 +159,7 @@ export function Row({ row }: RowProps) {
<ExpandedRowCell>
<ExpandedRowLabel>Principales espèces à bord :</ExpandedRowLabel>
{priorNotification.onBoardCatches.length > 0 ? (
<ExpandedRowList>
{firstFiveOnBoardCatchesByWeight.map(({ species, speciesName, weight }) => (
<StyledLi
key={species}
title={`${speciesName} (${species}) – ${weight} kg`}
>{`${speciesName} (${species}) – ${weight} kg`}</StyledLi>
))}
</ExpandedRowList>
<ExpandedRowList>{displayOnboardFishingSpecies(priorNotification.onBoardCatches)}</ExpandedRowList>
) : (
<None>Aucune capture à bord.</None>
)}
Expand Down Expand Up @@ -211,13 +202,6 @@ export function Row({ row }: RowProps) {
)
}

const StyledLi = styled.li`
overflow: hidden;
text-overflow: ellipsis;
width: 215px;
white-space: nowrap;
`

const ExpandableRowCell = styled(TableWithSelectableRows.Td)<{
$isInvalidated: boolean
}>`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`features/PriorNotification/components/PriorNotificationList/utils displayOnboardFishingSpecies() should return the expected result 1`] = `
[
<styled.li
title="Thon rouge de l'Atlantique (BFT) – 18 kg"
>
Thon rouge de l'Atlantique (BFT) – 18 kg
</styled.li>,
<styled.li
title="DEF Name (DEF) – 7 kg"
>
DEF Name (DEF) – 7 kg
</styled.li>,
<styled.li
title="ABC Name (ABC) – 3 kg"
>
ABC Name (ABC) – 3 kg
</styled.li>,
]
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, expect, it } from '@jest/globals'

import { displayOnboardFishingSpecies } from '../utils'

import type { LogbookMessage } from '@features/Logbook/LogbookMessage.types'

describe('features/PriorNotification/components/PriorNotificationList/utils', () => {
it('displayOnboardFishingSpecies() should return the expected result', () => {
const onboardCatches = [
{ species: 'ABC', speciesName: `ABC Name`, weight: 0 },
{ species: 'BF1', speciesName: `Thon rouge de l'Atlantique (Calibre 1)`, weight: 1 },
{ species: 'DEF', speciesName: `DEF Name`, weight: 2 },
{ species: 'ABC', speciesName: `ABC Name`, weight: 3 },
{ species: 'BFT', speciesName: `Thon rouge de l'Atlantique`, weight: 4 },
{ species: 'DEF', speciesName: `DEF Name`, weight: 5 },
{ species: 'BF2', speciesName: `Thon rouge de l'Atlantique (Calibre 2)`, weight: 6 },
{ species: 'BF3', speciesName: `Thon rouge de l'Atlantique (Calibre 3)`, weight: 7 }
] as LogbookMessage.Catch[]

const result = displayOnboardFishingSpecies(onboardCatches)

expect(result).toMatchSnapshot()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import {
type AllSeafrontGroup
} from '@constants/seafront'
import { LogbookMessage } from '@features/Logbook/LogbookMessage.types'
import {
BLUEFIN_TUNA_EXTENDED_SPECY_CODES,
BLUEFIN_TUNA_NAME_FR,
BLUEFIN_TUNA_SPECY_CODE
} from '@features/PriorNotification/constants'
import { THEME, customDayjs, getMaybeBooleanFromRichBoolean, type DateAsStringRange } from '@mtes-mct/monitor-ui'
import { update } from 'lodash'
import styled from 'styled-components'

import {
COMMUNITY_PRIOR_NOTIFICATION_TYPES,
Expand All @@ -23,6 +30,51 @@ import { PriorNotification } from '../../PriorNotification.types'
import type { FilterStatus, ListFilter } from './types'
import type { CSSProperties } from 'react'

export function displayOnboardFishingSpecies(onBoardCatches: LogbookMessage.Catch[]) {
const heaviestOnBoardCatches = onBoardCatches
.reduce<
Array<{
specyCode: string
specyName: string
weight: number
}>
>((aggregatedCatches, currentCatch) => {
const [normalizedSpecyCode, normalizedSpecyName] = BLUEFIN_TUNA_EXTENDED_SPECY_CODES.includes(
currentCatch.species
)
? [BLUEFIN_TUNA_SPECY_CODE, BLUEFIN_TUNA_NAME_FR]
: [currentCatch.species, currentCatch.speciesName]
const existingCatchSpecyIndex = aggregatedCatches.findIndex(
aggregatedCatch => aggregatedCatch.specyCode === normalizedSpecyCode
)
if (
existingCatchSpecyIndex > -1 &&
currentCatch.weight !== undefined &&
aggregatedCatches[existingCatchSpecyIndex]?.weight !== undefined
) {
return update(aggregatedCatches, `[${existingCatchSpecyIndex}].weight`, weight => weight + currentCatch.weight)
}

return [
...aggregatedCatches,
{
specyCode: normalizedSpecyCode,
specyName: normalizedSpecyName,
weight: currentCatch.weight
}
]
}, [])
.sort((a, b) => b.weight - a.weight)
.slice(0, 5)

return heaviestOnBoardCatches.map(({ specyCode, specyName, weight }) => (
<StyledLi
key={specyCode}
title={`${specyName} (${specyCode}) – ${weight} kg`}
>{`${specyName} (${specyCode}) – ${weight} kg`}</StyledLi>
))
}

function getApiFilterFromExpectedArrivalPeriod(
period: ExpectedArrivalPeriod,
customPeriod: DateAsStringRange | undefined
Expand Down Expand Up @@ -254,3 +306,10 @@ export function getTitle(seafrontGroup: SeafrontGroup | AllSeafrontGroup | NoSea
return `Préavis en ${SUB_MENU_LABEL[seafrontGroup]}`
}
}

const StyledLi = styled.li`
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
width: 215px;
`
4 changes: 4 additions & 0 deletions frontend/src/features/PriorNotification/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const BLUEFIN_TUNA_SPECY_CODE = 'BFT'
export const BLUEFIN_TUNA_EXTENDED_SPECY_CODES = ['BF1', 'BF2', 'BF3']
export const BLUEFIN_TUNA_NAME_FR = `Thon rouge de l'Atlantique`

export enum OpenedPriorNotificationType {
LogbookForm = 'LogbookForm',
ManualForm = 'ManualForm'
Expand Down
Loading