-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataBaseTable.tsx
184 lines (169 loc) · 4.72 KB
/
DataBaseTable.tsx
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import React, { useEffect, useState } from "react";
import { Button, Table } from "antd";
import {
EditableCell,
EditableRow,
} from "../../../modules/database/EditTableContent";
import { DeleteOutlined, PlusOutlined } from "@ant-design/icons";
import { useDispatch, useSelector } from "react-redux";
import {
deleteRows,
setNewRow,
setNewTypeBlock,
} from "../../../store/slices/blockSlice";
import AddColumnDataBase, {
INewTypeBlock,
} from "../../../modules/database/AddColumnTable";
import { BlockType, IContentBlock } from "../../../store/interfaces/block";
import { IColumnTableDataBase } from "../../../modules/interfaces/database";
import { RootState } from "../../../store/store";
import { useLocation, useParams } from "react-router-dom";
import { updatePageContent } from "../../../store/API/Page";
type EditableTableProps = Parameters<typeof Table>[0];
type ColumnTypes = Exclude<EditableTableProps["columns"], undefined>;
interface EditableTableState {
dataSource: IContentBlock;
isEditable: boolean;
isSubPage: boolean;
index: number;
}
const TableDataBase: React.FC<EditableTableState> = ({
dataSource,
isEditable,
isSubPage,
index,
}) => {
const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
const globalState = useSelector((state: RootState) => state.blocks.content);
const [updateDataBase, setUpdateData] = useState(false);
const param = useParams();
const dispatch = useDispatch();
const setToTrelloView = (index: number) => {
const values: INewTypeBlock = {
index: index,
type: BlockType.TRELLO,
};
dispatch(setNewTypeBlock(values));
setUpdateData(true);
};
useEffect(() => {
(async () => {
if (updateDataBase) {
// console.log(`Database is Updating TRELLO`, globalState);
if (!param.id) return;
await updatePageContent(param.id, globalState);
setUpdateData(false);
}
})();
}, [updateDataBase, globalState, param.id]);
const onSelectChange = (newSelectedRowKeys: React.Key[]) => {
setSelectedRowKeys(newSelectedRowKeys);
};
if (typeof dataSource.content === "string") return null;
const components = {
body: {
row: EditableRow,
cell: EditableCell,
},
};
const columns = dataSource.content.columns.map((col) => {
if (typeof col === "string" || !("title" in col)) {
return col;
}
return {
...col,
onCell: (record: IColumnTableDataBase) => ({
title: col.title,
record,
blockIndex: index,
editable: col.editable,
isEditable: isEditable,
typeIndex: col.typeIndex,
dataIndex: col.dataIndex,
}),
};
});
const rowSelection = {
selectedRowKeys,
onChange: onSelectChange,
};
const rowToDelete = {
index: index,
keys: selectedRowKeys,
};
const hasSelected = selectedRowKeys.length > 0;
const isColumn = dataSource.content.columns.length > 0;
const deleteAllSelectedRowKeys = () => {
dispatch(deleteRows(rowToDelete));
setUpdateData(true);
setSelectedRowKeys([]);
};
return (
<div
style={{
marginTop: 5,
marginBottom: 16,
width: "90%",
position: "relative",
}}
>
<Table
components={components}
rowClassName={() => "editable-row"}
rowSelection={rowSelection}
bordered
dataSource={dataSource.content.rows}
columns={columns as ColumnTypes}
pagination={false}
/>
<div>
{isColumn && isEditable ? (
<Button
onClick={() => dispatch(setNewRow({ index: index }))}
style={{
marginBottom: 10,
marginTop: 10,
color: "black",
}}
type="text"
icon={<PlusOutlined />}
>
Add new raw
</Button>
) : null}
{hasSelected && isEditable ? (
<Button
type="text"
onClick={() => deleteAllSelectedRowKeys()}
style={{ color: "red", marginLeft: 10 }}
disabled={!hasSelected}
icon={<DeleteOutlined />}
/>
) : null}
<div
style={{
display: "flex",
alignItems: "center",
}}
>
{!isSubPage ? (
<>
{isEditable ? (
<>
<AddColumnDataBase blockIndex={index} />{" "}
</>
) : null}
<Button
style={{ marginTop: 12 }}
onClick={() => setToTrelloView(index)}
>
Trello view
</Button>
</>
) : null}
</div>
</div>
</div>
);
};
export default TableDataBase;