diff --git a/frontend/src/domain/use_cases/fleetSegment/deleteFleetSegment.ts b/frontend/src/domain/use_cases/fleetSegment/deleteFleetSegment.ts index fac464192c..ea40fa8709 100644 --- a/frontend/src/domain/use_cases/fleetSegment/deleteFleetSegment.ts +++ b/frontend/src/domain/use_cases/fleetSegment/deleteFleetSegment.ts @@ -1,4 +1,5 @@ -import { deleteFleetSegmentFromAPI } from '../../../api/fleetSegment' +import { deleteFleetSegmentFromAPI } from '@api/fleetSegment' + import { setError } from '../../shared_slices/Global' import type { FleetSegment } from '../../types/fleetSegment' @@ -6,11 +7,18 @@ 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 => { + 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 + } + } diff --git a/frontend/src/features/Logbook/utils.ts b/frontend/src/features/Logbook/utils.ts index d9ca7fe08f..8f2b403f64 100644 --- a/frontend/src/features/Logbook/utils.ts +++ b/frontend/src/features/Logbook/utils.ts @@ -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 }, []) @@ -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 }, [])