All files / js/components NotesPanel.vue

49.36% Statements 78/158
40.33% Branches 48/119
30% Functions 9/30
51.36% Lines 75/146

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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405                                                                                              2x               2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x   2x               2x 1x     2x   2x                   2x 2x               2x 2x 2x 2x           2x     2x       2x       2x 2x 2x 2x 2x         2x       2x 1x 1x 1x                                                                                 2x         2x 2x               2x                 2x                                             2x 2x         8x 1x 1x 1x 1x 1x       1x               1x   1x                       1x 1x 1x 1x     1x                                               1x                           1x 1x         1x 1x                                                             1x                 1x                 1x                     1x                                            
<script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue';
import { useDateTimeFormat } from '@/composables/useDateTimeFormat';
import { getSafeExternalUrl } from '@/composables/useSafeExternalUrl';
import { getCsrfToken } from '@/lib/csrf';
 
interface NoteUser {
    id: number;
    name: string;
}
 
interface NoteItem {
    id: number;
    subject: string;
    description?: string | null;
    created_at: string;
    user?: NoteUser | null;
    attachment_original_name?: string | null;
    attachment_size?: number | null;
    attachment_url?: string | null;
}
 
interface RecordOption {
    id: number;
    label: string;
}
 
interface ModuleOption {
    value: string;
    label: string;
}
 
interface NotesResponse {
    data: NoteItem[];
    current_page: number;
    last_page: number;
    total: number;
}
 
interface Props {
    module?: string | null;
    recordId?: number | null;
    title?: string;
    showModuleSelector?: boolean;
    modules?: ModuleOption[];
}
 
const props = withDefaults(defineProps<Props>(), {
    module: null,
    recordId: null,
    title: 'Notes',
    showModuleSelector: false,
    modules: () => [],
});
 
const notes = ref<NoteItem[]>([]);
const loading = ref(false);
const saving = ref(false);
const showCreateModal = ref(false);
const search = ref('');
const page = ref(1);
const lastPage = ref(1);
const total = ref(0);
const errorMessage = ref('');
 
const formModule = ref<string>(props.module || '');
const formRecordId = ref<number | null>(props.recordId || null);
const formSubject = ref('');
const formDescription = ref('');
const formAttachment = ref<File | null>(null);
const recordSearch = ref('');
const recordResults = ref<RecordOption[]>([]);
const searchingRecords = ref(false);
 
const canLoadNotes = computed(() => !!formModule.value && !!formRecordId.value);
const isContextLocked = computed(() => !props.showModuleSelector && !!props.module && !!props.recordId);
const { formatDateTime } = useDateTimeFormat();
 
const fileSize = (size?: number | null) => {
    const value = Number(size || 0);
    if (!value) return '';
    if (value >= 1024 * 1024) return `${(value / (1024 * 1024)).toFixed(2)} MB`;
    if (value >= 1024) return `${(value / 1024).toFixed(1)} KB`;
    return `${value} B`;
};
 
const formatDate = (value: string) => {
    return formatDateTime(value);
};
 
const getSafeAttachmentUrl = (url?: string | null) => getSafeExternalUrl(url);
 
const resetForm = () => {
    formSubject.value = '';
    formDescription.value = '';
    formAttachment.value = null;
    if (!isContextLocked.value) {
        formRecordId.value = null;
        recordSearch.value = '';
    }
};
 
const fetchNotes = async (targetPage = 1) => {
    Iif (!canLoadNotes.value) {
        notes.value = [];
        total.value = 0;
        page.value = 1;
        lastPage.value = 1;
        return;
    }
 
    loading.value = true;
    errorMessage.value = '';
    try {
        const params = new URLSearchParams({
            module: formModule.value,
            record_id: String(formRecordId.value),
            page: String(targetPage),
            per_page: '10',
        });
        Iif (search.value.trim()) {
            params.set('search', search.value.trim());
        }
        const response = await fetch(`/notes/record?${params.toString()}`, {
            headers: { Accept: 'application/json' },
        });
 
        Iif (!response.ok) {
            throw new Error('Failed to load notes');
        }
 
        const payload: NotesResponse = await response.json();
        notes.value = payload.data || [];
        page.value = payload.current_page || 1;
        lastPage.value = payload.last_page || 1;
        total.value = payload.total || 0;
    } catch (error) {
        errorMessage.value = 'Unable to load notes right now.';
        console.error(error);
    } finally {
        loading.value = false;
    }
};
 
