No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

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