From a369386922ada4e96ab268494343c0d40e0433ec Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Fri, 24 Apr 2026 17:22:20 +0700 Subject: [PATCH] feat: add share to whatsapp after submitting daily checklist --- .../daily-checklist/DailyChecklistContent.tsx | 78 ++++++++++++++++++- 1 file changed, 75 insertions(+), 3 deletions(-) diff --git a/src/figma-make/components/pages/daily-checklist/DailyChecklistContent.tsx b/src/figma-make/components/pages/daily-checklist/DailyChecklistContent.tsx index 4024b581..b83c7b6a 100644 --- a/src/figma-make/components/pages/daily-checklist/DailyChecklistContent.tsx +++ b/src/figma-make/components/pages/daily-checklist/DailyChecklistContent.tsx @@ -53,7 +53,6 @@ import { useRouter, useSearchParams, usePathname } from 'next/navigation'; import { Icon } from '@iconify/react'; import { DailyChecklistKandangApi } from '@/services/api/daily-checklist/kandang'; -// Static categories const CATEGORIES = [ { value: 'pullet_open', label: 'Pullet Open' }, { value: 'pullet_close', label: 'Pullet Close' }, @@ -62,6 +61,14 @@ const CATEGORIES = [ { value: 'empty_kandang', label: 'Kandang Kosong' }, ]; +const CATEGORY_LABELS: { [key: string]: string } = { + pullet_open: 'Pullet Open', + pullet_close: 'Pullet Close', + produksi_open: 'Produksi Open', + produksi_close: 'Produksi Close', + empty_kandang: 'Kandang Kosong', +}; + const TIME_TYPE_ORDER = ['Umum', 'Pagi', 'Siang', 'Sore', 'Malam']; const TIME_TYPE_LABELS: { [key: string]: string } = { Umum: 'Umum', @@ -246,7 +253,6 @@ export function DailyChecklistContent() { } }, [selectedCategory]); - // Format date for display const formatDateForDisplay = (dateStr: string) => { if (!dateStr) return 'Pilih tanggal'; const [year, month, day] = dateStr.split('-'); @@ -259,6 +265,36 @@ export function DailyChecklistContent() { }); }; + const formatDate = (dateString: string) => { + const date = new Date(dateString); + return date.toLocaleDateString('id-ID', { + day: '2-digit', + month: 'long', + year: 'numeric', + }); + }; + + const isMobileDevice = () => { + return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test( + navigator.userAgent + ); + }; + + const getStatusMessage = () => { + switch (checklistStatus) { + case 'DRAFT': + return 'Checklist harian perlu disubmit'; + case 'SUBMITTED': + return 'Checklist harian menunggu persetujuan'; + case 'APPROVED': + return 'Checklist harian telah disetujui'; + case 'REJECTED': + return 'Checklist harian telah ditolak'; + default: + return ''; + } + }; + // Fetch master data on mount useEffect(() => { setInitialLoading(false); @@ -842,7 +878,43 @@ export function DailyChecklistContent() { } setChecklistStatus('SUBMITTED'); - toast.success('Checklist berhasil disubmit untuk approval'); + + const shareToWhatsApp = () => { + const kandangName = kandangOptions.find( + (k) => String(k.value) === kandangId + )?.label || kandangId; + const statusMsg = getStatusMessage(); + const category = selectedCategory || ''; + const message = encodeURIComponent( + `Daily Checklist\n\nTanggal: ${formatDate(date)}\nKandang: ${kandangName}\nKategori: ${CATEGORY_LABELS[category] || category}\nStatus: SUBMITTED${statusMsg ? ` - ${statusMsg}` : ''}\n\nLihat detail lengkap: ${window.location.href}` + ); + + const isMobile = isMobileDevice(); + const whatsappUrl = isMobile + ? `https://wa.me/?text=${message}` + : `https://web.whatsapp.com/send?text=${message}`; + + window.open(whatsappUrl, '_blank'); + }; + + toast.success('Checklist berhasil disubmit untuk approval', { + action: { + label: 'Bagikan ke WhatsApp', + onClick: shareToWhatsApp, + }, + description: ( + + ), + }); } catch (error) { console.error('Error submitting:', error); toast.error('Terjadi kesalahan');