1- import React , { useEffect , useMemo , useState } from 'react' ;
1+ import React , { useEffect , useState } from 'react' ;
22import { useTranslation } from 'react-i18next' ;
33import Table from '@mui/material/Table' ;
44import TableBody from '@mui/material/TableBody' ;
@@ -44,61 +44,38 @@ const AccessibilityTable = ({ userPosition, setIsAccessibilityTableOpen }) => {
4444 } , [ categories , userPosition ] ) ;
4545
4646 useEffect ( ( ) => {
47+ if ( ! data ) {
48+ return ;
49+ }
4750 try {
48- const uniqueHeadersSet = new Set ( ) ;
49- if ( ! data ) {
50- return ;
51- }
52- uniqueHeadersSet . add ( t ( 'title' ) ) ;
53- for ( const place of data ) {
54- for ( const item of place . data ) {
51+ const uniqueHeadersSet = new Set ( [ t ( 'title' ) ] ) ;
52+ data . forEach ( place => {
53+ place . data . forEach ( item => {
5554 uniqueHeadersSet . add ( item [ 0 ] ) ;
56- }
57- }
58- const uniqueNumberedKeys = { } ;
59- for ( const [ index , key ] of Array . from ( uniqueHeadersSet ) . entries ( ) ) {
60- uniqueNumberedKeys [ key ] = index ;
61- }
62- const orderedKeysArray = Object . keys ( uniqueNumberedKeys ) . sort (
63- ( a , b ) => uniqueNumberedKeys [ a ] - uniqueNumberedKeys [ b ] ,
64- ) ;
55+ } ) ;
56+ } ) ;
57+ const orderedKeysArray = Array . from ( uniqueHeadersSet ) ;
6558 setHeaders ( orderedKeysArray ) ;
6659
67- const rowsLocal = [ ] ;
68-
6960 const getArr = ( placeItem , key ) => {
7061 const item = placeItem . find ( it => it [ 0 ] === key ) ;
71- if ( ! item ) {
72- return [ '' , '—' ] ;
73- }
74- return item ;
62+ return item || [ '' , '—' ] ;
7563 } ;
7664
77- for ( const it of data ) {
78- const row = [ ] ;
79- const place = it . data ;
80- row . push ( it . title ) ;
65+ const rowsLocal = data . map ( it => {
66+ const row = [ it . title ] ;
8167 // Skip first element (title) and iterate over remaining keys
82- for ( const key of orderedKeysArray . slice ( 1 ) ) {
83- const values = getArr ( place , key ) ;
84- if ( values === undefined ) {
85- continue ;
86- }
87- const value = values [ 1 ] ;
88- if ( Array . isArray ( value ) ) {
89- const str = value . join ( ', ' ) ;
90- row . push ( str ) ;
91- continue ;
92- }
93- row . push ( value ) ;
94- }
95- rowsLocal . push ( row ) ;
96- }
68+ orderedKeysArray . slice ( 1 ) . forEach ( key => {
69+ const [ , value ] = getArr ( it . data , key ) ;
70+ row . push ( Array . isArray ( value ) ? value . join ( ', ' ) : value ) ;
71+ } ) ;
72+ return row ;
73+ } ) ;
9774 setRows ( rowsLocal ) ;
9875 } catch ( error ) {
9976 console . log ( 'AccessibilityTable: ' , error ) ;
10077 }
101- } , [ data ] ) ;
78+ } , [ data , t ] ) ;
10279
10380 return (
10481 < >
@@ -125,13 +102,13 @@ const AccessibilityTable = ({ userPosition, setIsAccessibilityTableOpen }) => {
125102 </ TableRow >
126103 </ TableHead >
127104 < TableBody >
128- { rows . map ( ( row , index ) => (
105+ { rows . map ( row => (
129106 < TableRow
130107 key = { row . toString ( ) }
131108 sx = { { '&:last-child td, &:last-child th' : { border : 0 } } }
132109 >
133- { row . map ( ( cell , index ) => (
134- < TableCell key = { ` ${ cell . toString ( ) } - ${ index } ` } align = "center" >
110+ { row . map ( ( cell , cellIndex ) => (
111+ < TableCell key = { headers [ cellIndex ] } align = "center" >
135112 { cell . type ? < FieldRenderer value = { cell } /> : cell }
136113 </ TableCell >
137114 ) ) }
0 commit comments