const submitNote = async () => {
    Eif (!formModule.value || !formRecordId.value || !formSubject.value.trim()) {
        errorMessage.value = 'Subject and related record are required.';
        return;
    }
 
    saving.value = true;
    errorMessage.value = '';
 
    try {
        const payload = new FormData();
        payload.append('module', formModule.value);
        payload.append('record_id', String(formRecordId.value));
        payload.append('subject', formSubject.value.trim());
        payload.append('description', formDescription.value.trim());
        if (formAttachment.value) {
            payload.append('attachment', formAttachment.value);
        }
 
        const response = await fetch('/notes', {
            method: 'POST',
            headers: {
                'X-CSRF-TOKEN': getCsrfToken(),
                Accept: 'application/json',
            },
            body: payload,
        });
 
        if (!response.ok) {
            throw new Error('Unable to create note');
        }
 
        resetForm();
        showCreateModal.value = false;
        await fetchNotes(1);
    } catch (error) {
        errorMessage.value = 'Unable to save note.';
        console.error(error);
    } finally {
        saving.value = false;
    }
};
 
let searchDebounce: number | undefined;
watch(search, () => {
    window.clearTimeout(searchDebounce);
    searchDebounce = window.setTimeout(() => fetchNotes(1), 250);
});
 
watch(
    () => [props.module, props.recordId],
    () => {
        if (props.module) formModule.value = props.module;
        if (props.recordId) formRecordId.value = props.recordId;
        fetchNotes(1);
    }
);
 
watch(formModule, () => {
    if (!isContextLocked.value) {
        formRecordId.value = null;
        recordSearch.value = '';
        recordResults.value = [];
    }
});
 
let recordDebounce: number | undefined;
watch(recordSearch, () => {
    if (isContextLocked.value || !formModule.value || !recordSearch.value.trim()) {
        recordResults.value = [];
        return;
    }
    window.clearTimeout(recordDebounce);
    recordDebounce = window.setTimeout(async () => {
        searchingRecords.value = true;
        try {
            const params = new URLSearchParams({
                module: formModule.value,
                q: recordSearch.value.trim(),
            });
            const response = await fetch(`/notes/related-records?${params.toString()}`, {
                headers: { Accept: 'application/json' },
            });
            recordResults.value = response.ok ? await response.json() : [];
        } finally {
            searchingRecords.value = false;
        }
    }, 250);
});
 
onMounted(() => {
    fetchNotes(1);
});
</script>
 
