Browse Source

actualizar

hiesoftrd 1 năm trước cách đây
mục cha
commit
320395a096
2 tập tin đã thay đổi với 92 bổ sung0 xóa
  1. 56 0
      frontend/src/errors/toastError.js
  2. 36 0
      frontend/src/helpers/contrastColor.js

+ 56 - 0
frontend/src/errors/toastError.js

@@ -0,0 +1,56 @@
+import { toast } from "react-toastify";
+import { i18n } from "../translate/i18n";
+import { isString } from 'lodash';
+
+const toastError = err => {
+    const errorMsg = err.response?.data?.error;
+    if (errorMsg) {
+        if (i18n.exists(`backendErrors.${errorMsg}`)) {
+            console.error(`Error: ${i18n.t(`backendErrors.${errorMsg}`)}`);
+            // Optionally log the error to an external service here
+            
+            toast.error(i18n.t(`backendErrors.${errorMsg}`), {
+                toastId: errorMsg,
+                autoClose: 2000,
+                hideProgressBar: false,
+                closeOnClick: true,
+                pauseOnHover: false,
+                draggable: true,
+                progress: undefined,
+                theme: "light",
+            });
+            
+            return;
+        } else {
+            
+            toast.error(errorMsg, {
+                toastId: errorMsg,
+                autoClose: 2000,
+                hideProgressBar: false,
+                closeOnClick: true,
+                pauseOnHover: false,
+                draggable: true,
+                progress: undefined,
+                theme: "light",
+            });
+            
+            return;
+        }
+    } if (isString(err)) {
+        console.error(`Error: ${err}`);
+        // Optionally log the error to an external service here
+        /*
+        toast.error(err);
+        */
+        return;
+    } else {
+        console.error("An error occurred!");
+        // Optionally log the error to an external service here
+        /*
+        toast.error("An error occurred!");
+        */
+        return;
+    }
+};
+
+export default toastError;

+ 36 - 0
frontend/src/helpers/contrastColor.js

@@ -0,0 +1,36 @@
+function getRGB(c) {
+  return parseInt(c, 16) || c
+}
+
+function getsRGB(c) {
+  return getRGB(c) / 255 <= 0.03928
+    ? getRGB(c) / 255 / 12.92
+    : Math.pow((getRGB(c) / 255 + 0.055) / 1.055, 2.4)
+}
+
+function getLuminance(hexColor) {
+  return (
+    0.2126 * getsRGB(hexColor.substr(1, 2)) +
+    0.7152 * getsRGB(hexColor.substr(3, 2)) +
+    0.0722 * getsRGB(hexColor.substr(-2))
+  )
+}
+
+function getContrast(f, b) {
+  const L1 = getLuminance(f)
+  const L2 = getLuminance(b)
+  return (Math.max(L1, L2) + 0.05) / (Math.min(L1, L2) + 0.05)
+}
+
+function getTextColor(bgColor) {
+  const whiteContrast = getContrast(bgColor, '#ffffff')
+  const blackContrast = getContrast(bgColor, '#000000')
+
+  return whiteContrast > blackContrast ? '#ffffff' : '#000000'
+}
+
+const contrastColor = (color) => {
+    return getTextColor(color);
+}
+
+export default contrastColor;