|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import React, { useState } from 'react'; |
| 4 | +import { |
| 5 | + SimpleDialog as Dialog, |
| 6 | + SimpleDialogContent as DialogContent, |
| 7 | + SimpleDialogDescription as DialogDescription, |
| 8 | +} from '../ui/simple-dialog'; |
| 9 | +import { Button } from '../ui/button'; |
| 10 | +import { Loader2, Heart } from 'lucide-react'; |
| 11 | +import { sendGratitude } from '../../features/email/api/gratitudeApi'; |
| 12 | +import { useEntries } from '../../features/statements/hooks/useEntries'; |
| 13 | +import { Action } from '../../types/entries'; |
| 14 | + |
| 15 | +interface GratitudeModalProps { |
| 16 | + onClose: () => void; |
| 17 | + statementId: string; |
| 18 | + action: Action; |
| 19 | + onGratitudeSent: ( |
| 20 | + statementId: string, |
| 21 | + actionId: string, |
| 22 | + message: string |
| 23 | + ) => void; |
| 24 | +} |
| 25 | + |
| 26 | +const GratitudeModal: React.FC<GratitudeModalProps> = ({ |
| 27 | + onClose, |
| 28 | + statementId, |
| 29 | + action, |
| 30 | + onGratitudeSent, |
| 31 | +}) => { |
| 32 | + const { data } = useEntries(); |
| 33 | + const { managerName, managerEmail } = data; |
| 34 | + |
| 35 | + const [message, setMessage] = useState(''); |
| 36 | + const [isSending, setIsSending] = useState(false); |
| 37 | + const [sendError, setSendError] = useState<string | null>(null); |
| 38 | + const [sendSuccess, setSendSuccess] = useState(false); |
| 39 | + |
| 40 | + const handleSendGratitude = async () => { |
| 41 | + try { |
| 42 | + setIsSending(true); |
| 43 | + setSendError(null); |
| 44 | + |
| 45 | + if (!managerEmail || managerEmail.trim() === '') { |
| 46 | + throw new Error( |
| 47 | + 'Manager email is not set. Please set a manager email first.' |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + if (!message.trim()) { |
| 52 | + throw new Error('Please enter a gratitude message.'); |
| 53 | + } |
| 54 | + |
| 55 | + const gratitudeRequest = { |
| 56 | + statementId, |
| 57 | + actionId: action.id, |
| 58 | + message: message.trim(), |
| 59 | + recipientEmail: managerEmail, |
| 60 | + recipientName: managerName, |
| 61 | + }; |
| 62 | + |
| 63 | + // Check if we're in mock mode |
| 64 | + const isMockMode = |
| 65 | + typeof import.meta.env.VITE_MOCK_EMAIL_SENDING === 'undefined' || |
| 66 | + import.meta.env.VITE_MOCK_EMAIL_SENDING === 'true'; |
| 67 | + |
| 68 | + try { |
| 69 | + // Try to send gratitude via API |
| 70 | + await sendGratitude(gratitudeRequest); |
| 71 | + } catch (apiError) { |
| 72 | + // If we're in mock mode, continue as if successful |
| 73 | + if (!isMockMode) { |
| 74 | + // Only throw the error if we're not in mock mode |
| 75 | + throw apiError; |
| 76 | + } |
| 77 | + console.warn('API error in mock mode (continuing anyway):', apiError); |
| 78 | + } |
| 79 | + |
| 80 | + // Always mark the action as having gratitude sent |
| 81 | + // (This works regardless of API success when in mock mode) |
| 82 | + onGratitudeSent(statementId, action.id, message); |
| 83 | + |
| 84 | + setSendSuccess(true); |
| 85 | + |
| 86 | + // Automatically close after successful send |
| 87 | + setTimeout(() => onClose(), 2000); |
| 88 | + } catch (error) { |
| 89 | + console.error('Failed to send gratitude:', error); |
| 90 | + setSendError( |
| 91 | + typeof error === 'object' && error !== null && 'message' in error |
| 92 | + ? (error as Error).message |
| 93 | + : 'Failed to send gratitude email. Please try again later.' |
| 94 | + ); |
| 95 | + } finally { |
| 96 | + setIsSending(false); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + return ( |
| 101 | + <> |
| 102 | + <Dialog open onOpenChange={onClose}> |
| 103 | + <DialogContent headerTitle='Send Gratitude'> |
| 104 | + <div |
| 105 | + className='relative rounded-md overflow-hidden' |
| 106 | + style={{ |
| 107 | + border: '8px solid transparent', |
| 108 | + borderImage: |
| 109 | + 'repeating-linear-gradient(45deg, #ff69b4, #ff69b4 10px, #fb86b0 10px, #fb86b0 20px) 8', |
| 110 | + padding: '16px', |
| 111 | + margin: '-8px', |
| 112 | + background: '#f9f9f9', |
| 113 | + }} |
| 114 | + onClick={(e) => e.stopPropagation()} |
| 115 | + > |
| 116 | + {/* Heart decoration */} |
| 117 | + <div className='absolute top-4 right-4 text-pink-400 opacity-20 pointer-events-none'> |
| 118 | + <Heart size={40} /> |
| 119 | + </div> |
| 120 | + |
| 121 | + <DialogDescription className='mt-0 text-center'> |
| 122 | + Express gratitude for this action |
| 123 | + </DialogDescription> |
| 124 | + |
| 125 | + <div className='text-center mb-4'> |
| 126 | + <p className='text-sm text-gray-500 mt-1'> |
| 127 | + To: {managerName || managerEmail || 'No recipient set'} |
| 128 | + </p> |
| 129 | + {sendSuccess && ( |
| 130 | + <p className='text-green-600 text-sm font-medium mt-2 bg-green-50 py-1 px-2 rounded-full inline-block'> |
| 131 | + ✓ Gratitude sent successfully! |
| 132 | + </p> |
| 133 | + )} |
| 134 | + </div> |
| 135 | + |
| 136 | + {sendError && ( |
| 137 | + <div className='bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded mb-4'> |
| 138 | + {sendError} |
| 139 | + </div> |
| 140 | + )} |
| 141 | + |
| 142 | + <div className='mb-6 p-4 bg-white rounded-md shadow-sm border border-pink-100'> |
| 143 | + <h3 className='font-semibold text-gray-700 mb-2 flex items-center'> |
| 144 | + <span className='mr-2'>→</span> |
| 145 | + Action |
| 146 | + </h3> |
| 147 | + <p className='text-gray-600'>{action.action}</p> |
| 148 | + {action.byDate && ( |
| 149 | + <p className='text-xs text-gray-500 mt-1'> |
| 150 | + Due by: {action.byDate} |
| 151 | + </p> |
| 152 | + )} |
| 153 | + </div> |
| 154 | + |
| 155 | + <div className='mb-6'> |
| 156 | + <label |
| 157 | + htmlFor='gratitude-message' |
| 158 | + className='block text-sm font-medium text-gray-700 mb-2' |
| 159 | + > |
| 160 | + Your gratitude message: |
| 161 | + </label> |
| 162 | + <textarea |
| 163 | + id='gratitude-message' |
| 164 | + rows={4} |
| 165 | + className='w-full rounded-md border border-pink-200 focus:border-pink-500 focus:ring-pink-500 text-gray-900 p-3' |
| 166 | + placeholder='Express your thanks and appreciation here...' |
| 167 | + value={message} |
| 168 | + onChange={(e) => setMessage(e.target.value)} |
| 169 | + /> |
| 170 | + <p className='text-xs text-gray-500 mt-1'> |
| 171 | + This message will be sent with your gratitude email. |
| 172 | + </p> |
| 173 | + </div> |
| 174 | + |
| 175 | + <div className='mt-6 flex justify-center gap-4'> |
| 176 | + <Button |
| 177 | + variant='default' |
| 178 | + onClick={handleSendGratitude} |
| 179 | + disabled={isSending || !message.trim() || !managerEmail} |
| 180 | + className='shadow-sm bg-pink-600 hover:bg-pink-700 text-white' |
| 181 | + > |
| 182 | + {isSending ? ( |
| 183 | + <> |
| 184 | + <Loader2 className='mr-2 h-4 w-4 animate-spin' /> |
| 185 | + <span>Sending...</span> |
| 186 | + </> |
| 187 | + ) : ( |
| 188 | + <> |
| 189 | + <Heart className='mr-2 h-4 w-4' /> |
| 190 | + <span>Send Gratitude</span> |
| 191 | + </> |
| 192 | + )} |
| 193 | + </Button> |
| 194 | + <Button variant='outline' onClick={onClose}> |
| 195 | + <span>Cancel</span> |
| 196 | + </Button> |
| 197 | + </div> |
| 198 | + </div> |
| 199 | + </DialogContent> |
| 200 | + </Dialog> |
| 201 | + </> |
| 202 | + ); |
| 203 | +}; |
| 204 | + |
| 205 | +export default GratitudeModal; |
0 commit comments