<template>
    <div class="rounded-lg border border-gray-200 bg-white shadow-sm">
        <div class="border-b border-gray-200 bg-gray-50 px-4 py-3">
            <div class="flex items-center justify-between gap-3">
                <h3 class="text-base font-semibold text-gray-900">{{ title }}</h3>
                <div class="flex items-center gap-2">
                    <span class="text-xs text-gray-500">{{ total }} total</span>
                    <button
                        type="button"
                        class="rounded bg-blue-600 px-2.5 py-1.5 text-xs font-medium text-white hover:bg-blue-700"
                        @click="showCreateModal = true"
                    >
                        Create Note
                    </button>
                </div>
            </div>
        </div>
 
        <div class="space-y-4 p-4">
            <div>
                <div class="mb-3 flex items-center gap-2">
                    <input
                        v-model="search"
                        type="text"
                        class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm shadow-sm focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500"
                        placeholder="Search notes..."
                    />
                </div>
 
                <div v-if="loading" class="py-4 text-sm text-gray-500">Loading notes...</div>
                <div v-else-if="notes.length === 0" class="py-4 text-sm text-gray-500">No notes yet.</div>
                <div v-else class="space-y-3">
                    <div v-for="note in notes" :key="note.id" class="rounded-lg border border-gray-200 bg-white p-3">
                        <div class="flex items-start justify-between gap-2">
                            <h4 class="text-sm font-semibold text-gray-900">{{ note.subject }}</h4>
                            <span class="text-xs text-gray-500">{{ formatDate(note.created_at) }}</span>
                        </div>
                        <p v-if="note.description" class="mt-1 whitespace-pre-wrap text-sm text-gray-700">{{ note.description }}</p>
                        <div class="mt-2 flex items-center justify-between gap-2 text-xs text-gray-500">
                            <span>By {{ note.user?.name || 'System' }}</span>
                            <a
                                v-if="getSafeAttachmentUrl(note.attachment_url)"
                                :href="getSafeAttachmentUrl(note.attachment_url) || '#'"
                                rel="noopener noreferrer"
                                class="text-blue-600 hover:text-blue-800 hover:underline"
                            >
                                {{ note.attachment_original_name || 'Download attachment' }}
                                <span v-if="note.attachment_size">({{ fileSize(note.attachment_size) }})</span>
                            </a>
                        </div>
                    </div>
                </div>
 
                <div v-if="lastPage > 1" class="mt-4 flex items-center justify-end gap-2">
                    <button
                        type="button"
                        class="rounded-md border border-gray-300 bg-white px-2.5 py-1.5 text-xs font-medium text-gray-700 hover:bg-gray-50 disabled:opacity-50"
                        :disabled="page <= 1"
                        @click="fetchNotes(page - 1)"
                    >
                        Previous
                    </button>
                    <span class="text-xs text-gray-600">Page {{ page }} of {{ lastPage }}</span>
                    <button
                        type="button"
                        class="rounded-md border border-gray-300 bg-white px-2.5 py-1.5 text-xs font-medium text-gray-700 hover:bg-gray-50 disabled:opacity-50"
                        :disabled="page >= lastPage"
                        @click="fetchNotes(page + 1)"
                    >
                        Next
                    </button>
                </div>
            </div>
        </div>
 
        <div v-if="showCreateModal" class="fixed inset-0 z-50 overflow-y-auto bg-gray-600 bg-opacity-50">
            <div class="flex min-h-full items-center justify-center p-4">
                <div class="w-full max-w-2xl rounded-lg border border-gray-200 bg-white shadow-lg">
                    <div class="border-b border-gray-200 bg-gray-50 px-6 py-4">
                        <h4 class="text-lg font-semibold text-gray-900">Create Note</h4>
                        <p class="mt-1 text-sm text-gray-600">Add a note linked to this record</p>
                    </div>
                    <div class="space-y-4 px-6 py-5">
                        <div class="grid gap-3 md:grid-cols-2">
                            <div v-if="showModuleSelector">
                                <label class="mb-1 block text-sm font-medium text-gray-700">Related Module</label>
                                <select v-model="formModule" class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm shadow-sm focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500">
                                    <option value="">Select module</option>
                                    <option v-for="m in modules" :key="m.value" :value="m.value">
                                        {{ m.label }}
                                    </option>
                                </select>
                            </div>
                            <div v-if="showModuleSelector">
                                <label class="mb-1 block text-sm font-medium text-gray-700">Related Record</label>
                                <input
                                    v-model="recordSearch"
                                    type="text"
                                    class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm shadow-sm focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500"
                                    placeholder="Search record"
                                />
                                <div v-if="searchingRecords" class="mt-1 text-xs text-gray-500">Searching...</div>
                                <div v-if="recordResults.length > 0" class="mt-1 max-h-36 overflow-auto rounded-md border border-gray-200 bg-white shadow-sm">
                                    <button
                                        v-for="record in recordResults"
                                        :key="record.id"
                                        type="button"
                                        class="block w-full border-b border-gray-100 px-3 py-2 text-left text-sm text-gray-700 hover:bg-gray-50"
                                        @click="formRecordId = record.id; recordSearch = record.label; recordResults = []"
                                    >
                                        {{ record.label }}
                                    </button>
                                </div>
                            </div>
                            <div class="md:col-span-2">
                                <label class="mb-1 block text-sm font-medium text-gray-700">Subject</label>
                                <input
                                    v-model="formSubject"
                                    type="text"
                                    class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm shadow-sm focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500"
                                    placeholder="Subject"
                                />
                            </div>
                            <div class="md:col-span-2">
                                <label class="mb-1 block text-sm font-medium text-gray-700">Description</label>
                                <textarea
                                    v-model="formDescription"
                                    rows="4"
                                    class="w-full rounded-md border border-gray-300 px-3 py-2 text-sm shadow-sm focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500"
                                    placeholder="Description"
                                />
                            </div>
                            <div class="md:col-span-2">
                                <label class="mb-1 block text-sm font-medium text-gray-700">Attachment</label>
                                <input
                                    type="file"
                                    class="w-full rounded-md border border-gray-300 bg-white px-3 py-2 text-sm shadow-sm file:mr-3 file:rounded file:border-0 file:bg-gray-100 file:px-3 file:py-1.5 file:text-sm file:font-medium file:text-gray-700 hover:file:bg-gray-200"
                                    @change="formAttachment = ($event.target as HTMLInputElement)?.files?.[0] || null"
                                />
                            </div>
                        </div>
                        <div v-if="errorMessage" class="text-sm text-red-600">{{ errorMessage }}</div>
                    </div>
                    <div class="flex items-center justify-end gap-3 border-t border-gray-200 px-6 py-4">
                        <button
                            type="button"
                            class="rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50"
                            @click="showCreateModal = false"
                        >
                            Cancel
                        </button>
                        <button
                            type="button"
                            class="rounded-md bg-blue-600 px-4 py-2 text-sm font-medium text-white hover:bg-blue-700 disabled:opacity-50"
                            :disabled="saving"
                            @click="submitNote"
                        >
                            {{ saving ? 'Saving...' : 'Create Note' }}
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>