|
| 1 | +<template> |
| 2 | + <div class="w-full space-y-6 pb-8 relative"> |
| 3 | + <!-- Header --> |
| 4 | + <div class="flex items-center justify-between mb-4"> |
| 5 | + <span class="text-xl font-semibold text-gray-900">{{ $t('globals.terms.note', 2) }}</span> |
| 6 | + <Button |
| 7 | + variant="outline" |
| 8 | + size="sm" |
| 9 | + @click="startAddingNote" |
| 10 | + v-if="!isAddingNote && !isLoading && notes.length !== 0" |
| 11 | + class="transition-all hover:bg-primary/10 hover:border-primary/30" |
| 12 | + > |
| 13 | + <PlusIcon class="mr-2" size="18" /> |
| 14 | + {{ $t('globals.messages.new', { name: $t('globals.terms.note') }) }} |
| 15 | + </Button> |
| 16 | + </div> |
| 17 | + |
| 18 | + <div class="h-52" v-if="isLoading"> |
| 19 | + <Spinner /> |
| 20 | + </div> |
| 21 | + |
| 22 | + <!-- Note input --> |
| 23 | + <div v-if="isAddingNote"> |
| 24 | + <form @submit.prevent="addOrUpdateNote" @keydown.ctrl.enter="addOrUpdateNote"> |
| 25 | + <div class="space-y-4"> |
| 26 | + <div class="box p-2 h-52 min-h-52"> |
| 27 | + <Editor |
| 28 | + v-model:htmlContent="newNote" |
| 29 | + @update:htmlContent="(value) => (newNote = value)" |
| 30 | + :placeholder="t('editor.placeholder')" |
| 31 | + /> |
| 32 | + </div> |
| 33 | + <div class="flex justify-end space-x-3 pt-2"> |
| 34 | + <Button |
| 35 | + variant="outline" |
| 36 | + @click="cancelAddNote" |
| 37 | + class="transition-all hover:bg-gray-100" |
| 38 | + > |
| 39 | + Cancel |
| 40 | + </Button> |
| 41 | + <Button type="submit" :disabled="!newNote.trim()"> |
| 42 | + {{ |
| 43 | + editingNoteId |
| 44 | + ? $t('globals.buttons.update') + ' ' + $t('globals.terms.note').toLowerCase() |
| 45 | + : $t('globals.buttons.save') + ' ' + $t('globals.terms.note').toLowerCase() |
| 46 | + }} |
| 47 | + </Button> |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + </form> |
| 51 | + </div> |
| 52 | + |
| 53 | + <!-- Notes card list --> |
| 54 | + <div class="space-y-4"> |
| 55 | + <Card |
| 56 | + v-for="note in notes" |
| 57 | + :key="note.id" |
| 58 | + class="overflow-hidden border-gray-2 00 hover:border-gray-300 transition-all duration-200 box hover:shadow" |
| 59 | + > |
| 60 | + <!-- Header --> |
| 61 | + <CardHeader class="bg-gray-50/50"> |
| 62 | + <div class="flex items-center justify-between"> |
| 63 | + <div class="flex items-center space-x-3"> |
| 64 | + <Avatar class="border border-gray-200 shadow-sm"> |
| 65 | + <AvatarImage :src="note.avatar_url" /> |
| 66 | + <AvatarFallback> |
| 67 | + {{ getInitials(note.first_name, note.last_name) }} |
| 68 | + </AvatarFallback> |
| 69 | + </Avatar> |
| 70 | + <div> |
| 71 | + <p class="text-sm font-medium text-gray-900">{{ note.first_name }}</p> |
| 72 | + <p class="text-xs text-muted-foreground flex items-center"> |
| 73 | + <ClockIcon class="h-3 w-3 mr-1 inline-block opacity-70" /> |
| 74 | + {{ formatDate(note.created_at) }} |
| 75 | + </p> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + <DropdownMenu> |
| 79 | + <DropdownMenuTrigger asChild> |
| 80 | + <Button variant="ghost" size="icon" class="h-8 w-8 rounded-full"> |
| 81 | + <MoreVerticalIcon class="h-4 w-4" /> |
| 82 | + <span class="sr-only">Open menu</span> |
| 83 | + </Button> |
| 84 | + </DropdownMenuTrigger> |
| 85 | + <DropdownMenuContent align="end" class="w-[180px]"> |
| 86 | + <DropdownMenuItem @click="editNote(note)" class="cursor-pointer"> |
| 87 | + <PencilIcon class="mr-2" size="15" /> |
| 88 | + {{ $t('globals.buttons.edit', { name: $t('globals.terms.note').toLowerCase() }) }} |
| 89 | + </DropdownMenuItem> |
| 90 | + <DropdownMenuSeparator /> |
| 91 | + <DropdownMenuItem |
| 92 | + @click="deleteNote(note.id)" |
| 93 | + class="text-destructive cursor-pointer" |
| 94 | + > |
| 95 | + <TrashIcon class="mr-2" size="15" /> |
| 96 | + {{ |
| 97 | + $t('globals.buttons.delete', { name: $t('globals.terms.note').toLowerCase() }) |
| 98 | + }} |
| 99 | + </DropdownMenuItem> |
| 100 | + </DropdownMenuContent> |
| 101 | + </DropdownMenu> |
| 102 | + </div> |
| 103 | + </CardHeader> |
| 104 | + |
| 105 | + <!-- Note content --> |
| 106 | + <CardContent class="pt-4 pb-5 text-gray-700"> |
| 107 | + <p class="whitespace-pre-wrap text-sm leading-relaxed" v-dompurify-html="note.note"></p> |
| 108 | + </CardContent> |
| 109 | + </Card> |
| 110 | + </div> |
| 111 | + |
| 112 | + <!-- No notes message --> |
| 113 | + <div |
| 114 | + v-if="notes.length === 0 && !isAddingNote && !isLoading" |
| 115 | + class="box border-dashed p-10 text-center bg-gray-50/50 mt-6" |
| 116 | + > |
| 117 | + <div class="flex flex-col items-center"> |
| 118 | + <div class="rounded-full bg-gray-100 p-4 mb-2"> |
| 119 | + <MessageSquareIcon class="text-gray-400" size="25" /> |
| 120 | + </div> |
| 121 | + <h3 class="mt-2 text-base font-medium text-gray-900">{{ $t('contact.noNotes') }}</h3> |
| 122 | + <p class="mt-1 text-sm text-muted-foreground max-w-sm mx-auto"> |
| 123 | + {{ $t('contact.notes.help') }} |
| 124 | + </p> |
| 125 | + <Button variant="outline" class="mt-3 border-gray-300" @click="startAddingNote"> |
| 126 | + <PlusIcon class="mr-2" size="15" /> |
| 127 | + {{ $t('globals.messages.add', { name: $t('globals.terms.note').toLowerCase() }) }} |
| 128 | + </Button> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | +</template> |
| 133 | + |
| 134 | +<script setup> |
| 135 | +import { ref, onMounted } from 'vue' |
| 136 | +import { format } from 'date-fns' |
| 137 | +import { Button } from '@/components/ui/button' |
| 138 | +import { Card, CardHeader, CardContent } from '@/components/ui/card' |
| 139 | +import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar' |
| 140 | +import { Spinner } from '@/components/ui/spinner' |
| 141 | +import { |
| 142 | + DropdownMenu, |
| 143 | + DropdownMenuTrigger, |
| 144 | + DropdownMenuContent, |
| 145 | + DropdownMenuItem, |
| 146 | + DropdownMenuSeparator |
| 147 | +} from '@/components/ui/dropdown-menu' |
| 148 | +import { |
| 149 | + PlusIcon, |
| 150 | + MoreVerticalIcon, |
| 151 | + PencilIcon, |
| 152 | + TrashIcon, |
| 153 | + ClockIcon, |
| 154 | + MessageSquareIcon |
| 155 | +} from 'lucide-vue-next' |
| 156 | +import Editor from '@/features/conversation/ConversationTextEditor.vue' |
| 157 | +import { useI18n } from 'vue-i18n' |
| 158 | +import { useEmitter } from '@/composables/useEmitter' |
| 159 | +import { EMITTER_EVENTS } from '@/constants/emitterEvents.js' |
| 160 | +import { handleHTTPError } from '@/utils/http' |
| 161 | +import { getInitials } from '@/utils/strings' |
| 162 | +import api from '@/api' |
| 163 | +
|
| 164 | +const props = defineProps({ contactId: Number }) |
| 165 | +const { t } = useI18n() |
| 166 | +const emitter = useEmitter() |
| 167 | +
|
| 168 | +const notes = ref([]) |
| 169 | +const isAddingNote = ref(false) |
| 170 | +const newNote = ref('') |
| 171 | +const editingNoteId = ref(null) |
| 172 | +const isLoading = ref(false) |
| 173 | +
|
| 174 | +const fetchNotes = async () => { |
| 175 | + try { |
| 176 | + isLoading.value = true |
| 177 | + const { data } = await api.getContactNotes(props.contactId) |
| 178 | + notes.value = data.data |
| 179 | + } catch (error) { |
| 180 | + emitter.emit(EMITTER_EVENTS.SHOW_TOAST, { |
| 181 | + variant: 'destructive', |
| 182 | + description: handleHTTPError(error).message |
| 183 | + }) |
| 184 | + } finally { |
| 185 | + isLoading.value = false |
| 186 | + } |
| 187 | +} |
| 188 | +
|
| 189 | +onMounted(fetchNotes) |
| 190 | +
|
| 191 | +const formatDate = (date) => format(new Date(date), 'PPP p') |
| 192 | +
|
| 193 | +const startAddingNote = () => { |
| 194 | + isAddingNote.value = true |
| 195 | +} |
| 196 | +
|
| 197 | +const cancelAddNote = () => { |
| 198 | + isAddingNote.value = false |
| 199 | + newNote.value = '' |
| 200 | + editingNoteId.value = null |
| 201 | +} |
| 202 | +
|
| 203 | +const addOrUpdateNote = async () => { |
| 204 | + try { |
| 205 | + if (editingNoteId.value) { |
| 206 | + await api.updateContactNote(props.contactId, editingNoteId.value, { note: newNote.value }) |
| 207 | + } else { |
| 208 | + await api.createContactNote(props.contactId, { note: newNote.value }) |
| 209 | + } |
| 210 | + await fetchNotes() |
| 211 | + cancelAddNote() |
| 212 | + } catch (error) { |
| 213 | + emitter.emit(EMITTER_EVENTS.SHOW_TOAST, { |
| 214 | + variant: 'destructive', |
| 215 | + description: handleHTTPError(error).message |
| 216 | + }) |
| 217 | + } |
| 218 | +} |
| 219 | +
|
| 220 | +const editNote = (note) => { |
| 221 | + editingNoteId.value = note.id |
| 222 | + newNote.value = note.note |
| 223 | + isAddingNote.value = true |
| 224 | +} |
| 225 | +
|
| 226 | +const deleteNote = async (noteId) => { |
| 227 | + await api.deleteContactNote(props.contactId, noteId) |
| 228 | + await fetchNotes() |
| 229 | +} |
| 230 | +</script> |
0 commit comments