From aedf523be9b0faf9cdd7a2231912a074fccdb262 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Mon, 8 Jul 2024 15:00:29 +0700
Subject: <iman> update salin voucher

---
 src-migrate/modules/promo/components/Voucher.tsx | 54 ++++++++++++++++++------
 1 file changed, 42 insertions(+), 12 deletions(-)

(limited to 'src-migrate/modules/promo/components/Voucher.tsx')

diff --git a/src-migrate/modules/promo/components/Voucher.tsx b/src-migrate/modules/promo/components/Voucher.tsx
index 397134e8..c978dc7b 100644
--- a/src-migrate/modules/promo/components/Voucher.tsx
+++ b/src-migrate/modules/promo/components/Voucher.tsx
@@ -7,7 +7,6 @@ import Image from 'next/image'
 import { useToast } from '@chakra-ui/react'
 import useDevice from '@/core/hooks/useDevice';
 
-
 const Voucher = () => {
   const { isMobile } = useDevice();
   const toast = useToast();
@@ -26,18 +25,50 @@ const Voucher = () => {
     slidesPerView: isMobile ? 1.2 : 3,
     spaceBetween: 16
   }
+  
   const vouchers = useMemo(() => voucherQuery.data || [], [voucherQuery.data]);
 
   const copyText = (text: string) => {
-    navigator.clipboard.writeText(text)
-    toast({
-      title: 'Salin ke papan klip',
-      description: 'Kode voucher berhasil disalin',
-      status: 'success',
-      duration: 3000,
-      isClosable: true,
-      position: 'bottom',
-    })
+    if (navigator.clipboard && navigator.clipboard.writeText) {
+      navigator.clipboard.writeText(text)
+        .then(() => {
+          toast({
+            title: 'Salin ke papan klip',
+            description: 'Kode voucher berhasil disalin',
+            status: 'success',
+            duration: 3000,
+            isClosable: true,
+            position: 'bottom',
+          })
+        })
+        .catch(() => {
+          fallbackCopyTextToClipboard(text);
+        });
+    } else {
+      fallbackCopyTextToClipboard(text);
+    }
+  }
+
+  const fallbackCopyTextToClipboard = (text: string) => {
+    const textArea = document.createElement("textarea");
+    textArea.value = text;
+    document.body.appendChild(textArea);
+    textArea.focus();
+    textArea.select();
+    try {
+      document.execCommand('copy');
+      toast({
+        title: 'Salin ke papan klip',
+        description: 'Kode voucher berhasil disalin',
+        status: 'success',
+        duration: 3000,
+        isClosable: true,
+        position: 'bottom',
+      })
+    } catch (err) {
+      console.error('Fallback: Oops, unable to copy', err);
+    }
+    document.body.removeChild(textArea);
   }
 
   return (
@@ -78,9 +109,8 @@ const Voucher = () => {
           </Swiper>
         </div>
       )}
-
     </>
   )
 }
 
-export default Voucher
\ No newline at end of file
+export default Voucher
-- 
cgit v1.2.3

