All files / js/components EmailRecipientsInput.vue

0% Statements 0/70
0% Branches 0/64
0% Functions 0/23
0% Lines 0/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
<template>
    <div>
        <label class="block text-sm font-medium text-gray-700 mb-1">Email Address(es) *</label>
        <div class="min-h-[42px] rounded border px-3 py-2"
             :class="{ 'border-red-500': error }">
            <div class="flex flex-wrap gap-2">
                <span
                    v-for="(email, idx) in modelValue"
                    :key="`${email}-${idx}`"
                    class="inline-flex items-center gap-1 px-2 py-1 rounded-md bg-gray-100 text-sm text-gray-800"
                >
                    {{ email }}
                    <button
                        type="button"
                        @click="remove(idx)"
                        class="text-gray-500 hover:text-red-600 focus:outline-none"
                        aria-label="Remove recipient"
                    >
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                        </svg>
                    </button>
                </span>
                <button
                    type="button"
                    @click="showAddModal = true"
                    class="inline-flex items-center gap-1 px-2 py-1 rounded-md border border-dashed border-gray-300 text-sm text-gray-600 hover:border-blue-400 hover:text-blue-600 focus:outline-none focus:ring-1 focus:ring-blue-500"
                >
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                    </svg>
                    Add email/contact
                </button>
            </div>
        </div>
        <div v-if="error" class="text-red-500 text-sm mt-1">{{ error }}</div>
 
        <!-- Add recipient modal -->
        <div
            v-if="showAddModal"
            class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-[60]"
            @click.self="showAddModal = false"
        >
            <div class="bg-white rounded-lg p-6 w-full max-w-md shadow-xl" @click.stop>
                <h3 class="text-lg font-semibold mb-4">Add Recipient</h3>
                <div class="space-y-4">
                    <!-- Tabs: Contact or Manual -->
                    <div class="flex gap-2 border-b border-gray-200">
                        <button
                            type="button"
                            :class="addMode === 'contact' ? 'border-blue-600 text-blue-600' : 'border-transparent text-gray-500 hover:text-gray-700'"
                            class="px-3 py-2 text-sm font-medium border-b-2 -mb-px"
                            @click="addMode = 'contact'"
                        >
                            From contact
                        </button>
                        <button
                            type="button"
                            :class="addMode === 'email' ? 'border-blue-600 text-blue-600' : 'border-transparent text-gray-500 hover:text-gray-700'"
                            class="px-3 py-2 text-sm font-medium border-b-2 -mb-px"
                            @click="addMode = 'email'"
                        >
                            Enter email
                        </button>
                    </div>
 
                    <!-- Contact search -->
                    <div v-if="addMode === 'contact'" class="space-y-2">
                        <p v-if="!customerId" class="text-sm text-amber-600">
                            No customer linked. Enter an email manually or link a customer to the document.
                        </p>
                        <template v-else>
                            <input
                                v-model="contactSearchQuery"
                                @input="searchContacts"
                                type="text"
                                placeholder="Search contact by name, email, or phone"
                                class="w-full rounded border px-3 py-2"
                            />
                            <div
                                v-if="contactSearchQuery && filteredContacts.length > 0"
                                class="max-h-40 overflow-auto border rounded mt-1"
                            >
                                <div
                                    v-for="c in filteredContacts"
                                    :key="c.id"
                                    @click="addFromContact(c)"
                                    class="px-3 py-2 hover:bg-gray-100 cursor-pointer flex justify-between items-center"
                                >
                                    <div>
                                        <span class="font-medium">{{ c.name }}</span>
                                        <span v-if="c.email" class="text-gray-500 text-sm ml-2">{{ c.email }}</span>
                                    </div>
                                    <span v-if="isAlreadyAdded(c.email)" class="text-xs text-amber-600">Already added</span>
                                </div>
                            </div>
                        </template>
                    </div>
 
                    <!-- Manual email -->
                    <div v-else class="space-y-2">
                        <input
                            v-model="manualEmail"
                            type="email"
                            placeholder="email@example.com"
                            class="w-full rounded border px-3 py-2"
                            @keydown.enter.prevent="addManualEmail"
                        />
                        <button
                            type="button"
                            @click="addManualEmail"
                            :disabled="!isValidEmail(manualEmail)"
                            class="w-full rounded bg-blue-600 px-4 py-2 text-white hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed"
                        >
                            Add
                        </button>
                    </div>
                </div>
                <div class="mt-4 flex justify-end">
                    <button
                        type="button"
                        @click="closeAddModal"
                        class="rounded border px-4 py-2 hover:bg-gray-50"
                    >
                        Close
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>
 
<script setup lang="ts">
import { ref, watch } from 'vue';
import axios from 'axios';
 
interface Contact {
    id: number;
    customer_id: number;
    name: string;
    email?: string | null;
    phone?: string | null;
}
 
const props = withDefaults(
    defineProps<{
        modelValue: string[];
        customerId: number | null;
        error?: string;
    }>(),
    { error: '' }
);
 
const emit = defineEmits<{
    (e: 'update:modelValue', value: string[]): void;
}>();
 
const showAddModal = ref(false);
const addMode = ref<'contact' | 'email'>('contact');
const contactSearchQuery = ref('');
const manualEmail = ref('');
const filteredContacts = ref<Contact[]>([]);
let searchTimeout: ReturnType<typeof setTimeout> | null = null;
 
function remove(idx: number) {
    const next = [...props.modelValue];
    next.splice(idx, 1);
    emit('update:modelValue', next);
}
 
function addEmail(email: string) {
    const trimmed = email.trim().toLowerCase();
    if (!trimmed || !isValidEmail(trimmed)) return;
    const existing = props.modelValue.map(e => e.toLowerCase());
    if (existing.includes(trimmed)) return;
    emit('update:modelValue', [...props.modelValue, trimmed]);
}
 
function addFromContact(contact: Contact) {
    if (!contact.email) return;
    addEmail(contact.email);
    closeAddModal();
}
 
function addManualEmail() {
    if (!manualEmail.value) return;
    addEmail(manualEmail.value);
    manualEmail.value = '';
    closeAddModal();
}
 
function isValidEmail(s: string): boolean {
    return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s?.trim() || '');
}
 
function isAlreadyAdded(email: string | null | undefined): boolean {
    if (!email) return false;
    return props.modelValue.map(e => e.toLowerCase()).includes(email.trim().toLowerCase());
}
 
function searchContacts() {
    if (searchTimeout) clearTimeout(searchTimeout);
    searchTimeout = setTimeout(async () => {
        if (!props.customerId || !contactSearchQuery.value.trim()) {
            filteredContacts.value = [];
            return;
        }
        try {
            const { data } = await axios.get('/api/contacts/search', {
                params: { customer_id: props.customerId, search: contactSearchQuery.value.trim() },
            });
            filteredContacts.value = data.contacts || [];
        } catch {
            filteredContacts.value = [];
        }
    }, 200);
}
 
function closeAddModal() {
    showAddModal.value = false;
    addMode.value = 'contact';
    contactSearchQuery.value = '';
    manualEmail.value = '';
    filteredContacts.value = [];
}
 
watch(showAddModal, (open) => {
    if (open) {
        addMode.value = 'contact';
        contactSearchQuery.value = '';
        manualEmail.value = '';
        filteredContacts.value = [];
    }
});
</script>