-
-
- message +
- {{ m.message }}
diff --git a/conversations/index.php b/conversations/index.php index d21c962..f24c815 100644 --- a/conversations/index.php +++ b/conversations/index.php @@ -22,3 +22,45 @@ } $directoryURI = $_SERVER['REQUEST_URI']; + +switch ($action) { + case 'get': + $threadId = $input['threadId']; + if (empty($threadId)) { + $errorStatus->response(400, "threadId field is required"); + } + + $comments = getConversation($threadId); + // recursively updated comments w/ child comments + + echo json_encode($comments); + break; + case 'submit': + $threadId = $input['threadId']; + $message = $input['message']; + if (empty($threadId) || empty($message)) { + $errorStatus->response(400, "threadId, message field(s) are required"); + } + $regOutcome = sendMessage($threadId, $message); + if ($regOutcome['rowsChanged'] === 1) { + $result = getMessage($regOutcome['lastId']); + + $statuscode = 201; + + header("HTTP/1.1 " . $statuscode); + + $response = array('Status' => 'success'); + + echo json_encode($result); + } else { + $errorStatus->response(500, "Error saving message"); + } + + break; + default; + // header('location: /accounts/'); + $errorStatus->response(404, "Method not valid"); + // include '../view/admin.php'; + break; + // include '../view/404.php'; +} diff --git a/examples/js/vue.js b/examples/js/vue.js index 9e7039d..0425115 100644 --- a/examples/js/vue.js +++ b/examples/js/vue.js @@ -26,10 +26,12 @@ var app = new Vue({ example: "comments", status: null, form: {}, + conversations: [], loading: { general: false, comments: false, - form: false + form: false, + chat: false }, userForm: {}, modal: { @@ -81,6 +83,38 @@ var app = new Vue({ }); + }, + sendMessage() { + if (this.loading.chat) + return; + // retrieve from cookie + let threadId = "effba2487ece11eb8e3a0242ac110002" + let config = {} + let request = { + threadId: threadId, + message: this.chat.message + } + this.loading.chat = true; + + this.$http.post("/conversations/?action=submit", request, config).then((response) => { + this.loading.chat = false; + console.log(response) + // this.message = response.data.message; + if (response.status == 201) { + this.chat.message = ""; + console.log(response.data); + this.conversations.push(response.data); + // this.$set() + // this.comments = response.data; + } else { + this.errors = "Failed to send message" + } + }).catch((error) => { + this.errors = error.data + console.log(error) + this.loading.chat = null; + + }); }, addComment() { // don't run if not logged in or loading @@ -156,6 +190,29 @@ var app = new Vue({ } this.modal.signIn = false; }, + getConversation() { + // check from cookie for conversation id + let threadId = "effba2487ece11eb8e3a0242ac110002" + this.loading.chat = true; + this.errors = null; + this.$http.post("/conversations/?action=get", { "threadId": threadId }).then((response) => { + this.loading.chat = false; + console.log(response) + // this.message = response.data.message; + if (response.status == 200) { + console.log(response.data) + this.conversations = response.data; + } else { + this.errors = "Failed to load chat" + } + }).catch((error) => { + this.errors = "Failed to get chat" + console.log(error) + this.loading.chat = null; + + }); + + }, getComments() { this.loading.comments = true; this.errors = null; @@ -179,5 +236,6 @@ var app = new Vue({ }, mounted() { this.getComments(); + this.getConversation(); } }) \ No newline at end of file diff --git a/examples/vue.html b/examples/vue.html index d68ccc1..60f56e3 100644 --- a/examples/vue.html +++ b/examples/vue.html @@ -192,10 +192,10 @@