-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddNote.js
132 lines (119 loc) · 5.82 KB
/
addNote.js
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
export default function addNote(projectId, customerId, myName, myAvatar, myID) {
//region handle add note form
const addNoteForm = document.querySelector('#add-note-form');
const notesContainer = document.querySelector('#notes-container');
const noteStatus = document.querySelector('#note-status');
addNoteForm.addEventListener('submit', async function (e) {
e.preventDefault();
const noteText = document.querySelector('#note-text').value.trim();
if (!noteText) return;
try {
const response = await fetch('api/notes/note.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
action: 'add',
object_id: projectId,
object_type: 'project',
note: noteText,
customer_id: customerId
})
});
// Log the raw response text
const responseText = await response.text();
console.log(responseText);
// Parse the response text as JSON
const data = JSON.parse(responseText);
if (data.success) {
// Create new note element
const noteElement = document.createElement('div');
noteElement.className = 'note border rounded mb-2';
noteElement.id = `note-${data.note_id}`;
// Get current user data
const userName = myName;
const userAvatar = myAvatar;
const userId = myID;
let noteTextFormatted = noteText.replace(/\n/g, '<br>');
noteElement.innerHTML = `
<div class="card-body" dir="auto">
<p class="mt-3">${noteTextFormatted}</p>
</div>
<div class="card-footer">
<div class="d-flex align-items-start">
<a href="user?id=${userId}">
<img class="me-3 avatar-sm rounded-circle" src="${userAvatar}" alt="${userName}">
</a>
<div class="w-100 overflow-hidden">
<h4 class="my-0">
<a href="user?id=${userId}">${userName}</a>
</h4>
<span class="small">
<span datetime="true">Just now</span>
<a href="#" data-bs-toggle="modal" data-bs-target="#editNote${data.note_id}"><i class="fa-solid fa-pencil"></i></a>
</span>
</div>
</div>
</div>
<form method="post">
<input type="hidden" name="note_id" value="${data.note_id}">
<div id="editNote${data.note_id}" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="editNote${data.note_id}" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Edit Note</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-hidden="true"></button>
</div>
<div class="modal-body">
<textarea name="editnote" dir="auto" class="form-control" rows="10">${noteText}</textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
<button type="button" data-bs-dismiss="modal" data-bs-toggle="modal" data-bs-target="#delete-note-confirm-${data.note_id}" class="btn btn-danger">Delete</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<div class="modal fade" tabindex="-1" role="dialog" id="delete-note-confirm-${data.note_id}" aria-labelledby="delete-note-confirm-${data.note_id}" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header bg-danger text-white">
<h4 class="modal-title">Delete this note?</h4>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-hidden="true"></button>
</div>
<div class="modal-body">
${noteText}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
<a href="project?id=${projectId}&delete_note=${data.note_id}" class="btn btn-danger">Delete</a>
</div>
</div>
</div>
</div>
</form>
`;
// Insert the new note at the top of the notes container
notesContainer.insertBefore(noteElement, notesContainer.firstChild);
// Clear form and show success message
addNoteForm.reset();
noteStatus.textContent = 'Note added successfully';
noteStatus.className = 'text-success ms-2';
// Clear success message after 3 seconds
setTimeout(() => {
noteStatus.textContent = '';
}, 3000);
} else {
throw new Error(data.error || 'Failed to add note');
}
} catch (error) {
console.error('Error:', error);
console.error('Full error details:', error.stack);
noteStatus.textContent = error.message;
noteStatus.className = 'text-danger ms-2';
}
});
//endregion handle add note form
}