-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.js
More file actions
94 lines (69 loc) · 2.75 KB
/
Card.js
File metadata and controls
94 lines (69 loc) · 2.75 KB
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
'use strict';
import { getRelativeTime } from "./utils.js";
import { Tooltip } from "./Tolltip.js";
import { DeleteConfirmModal, NoteModal } from "./Modal.js";
import { client } from "./client.js";
import { db } from "./db.js";
/**
* Creates and HTML card element representing a note based on provided note data.
*
* @param {Object} noteData - Data representing the note to be displayed in the card
* @returns {HTMLElement} - The generated card element.
*/
export const Card = function (noteData) {
const { id, title, text, postedOn, notebookId } = noteData;
const $card = document.createElement('div');
$card.classList.add('card');
$card.setAttribute('data-note', id);
$card.innerHTML = `
<h3 class="card-title text-title-medium">${title}</h3>
<p class="card-text-text-body-large">${text}</p>
<div class="wrapper">
<div class="card-time text-label-large">${getRelativeTime(postedOn)}</div>
<button class="icon-btn large" aria-label="Delete note" data-tooltip="Delete note" data-delete-btn>
<ion-icon class="material-symbols-rounded" name="trash"></ion-icon>
<div class="state-layer"></div>
</button>
</div>
<div class="state-layer"></div>
`;
Tooltip($card.querySelector('[data-tooltip]'));
/**
* Note detail and edit functionality
*
* Attaches a click event listener to card element.
* When the card is clicked. it opens a modal with the note's details and allows for updating the note
*/
$card.addEventListener('click', function () {
const modal = NoteModal(title, text, getRelativeTime(postedOn));
modal.open();
// Attach a callback to handle the submission of changes
modal.onSubmit(noteData => {
const updatedData = db.update.note(id, noteData);
modal.close();
// Update the note in the client UI
client.note.update(id, updatedData);
});
});
/**
* Note delete functionality
*
* Attaches a click event listener to delete button element within card
* When the delete button is clicked, it opens a confirmation modal for deleting the associated note.
* If the deletion is confirmed, it update the UI and database to remove the note.
*/
const $deleteBtn = $card.querySelector('[data-delete-btn]')
$deleteBtn.addEventListener('click', function (event) {
event.stopImmediatePropagation();
const modal = DeleteConfirmModal(title);
modal.open();
modal.onSubmit(function (isConfirm) {
if (isConfirm) {
const existedNotes = db.delete.note(notebookId, id);
client.note.delete(id, existedNotes.length);
}
modal.close();
})
})
return $card;
}