generated from Bjorn86/cra-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory-table.jsx
111 lines (101 loc) · 3.05 KB
/
history-table.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { Table, Column } from 'react-virtualized';
import { memo, useCallback } from 'react';
import { useDispatch } from 'react-redux';
import { Link } from 'react-router-dom';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { deletedFromHistory } from 'features/history/model/deleted-from-history';
import { ReactComponent as Trash } from 'shared/ui/assets/icons/trash.svg';
import { useTheme } from 'shared/lib/use-theme';
import Button from 'shared/ui/button/button';
import { formatDate } from '../lib/utils';
/* Не используются CSS Module потому что они не поддерживаются в React Virtualized */
import './history-table.scss';
function HistoryTable({ data }) {
const theme = useTheme();
const dispatch = useDispatch();
const rowGetter = ({ index }) => data[index];
const renderNumber = ({ rowIndex }) => rowIndex + 1;
const renderLink = ({ rowData }) => {
const encodedQuery = encodeURIComponent(rowData.query);
return (
<Link
to={`/search?query=${encodedQuery}`}
className={clsx('table__link', {
table__link_theme_dark: theme === 'dark',
})}
>
{rowData.query}
</Link>
);
};
const removeRecord = useCallback(
(query) => {
dispatch(deletedFromHistory(query));
},
[dispatch],
);
const renderDeleteButton = ({ rowData }) => (
<Button
view='rounded'
content={theme === 'dark' ? <Trash fill='#fff' /> : <Trash />}
alt='Delete from history'
onClick={() => removeRecord(rowData)}
/>
);
return (
<Table
width={1400}
height={550}
headerHeight={50}
rowHeight={40}
rowCount={data.length}
rowGetter={rowGetter}
rowClassName='table__row'
headerClassName={clsx('table__header', {
table__header_theme_dark: theme === 'dark',
})}
>
<Column
label='№'
dataKey='index'
width={150}
cellRenderer={renderNumber}
className={clsx('table__column', 'table__column_position_center', {
table__column_theme_dark: theme === 'dark',
})}
/>
<Column
label='Search query'
dataKey='query'
width={900}
cellRenderer={renderLink}
className={clsx('table__column', {
table__column_theme_dark: theme === 'dark',
})}
/>
<Column
label='Date'
dataKey='date'
width={200}
cellRenderer={({ cellData }) => formatDate(cellData)}
className={clsx('table__column', 'table__column_position_center', {
table__column_theme_dark: theme === 'dark',
})}
/>
<Column
label='Actions'
dataKey='actions'
width={150}
cellRenderer={renderDeleteButton}
className={clsx('table__column', 'table__column_position_center', {
table__column_theme_dark: theme === 'dark',
})}
/>
</Table>
);
}
export default memo(HistoryTable);
HistoryTable.propTypes = {
data: PropTypes.arrayOf(PropTypes.object).isRequired,
};