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

[Signalements] Correction du tri du tableau #2600

Merged
merged 5 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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,21 +6,22 @@ import type { TableOptions } from '../../../../../hooks/useTable/types'
export const REPORTING_LIST_TABLE_OPTIONS: TableOptions<InfractionSuspicionReporting | PendingAlertReporting> = {
columns: [
{
fallbackKey: 'creationDate',
ivangabriele marked this conversation as resolved.
Show resolved Hide resolved
fixedWidth: 112,
isSortable: true,
key: 'validationDate',
label: 'Ouvert il y a...'
},
{
fixedWidth: 112,
isSortable: true,
isSortable: false,
key: 'source',
label: 'Origine',
transform: getReportingOrigin
},
{
fixedWidth: 288,
isSortable: true,
isSortable: false,
key: 'title',
label: 'Titre',
transform: getReportingTitle
Expand Down Expand Up @@ -59,5 +60,14 @@ export const REPORTING_LIST_TABLE_OPTIONS: TableOptions<InfractionSuspicionRepor
}
],
isCheckable: true,
searchableKeys: ['dml', 'externalReferenceNumber', 'internalReferenceNumber', 'ircs', 'reportingTitle', 'vesselName']
searchableKeys: [
'value.dml',
'externalReferenceNumber',
'internalReferenceNumber',
'ircs',
'title',
'source',
'value.natinfCode',
'vesselName'
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ MMSI: ${reporting.mmsi || ''}`
<CardTableBody>
{tableData.map((reporting, index) => {
const editingIsDisabled = reporting.type === ReportingType.ALERT
const reportingDate = reporting.validationDate || reporting.creationDate

return (
<CardTableRow key={reporting.id} data-cy="side-window-current-reportings" index={index + 1} style={{}}>
Expand All @@ -187,8 +188,8 @@ MMSI: ${reporting.mmsi || ''}`
onChange={() => toggleTableCheckForId(reporting.id)}
/>
</Cell>
<Cell style={columnStyles[1]} title={reporting.validationDate || reporting.creationDate}>
{timeago.format(reporting.validationDate || reporting.creationDate, 'fr')}
<Cell style={columnStyles[1]} title={reportingDate}>
{timeago.format(reportingDate, 'fr')}
</Cell>
<Cell style={columnStyles[2]} title={getReportingOrigin(reporting, true)}>
{getReportingOrigin(reporting)}
Expand Down
13 changes: 5 additions & 8 deletions frontend/src/features/SideWindow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function SideWindowWithRef({ isFromURL }: SideWindowProps, ref: ForwardedRef<HTM
const [isOverlayed, setIsOverlayed] = useState(false)
const [isPreloading, setIsPreloading] = useState(true)

const beaconMalfunctionBoardGrayOverlayStyle: CSSProperties = useMemo(
const grayOverlayStyle: CSSProperties = useMemo(
() => ({
background: THEME.color.charcoal,
height: '100%',
Expand Down Expand Up @@ -121,12 +121,9 @@ function SideWindowWithRef({ isFromURL }: SideWindowProps, ref: ForwardedRef<HTM
<GlobalStyle />

<Menu selectedMenu={selectedPath.menu} />
{/* TODO Move that within BeaconMalfunctionBoard. */}
{selectedPath.menu === SideWindowMenuKey.BEACON_MALFUNCTION_BOARD && (
<BeaconMalfunctionsBoardGrayOverlay
onClick={closeRightSidebar}
style={beaconMalfunctionBoardGrayOverlayStyle}
/>
{(selectedPath.menu === SideWindowMenuKey.BEACON_MALFUNCTION_BOARD ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu ne penses pas qu'il faille garder la TODO ? Parce que c'est pas top de garder un truc dédié à un des contenus directement dans l'index de la SideWindow.

selectedPath.menu === SideWindowMenuKey.ALERT_LIST_AND_REPORTING_LIST) && (
<GrayOverlay onClick={closeRightSidebar} style={grayOverlayStyle} />
)}
<FrontendErrorBoundary>
{isPreloading && (
Expand Down Expand Up @@ -228,7 +225,7 @@ const Content = styled.div`
min-width: 0;
`

const BeaconMalfunctionsBoardGrayOverlay = styled.div``
const GrayOverlay = styled.div``

const Loading = styled.div`
margin-left: 550px;
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/hooks/useTable/TableHead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type TableHeadProps = {
isCheckable?: boolean | undefined
isSortingDesc: boolean
onAllCheckChange: () => Promisable<void>
onSort: (key: string, isDesc: boolean) => Promisable<void>
onSort: (key: string, isDesc: boolean, fallbackKey?: string | undefined) => Promisable<void>
sortingKey?: string | undefined
}
export function TableHead({
Expand All @@ -30,7 +30,7 @@ export function TableHead({
return
}

onSort(column.key, column.key === sortingKey && !isSortingDesc)
onSort(column.key, column.key === sortingKey && !isSortingDesc, column.fallbackKey)
}

return (
Expand Down
46 changes: 21 additions & 25 deletions frontend/src/hooks/useTable/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import diacritics from 'diacritics'
import Fuse from 'fuse.js'
import { ascend, assocPath, descend, equals, path, pipe, propEq, sortWith } from 'ramda'
import { get, orderBy } from 'lodash'
import { assocPath, equals, path, pipe, propEq } from 'ramda'
import { useCallback, useEffect, useMemo, useState } from 'react'

import { TableHead } from './TableHead'
import { getArrayPathFromStringPath, normalizeSearchQuery } from './utils'

import type { TableItem, FilterFunction, TableOptions } from './types'
import type { FilterFunction, TableItem, TableOptions } from './types'
import type { CollectionItem } from '../../types'

export function useTable<T extends CollectionItem = CollectionItem>(
Expand All @@ -25,6 +26,7 @@ export function useTable<T extends CollectionItem = CollectionItem>(
const [checkedIds, setCheckedIds] = useState<(number | string)[]>([])
const [isSortingDesc, setIsSortingDesc] = useState(Boolean(isDefaultSortingDesc))
const [sortingKey, setSortingKey] = useState<string | undefined>(defaultSortedKey)
const [sortingFallbackKey, setSortingFallbackKey] = useState<string | undefined>(undefined)

const rawData = useMemo(() => maybeRawData || [], [maybeRawData])

Expand Down Expand Up @@ -152,30 +154,23 @@ export function useTable<T extends CollectionItem = CollectionItem>(
return filteredAndSearchedTableData
}

const sortingKeyAsArrayPath = getArrayPathFromStringPath(sortingKey)
const sortingKeyPath = path(['$sortable', ...sortingKeyAsArrayPath]) as any
const bySortingKey = isSortingDesc ? descend(sortingKeyPath) : ascend(sortingKeyPath)

return sortWith(
[
bySortingKey,
// Sort undefined values (to the bottom when ascending and to the top when descending)
(firstDataItem, secondDataItem) => {
const firstDataItemProp = sortingKeyPath(firstDataItem)
const secondDataItemProp = sortingKeyPath(secondDataItem)

if (isSortingDesc) {
// eslint-disable-next-line no-nested-ternary
return firstDataItemProp === undefined ? (secondDataItemProp === undefined ? 0 : -1) : 1
}

// eslint-disable-next-line no-nested-ternary
return firstDataItemProp === undefined ? (secondDataItemProp === undefined ? 0 : 1) : -1
return orderBy(
filteredAndSearchedTableData,
item => {
const value = get(item, sortingKey)
if (value !== undefined) {
return value
}
],
filteredAndSearchedTableData

if (!sortingFallbackKey) {
return undefined
}

return get(item, sortingFallbackKey)
},
isSortingDesc ? ['desc'] : ['asc']
)
}, [filteredAndSearchedTableData, isSortingDesc, sortingKey])
}, [filteredAndSearchedTableData, isSortingDesc, sortingKey, sortingFallbackKey])

const getCheckedData = useCallback(
() => filteredAndSearchedAndSortedTableData.filter(({ id }) => checkedIds.includes(id)),
Expand All @@ -186,8 +181,9 @@ export function useTable<T extends CollectionItem = CollectionItem>(
setCheckedIds(isAllChecked ? [] : filteredAndSearchedAndSortedTableData.map(({ id }) => id).sort())
}, [filteredAndSearchedAndSortedTableData, isAllChecked])

const sortColumn = useCallback((key: string, isDesc: boolean) => {
const sortColumn = useCallback((key: string, isDesc: boolean, fallbackKey: string | undefined) => {
setSortingKey(key)
setSortingFallbackKey(fallbackKey)
setIsSortingDesc(isDesc)
}, [])

Expand Down
1 change: 1 addition & 0 deletions frontend/src/hooks/useTable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { CollectionItem, Native } from '../../types'
import type Fuse from 'fuse.js'

export type TableColumn<T extends Record<string, any> = Record<string, any>> = {
fallbackKey?: string | undefined
/** Fixed width expressed in root ephemeral unit (rem) */
fixedWidth?: number
isSortable?: boolean
Expand Down
Loading