-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEditModal.jsx
More file actions
170 lines (154 loc) · 5.26 KB
/
Copy pathEditModal.jsx
File metadata and controls
170 lines (154 loc) · 5.26 KB
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
import React, { useState } from 'react';
import { Modal, Col, Input, Select,
Dropdown, Row, Upload, Button, Menu, TreeSelect } from 'antd';
import { UploadOutlined } from "@ant-design/icons";
import { DownOutlined } from "@ant-design/icons";
import { widgetDict } from './Constants';
import { GetDataById } from '../../api/api';
import './Dashboards.css';
import DataDropdown from "../data/DataDropdown";
import { useEffect } from 'react';
const EditModal = props => {
console.log(props)
const [title, setTitle] = useState(props.el.chartTitle);
const [dataProps, setDataProps] = useState(props.el.dataProps || {});
const [dataId, setDataId] = useState(props.el.data);
const [headerMenu, setHeaderMenu] = useState([]);
const handleOk = () => {
// Send this chart with its updated properties to main grid to apply updates
let elCopy = Object.assign({}, props.el);
elCopy.chartTitle = title;
elCopy.dataProps = dataProps;
elCopy.data = dataId;
props.setVisible(false);
props.updateChart(elCopy);
}
const onTitleChange = e => {
setTitle(e.target.value);
}
const updateDataProps = e => {
GetDataById(localStorage.getItem('token'), e)
.then(res => {
const axes = Object.keys(res.file_data[0]);
const newData = { data: res.file_data };
//axis needs to be selectable by user..
newData['dataTitle'] = res.title;
//axes.forEach(axis => newData[axis] = axis);
newData['x'] = (axes.length > 0) ? axes[0] : 'x';
newData['y'] = (axes.length > 1) ? axes[1] : 'y';
setDataProps(newData)
setDataId(e)
})
.catch(e => {
setDataProps(undefined)
setDataId(e)
})
}
useEffect(() => {
updateMenu()
},[dataProps])
//get axes labels for dropdown
const updateMenu = () => {
if(dataProps.data){
const menu = Object.keys(dataProps.data[0]).map((header, index) => (
<Menu.Item key={index}>{header}</Menu.Item>
));
setHeaderMenu(menu)
}
}
const handleAxesConfigChange = (axis, { key }) => {
//update dataProps with new axis selection
setDataProps(d => {
d[axis] = Object.keys(dataProps.data[0])[key];
return d;
})
//update input
updateMenu()
};
const selectedWidget = widgetDict[props.el.widgetType];
const WidgetRender = selectedWidget || <div />;
const singleAxis = ['Bubble chart', 'Simple pie chart', 'Active shape pie chart', 'Simple radial bar chart', 'Tree map']
const axisMap = singleAxis.includes(props.el.widgetType) ? ['y'] : ['x', 'y']
const headers = ['h1', 'h2', 'h3']
const axesConfig =
headers.length !== 0 ? (
<React.Fragment>
<div className="widget-header">
Configure the axes of your widget.
</div>
<div style={{ margin: "1rem" }}>
<Row gutter={48}>
{axisMap.map((axis, index) => (
<React.Fragment key={index}>
<Col span={4} key={index}>
{singleAxis.includes(props.el.widgetType) ? 'Column': `${axis}-axis`}
<br />
<Dropdown
overlay={
<Menu
onClick={key =>
handleAxesConfigChange(axis, key)
}
>
{headerMenu}
</Menu>
}
>
<Button>
{dataProps[axis]? dataProps[axis]: axis}
<DownOutlined />
</Button>
</Dropdown>
</Col>
<Col span={2} />
</React.Fragment>
))}
</Row>
</div>
</React.Fragment>
) : "";
return (
<Modal
className="modal-style"
title={props.el.chartTitle || 'Unnamed Chart'}
visible={props.visible}
onOk={handleOk}
onCancel={() => props.setVisible(false)}
afterClose={props.onClose}
okText="Save"
width="50rem"
bodyStyle={{
overflowY: "auto",
padding: "2rem 3rem"
}}
>
<center className="widget-header">
{selectedWidget.value}
</center>
<Col style={{ height: "14rem" }} span={24}>
<WidgetRender {...dataProps} />
</Col>
<Col span={24}>
<div>
Select data
</div>
<DataDropdown onSelectData={updateDataProps} currentData={dataProps ? dataProps.dataTitle: null}/>
{axesConfig}
</Col>
{
props.el.widgetType !== "Text Box" &&
<Col span={24}>
<div className="widget-header">
Insert your chart name here (50 character limit).
</div>
<Input
maxLength={50}
value={title}
onChange={onTitleChange}
/>
</Col>
}
</Modal>
)
}
export default EditModal;