Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

198 wiersze
5.9 KiB

  1. // material-ui
  2. import * as React from 'react';
  3. import {
  4. GridActionsCellItem,
  5. } from "@mui/x-data-grid";
  6. import * as Icon from '../utils/IconUtils';
  7. import * as HttpUtils from "../utils/HttpUtils"
  8. import * as UrlUtils from "../utils/ApiPathConst"
  9. import * as DateUtils from "../utils/DateUtils"
  10. import { FiDataGrid } from './FiDataGrid';
  11. // ==============================|| EVENT TABLE ||============================== //
  12. export default function FileList({ refType, refId, allowDelete, sx, dateHideable, ...props }) {
  13. const [rows, setRows] = React.useState([]);
  14. const [rowModesModel] = React.useState({});
  15. React.useEffect(() => {
  16. loadData();
  17. }, []);
  18. const onDeleteClick = (fileId, skey, filename) => () => {
  19. HttpUtils.get(
  20. {
  21. url: UrlUtils.GET_FILE_DELETE,
  22. params: {
  23. fileId: fileId,
  24. skey: skey,
  25. filename: filename
  26. },
  27. onSuccess: function () {
  28. loadData();
  29. }
  30. }
  31. );
  32. };
  33. const onDownloadClick = (fileId, skey, filename) => () => {
  34. HttpUtils.fileDownload({
  35. fileId: fileId,
  36. skey: skey,
  37. filename: filename,
  38. });
  39. };
  40. const loadData = () => {
  41. HttpUtils.post(
  42. {
  43. url: UrlUtils.POST_FILE_LIST,
  44. params: {
  45. refType: refType,
  46. refId:refId,
  47. },
  48. onSuccess: function (responseData) {
  49. setRows(responseData.records);
  50. }
  51. }
  52. );
  53. };
  54. const convertToStr = (bytes, decimals) => {
  55. if (bytes == 0) return '0 Bytes';
  56. var dm = decimals || 2,
  57. sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  58. let i = 0;
  59. i = Math.floor(Math.log(bytes) / Math.log(1024));
  60. return parseFloat((bytes / Math.pow(1024, i)).toFixed(dm)) + ' ' + sizes[i];
  61. }
  62. const columns = (dateHideable ?
  63. [
  64. {
  65. field: 'actions',
  66. type: 'actions',
  67. headerName: '',
  68. width: 50,
  69. cellClassName: 'actions',
  70. getActions: (params) => {
  71. return [
  72. <GridActionsCellItem
  73. key="OutSave"
  74. icon={<Icon.Download />}
  75. label="Download"
  76. className="textPrimary"
  77. onClick={onDownloadClick(params.id, params.row.skey, params.row.filename)}
  78. color="primary"
  79. />]
  80. },
  81. },
  82. {
  83. id: 'filename',
  84. field: 'filename',
  85. headerName: 'filename',
  86. flex: 3,
  87. },
  88. {
  89. id: 'filesize',
  90. field: 'filesize',
  91. headerName: 'filesize',
  92. flex: 1,
  93. valueGetter: (params) => {
  94. return convertToStr(params.value);
  95. }
  96. }
  97. ]
  98. :
  99. [
  100. {
  101. id: 'created',
  102. field: 'created',
  103. headerName: 'created',
  104. flex: 1,
  105. valueGetter: (params) => {
  106. return DateUtils.datetimeStr(params.value);
  107. }
  108. },
  109. {
  110. field: 'actions',
  111. type: 'actions',
  112. headerName: '',
  113. width: 50,
  114. cellClassName: 'actions',
  115. getActions: (params) => {
  116. return [
  117. <GridActionsCellItem
  118. key="OutSave"
  119. icon={<Icon.Download />}
  120. label="Download"
  121. className="textPrimary"
  122. onClick={onDownloadClick(params.id, params.row.skey, params.row.filename)}
  123. color="primary"
  124. />]
  125. },
  126. },
  127. {
  128. id: 'filename',
  129. field: 'filename',
  130. headerName: 'filename',
  131. flex: 3,
  132. },
  133. {
  134. id: 'filesize',
  135. field: 'filesize',
  136. headerName: 'filesize',
  137. flex: 1,
  138. valueGetter: (params) => {
  139. return convertToStr(params.value);
  140. }
  141. },
  142. ]
  143. );
  144. if (allowDelete) {
  145. columns.push({
  146. field: 'actions',
  147. type: 'actions',
  148. headerName: 'Delete',
  149. width: 100,
  150. cellClassName: 'actions',
  151. getActions: (params) => {
  152. return [
  153. <GridActionsCellItem
  154. key="OutSave"
  155. icon={<Icon.Delete />}
  156. label="Delete"
  157. className="textPrimary"
  158. onClick={onDeleteClick(params.id, params.row.skey, params.row.filename)}
  159. color="primary"
  160. />]
  161. },
  162. });
  163. }
  164. return (
  165. // <div style={{height: 400, width: '100%'}}>
  166. <FiDataGrid
  167. {...props}
  168. hideFooterSelectedRowCount={true}
  169. sx={sx}
  170. rows={rows}
  171. columns={columns}
  172. editMode="row"
  173. rowModesModel={rowModesModel}
  174. initialState={{
  175. pagination: {
  176. paginationModel: { page: 0, pageSize: 5 },
  177. },
  178. }}
  179. pageSizeOptions={[5, 10]}
  180. autoHeight={true}
  181. />
  182. // </div>
  183. );
  184. }