SettingsDataDashboard.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import { Button, Col, Form, Row, Spin, Tag } from '@douyinfe/semi-ui';
  3. import {
  4. compareObjects,
  5. API,
  6. showError,
  7. showSuccess,
  8. showWarning,
  9. } from '../../../helpers';
  10. export default function DataDashboard(props) {
  11. const optionsDataExportDefaultTime = [
  12. { key: 'hour', label: '小时', value: 'hour' },
  13. { key: 'day', label: '天', value: 'day' },
  14. { key: 'week', label: '周', value: 'week' },
  15. ];
  16. const [loading, setLoading] = useState(false);
  17. const [inputs, setInputs] = useState({
  18. DataExportEnabled: false,
  19. DataExportInterval: '',
  20. DataExportDefaultTime: '',
  21. });
  22. const refForm = useRef();
  23. const [inputsRow, setInputsRow] = useState(inputs);
  24. function onSubmit() {
  25. const updateArray = compareObjects(inputs, inputsRow);
  26. if (!updateArray.length) return showWarning('你似乎并没有修改什么');
  27. const requestQueue = updateArray.map((item) => {
  28. let value = '';
  29. if (typeof inputs[item.key] === 'boolean') {
  30. value = String(inputs[item.key]);
  31. } else {
  32. value = inputs[item.key];
  33. }
  34. return API.put('/api/option/', {
  35. key: item.key,
  36. value,
  37. });
  38. });
  39. setLoading(true);
  40. Promise.all(requestQueue)
  41. .then((res) => {
  42. if (requestQueue.length === 1) {
  43. if (res.includes(undefined)) return;
  44. } else if (requestQueue.length > 1) {
  45. if (res.includes(undefined)) return showError('部分保存失败,请重试');
  46. }
  47. showSuccess('保存成功');
  48. props.refresh();
  49. })
  50. .catch(() => {
  51. showError('保存失败,请重试');
  52. })
  53. .finally(() => {
  54. setLoading(false);
  55. });
  56. }
  57. useEffect(() => {
  58. const currentInputs = {};
  59. for (let key in props.options) {
  60. if (Object.keys(inputs).includes(key)) {
  61. currentInputs[key] = props.options[key];
  62. }
  63. }
  64. setInputs(currentInputs);
  65. setInputsRow(structuredClone(currentInputs));
  66. refForm.current.setValues(currentInputs);
  67. localStorage.setItem(
  68. 'data_export_default_time',
  69. String(inputs.DataExportDefaultTime),
  70. );
  71. }, [props.options]);
  72. return (
  73. <>
  74. <Spin spinning={loading}>
  75. <Form
  76. values={inputs}
  77. getFormApi={(formAPI) => (refForm.current = formAPI)}
  78. style={{ marginBottom: 15 }}
  79. >
  80. <Form.Section text={'数据看板设置'}>
  81. <Row gutter={16}>
  82. <Col span={8}>
  83. <Form.Switch
  84. field={'DataExportEnabled'}
  85. label={'启用数据看板(实验性)'}
  86. size='large'
  87. checkedText='|'
  88. uncheckedText='〇'
  89. onChange={(value) => {
  90. setInputs({
  91. ...inputs,
  92. DataExportEnabled: value,
  93. });
  94. }}
  95. />
  96. </Col>
  97. </Row>
  98. <Row>
  99. <Col span={8}>
  100. <Form.InputNumber
  101. label={'数据看板更新间隔 '}
  102. step={1}
  103. min={1}
  104. suffix={'分钟'}
  105. extraText={'设置过短会影响数据库性能'}
  106. placeholder={'数据看板更新间隔'}
  107. field={'DataExportInterval'}
  108. onChange={(value) =>
  109. setInputs({
  110. ...inputs,
  111. DataExportInterval: String(value),
  112. })
  113. }
  114. />
  115. </Col>
  116. <Col span={8}>
  117. <Form.Select
  118. label='数据看板默认时间粒度'
  119. optionList={optionsDataExportDefaultTime}
  120. field={'DataExportDefaultTime'}
  121. extraText={'仅修改展示粒度,统计精确到小时'}
  122. placeholder={'数据看板默认时间粒度'}
  123. style={{ width: 180 }}
  124. onChange={(value) =>
  125. setInputs({
  126. ...inputs,
  127. DataExportDefaultTime: String(value),
  128. })
  129. }
  130. />
  131. </Col>
  132. </Row>
  133. <Row>
  134. <Button size='large' onClick={onSubmit}>
  135. 保存数据看板设置
  136. </Button>
  137. </Row>
  138. </Form.Section>
  139. </Form>
  140. </Spin>
  141. </>
  142. );
  143. }