Skip to content

Commit

Permalink
Add fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
louptheron committed Feb 13, 2024
1 parent a1bd560 commit 7763a19
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
26 changes: 17 additions & 9 deletions frontend/src/domain/use_cases/fleetSegment/deleteFleetSegment.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { deleteFleetSegmentFromAPI } from '../../../api/fleetSegment'
import { deleteFleetSegmentFromAPI } from '@api/fleetSegment'

import { setError } from '../../shared_slices/Global'

import type { FleetSegment } from '../../types/fleetSegment'

/**
* Delete a fleet segment
*/
export const deleteFleetSegment = (segment: string, year: number) => dispatch =>
deleteFleetSegmentFromAPI(segment, year)
.then(updatedFleetSegments =>
(Object.assign([], updatedFleetSegments) as FleetSegment[]).sort((a, b) => a.segment.localeCompare(b.segment))
)
.catch(error => {
dispatch(setError(error))
})
export const deleteFleetSegment =
(segment: string, year: number) =>
async (dispatch): Promise<undefined | FleetSegment[]> => {
try {
const updatedFleetSegments = await deleteFleetSegmentFromAPI(segment, year)

return (Object.assign([], updatedFleetSegments) as FleetSegment[]).sort((a, b) =>
a.segment.localeCompare(b.segment)
)
} catch (e) {
dispatch(setError(e))

return undefined
}
}
29 changes: 14 additions & 15 deletions frontend/src/features/Logbook/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ export function buildCatchArray(catches: LogbookCatch[]): CatchWithProperties[]
return catches
.reduce((accumulator: CatchWithProperties[], logbookCatch) => {
const sameSpeciesIndex = accumulator.findIndex(accCatch => accCatch.species === logbookCatch.species)
const logbookCatchProperties = getCatchPropertiesObject(logbookCatch)

if (sameSpeciesIndex === NOT_FOUND) {
accumulator.push({
properties: [getCatchPropertiesObject(logbookCatch)],
return accumulator.concat({
properties: [logbookCatchProperties],
species: logbookCatch.species,
speciesName: undefinedize(logbookCatch.speciesName),
weight: logbookCatch.weight ? logbookCatch.weight : 0
})

return accumulator
}

// @ts-ignore
accumulator[sameSpeciesIndex].properties.push(getCatchPropertiesObject(logbookCatch))
// @ts-ignore
accumulator[sameSpeciesIndex].weight += logbookCatch.weight ? parseFloat(logbookCatch.weight) : 0
const nextCatch = accumulator[sameSpeciesIndex] as CatchWithProperties
nextCatch.properties = nextCatch.properties.concat(logbookCatchProperties)
nextCatch.weight += logbookCatch.weight ?? 0

accumulator[sameSpeciesIndex] = nextCatch

return accumulator
}, [])
Expand All @@ -61,20 +61,19 @@ export function buildProtectedCatchArray(catches: ProtectedSpeciesCatch[]): Prot
const sameSpeciesIndex = accumulator.findIndex(accCatch => accCatch.species === logbookCatch.species)

if (sameSpeciesIndex === NOT_FOUND) {
accumulator.push({
return accumulator.concat({
properties: [logbookCatch],
species: logbookCatch.species,
speciesName: undefinedize(logbookCatch.speciesName),
weight: logbookCatch.weight ? logbookCatch.weight : 0
})

return accumulator
}

// @ts-ignore
accumulator[sameSpeciesIndex].properties.push(logbookCatch)
// @ts-ignore
accumulator[sameSpeciesIndex].weight += logbookCatch.weight ? parseFloat(logbookCatch.weight) : 0
const nextCatch = accumulator[sameSpeciesIndex] as ProtectedCatchWithProperties
nextCatch.properties = nextCatch.properties.concat(logbookCatch)
nextCatch.weight += logbookCatch.weight ?? 0

accumulator[sameSpeciesIndex] = nextCatch

return accumulator
}, [])
Expand Down

0 comments on commit 7763a19

Please sign in to comment.