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.

CommonFunction.js 6.2 KiB

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