You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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