-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpm copy.js
141 lines (125 loc) · 4.2 KB
/
pm copy.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
133
134
135
136
137
138
139
140
141
$(document).ready(function() {
// Load conversations
loadConversations();
// Load message thread for a conversation when clicked
$('#conversation-list').on('click', '.conversation', function() {
var conversationId = $(this).data('conversation-id');
loadMessageThread(conversationId);
});
// Submit the compose form to send a message
$('#compose-form').submit(function(e) {
e.preventDefault();
var recipient = $('#recipient').val();
var message = $('#message-content').val();
sendMessage(recipient, message);
});
});
// Function to load conversations
function loadConversations() {
$.ajax({
url: 'getconversations.php',
type: 'GET',
dataType: 'json',
success: function(response) {
if (response.success) {
var conversations = response.conversations;
displayConversations(conversations);
} else {
displayNoConversationsMessage();
}
},
error: function(xhr, status, error) {
console.error(error);
displayErrorMessage();
}
});
}
// Function to load message thread for a conversation
function loadMessageThread(conversationId) {
$.ajax({
url: 'getmessages.php',
type: 'GET',
dataType: 'json',
data: { conversationId: conversationId },
success: function(response) {
if (response.success) {
var messages = response.messages;
displayMessageThread(messages);
} else {
displayNoMessagesMessage();
}
},
error: function(xhr, status, error) {
console.error(error);
displayErrorMessage();
}
});
}
// Function to send a message
function sendMessage(conversationId, recipientId, messageContent) {
$.ajax({
url: 'sendmessage.php',
type: 'POST',
dataType: 'json',
data: { conversationId: conversationId, recipientId: recipientId, messageContent: messageContent },
success: function(response) {
if (response.success) {
displaySuccessMessage();
} else {
displayErrorMessage();
}
},
error: function(xhr, status, error) {
console.error(error);
displayErrorMessage();
}
});
}
// Example function to display conversations
function displayConversations(conversations) {
// Clear the conversation list
$('#conversation-list').empty();
// Loop through conversations and display each one
conversations.forEach(function(conversation) {
var conversationId = conversation.conversation_id;
var recipientUsername = conversation.recipient_username;
// Create and append a conversation item to the list
var listItem = $('<li>')
.text(recipientUsername)
.data('conversation-id', conversationId)
.addClass('conversation');
$('#conversation-list').append(listItem);
});
}
// Example function to display message thread
function displayMessageThread(messages) {
// Clear the message thread
$('#message-thread').empty();
// Loop through messages and display each one
messages.forEach(function(message) {
var sender = message.sender_username; // Fix: Use 'sender_username' instead of 'sender'
var content = message.content;
// Create and append a message item to the thread
var messageItem = $('<div>')
.addClass('message')
.append($('<span>').text(sender + ': '))
.append($('<span>').text(content));
$('#message-thread').append(messageItem);
});
}
// Example function to display success message
function displaySuccessMessage() {
alert('Message sent successfully.');
}
// Example function to display error message
function displayErrorMessage() {
alert('An error occurred. Please try again.');
}
// Example function to handle the case when no conversations are available
function displayNoConversationsMessage() {
$('#conversation-list').html('<li>No conversations found.</li>');
}
// Example function to handle the case when no messages are available
function displayNoMessagesMessage() {
$('#message-thread').html('<div>No messages found.</div>');
}