選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

194 行
5.6 KiB

  1. import { toast } from "react-toastify";
  2. import DialogTitle from "@mui/material/DialogTitle";
  3. import DialogContent from "@mui/material/DialogContent";
  4. import DialogContentText from "@mui/material/DialogContentText";
  5. import DialogActions from "@mui/material/DialogActions";
  6. import { Button } from "@mui/material";
  7. import Dialog from "@mui/material/Dialog";
  8. import * as React from "react";
  9. export const clickableLink=(link, label)=> {
  10. return <a href={link}>{label}</a>;
  11. }
  12. export const handleActionKeyDown = (event, action) => {
  13. if (event.key === 'Enter' || event.key === ' ') {
  14. event.preventDefault();
  15. action(event);
  16. }
  17. };
  18. export function getDeletedRecordWithRefList(referenceList, updatedList) {
  19. return referenceList.filter(x => !updatedList.includes(x));
  20. }
  21. export function getIdList(input) {
  22. const output = input.map(function (obj) {
  23. return obj.id;
  24. });
  25. return output;
  26. }
  27. export function getObjectById(list, id) {
  28. const obj = list.find((element) => {
  29. return element.id === id;
  30. });
  31. return obj === undefined || Object.keys(obj).length <= 0 ? null : obj
  32. }
  33. export function getObjectByValue(list, valueName, value) {
  34. const obj = list.find((element) => {
  35. return element[valueName] === parseInt(value);
  36. });
  37. // console.log(obj);
  38. return obj === undefined || Object.keys(obj).length <= 0 ? null : obj
  39. }
  40. export function getObjectByType(list, valueName, value) {
  41. // console.log(list)
  42. // console.log(valueName)
  43. // console.log(value)
  44. const obj = list.find((element) => {
  45. // console.log(element[valueName])
  46. return element[valueName] === value;
  47. });
  48. // console.log(obj);
  49. return obj === undefined || Object.keys(obj).length <= 0 ? null : obj
  50. }
  51. export function removeObjectWithId(arr, id) {
  52. const arrCopy = Array.from(arr);
  53. const objWithIdIndex = arrCopy.findIndex((obj) => obj.id === id);
  54. arrCopy.splice(objWithIdIndex, 1);
  55. return arrCopy;
  56. }
  57. export function getComboValueByLabel(comboList, input) {
  58. for (let i = 0; i < comboList.length; i++) {
  59. if (comboList[i].label === input) {
  60. return comboList[i];
  61. }
  62. }
  63. return input === undefined ? null : input;
  64. }
  65. export function getDateString(queryDateArray) {
  66. return (
  67. queryDateArray[0]
  68. + "-" +
  69. queryDateArray[1]
  70. + "-" +
  71. queryDateArray[2]
  72. + " " +
  73. queryDateArray[3]
  74. + ":" +
  75. queryDateArray[4]
  76. + ":" +
  77. queryDateArray[5]
  78. )
  79. }
  80. const manualCloseToastOptions = {
  81. position: "bottom-right",
  82. autoClose: 30000,
  83. hideProgressBar: false,
  84. closeOnClick: true,
  85. pauseOnHover: true,
  86. draggable: true,
  87. progress: undefined,
  88. theme: "light",
  89. };
  90. export const notifySaveSuccess = () => {
  91. const userType = JSON.parse(localStorage.getItem("userData")).type;
  92. const msg = userType === "GLD" ? "Save success!" : "儲存成功!";
  93. toast.success(msg, manualCloseToastOptions);
  94. };
  95. export const notifyCreateSuccess = () => {
  96. const userType = JSON.parse(localStorage.getItem("userData")).type;
  97. const msg = userType === "GLD" ? "Create success!" : "創建成功!";
  98. toast.success(msg, manualCloseToastOptions);
  99. };
  100. export const notifyVerifySuccess = () => {
  101. const userType = JSON.parse(localStorage.getItem("userData")).type;
  102. const msg = userType === "GLD" ? "Verify success!" : "驗證成功!";
  103. toast.success(msg, manualCloseToastOptions);
  104. };
  105. export const notifyDeleteSuccess = () => {
  106. const userType = JSON.parse(localStorage.getItem("userData")).type;
  107. const msg = userType === "GLD" ? "Delete success!" : "刪除成功!";
  108. toast.success(msg, manualCloseToastOptions);
  109. };
  110. export const notifyLockSuccess = () => {
  111. toast.success("Lock success!", manualCloseToastOptions);
  112. };
  113. export const notifyUnlockSuccess = () => {
  114. toast.success("Unlock success!", manualCloseToastOptions);
  115. };
  116. export const notifyActiveSuccess = () => {
  117. toast.success("Active success!", manualCloseToastOptions);
  118. };
  119. export const notifyDownloadSuccess = () => {
  120. const userType = JSON.parse(localStorage.getItem("userData")).type;
  121. const msg = userType === "GLD" ? "Download success!" : "下載成功!";
  122. toast.success(msg, manualCloseToastOptions);
  123. };
  124. export const notifyActionSuccess = (actionMsg) => {
  125. toast.success(actionMsg ?? "Success", manualCloseToastOptions);
  126. };
  127. export const notifyActionError = (actionMsg) => {
  128. toast.error(`${actionMsg}`, manualCloseToastOptions);
  129. };
  130. export const notifyActionWarning = (actionMsg) => {
  131. toast.warn(`${actionMsg}`, manualCloseToastOptions);
  132. }
  133. export function prettyJson(json) {
  134. // console.log(json);
  135. // console.log(JSON.stringify(json, null, 2));
  136. return (
  137. <div>{JSON.stringify(json, null, 2)}</div>
  138. );
  139. }
  140. export function GeneralConfirmWindow({
  141. isWindowOpen,
  142. title,
  143. content,
  144. onNormalClose,
  145. onConfirmClose }) {
  146. return (
  147. <Dialog
  148. open={isWindowOpen}
  149. onClose={onNormalClose}
  150. aria-labelledby="alert-dialog-title"
  151. aria-describedby="alert-dialog-description"
  152. >
  153. <DialogTitle id="alert-dialog-title">
  154. {title}
  155. </DialogTitle>
  156. <DialogContent>
  157. <DialogContentText id="alert-dialog-description">
  158. {content}
  159. </DialogContentText>
  160. </DialogContent>
  161. <DialogActions>
  162. <Button onClick={onNormalClose}>Cancel</Button>
  163. <Button onClick={onConfirmClose} autoFocus>
  164. Confirm
  165. </Button>
  166. </DialogActions>
  167. </Dialog>
  168. )
  169. }