()
| 25 | } |
| 26 | |
| 27 | refresh() { |
| 28 | const { target, slug, host, endpoint, partials } = this.options; |
| 29 | |
| 30 | fetch(endpoint, { |
| 31 | credentials: 'include', |
| 32 | headers: { |
| 33 | 'Content-Type': 'application/json' |
| 34 | } |
| 35 | }) |
| 36 | .then(r => r.json()) |
| 37 | .then(data => { |
| 38 | data.comments_tpl = comments_tpl; |
| 39 | data.partials = partials; |
| 40 | $(target).innerHTML = schnack_tpl(data); |
| 41 | // console.log('data', data); |
| 42 | |
| 43 | const above = $(`${target} div.schnack-above`); |
| 44 | const form = $(`${target} div.schnack-form`); |
| 45 | const textarea = $(`${target} textarea.schnack-body`); |
| 46 | const preview = $(`${target} .schnack-form blockquote.schnack-body`); |
| 47 | |
| 48 | const draft = window.localStorage.getItem(`schnack-draft-${slug}`); |
| 49 | if (draft && textarea) textarea.value = draft; |
| 50 | |
| 51 | const postBtn = $(target + ' .schnack-button'); |
| 52 | const previewBtn = $(target + ' .schnack-preview'); |
| 53 | const writeBtn = $(target + ' .schnack-write'); |
| 54 | const cancelReplyBtn = $(target + ' .schnack-cancel-reply'); |
| 55 | const replyBtns = $$(target + ' .schnack-reply'); |
| 56 | |
| 57 | if (postBtn) { |
| 58 | postBtn.addEventListener('click', d => { |
| 59 | const body = textarea.value; |
| 60 | fetch(endpoint, { |
| 61 | credentials: 'include', |
| 62 | method: 'POST', |
| 63 | headers: { |
| 64 | 'Content-Type': 'application/json' |
| 65 | }, |
| 66 | body: JSON.stringify({ |
| 67 | comment: body, |
| 68 | replyTo: form.dataset.reply |
| 69 | }) |
| 70 | }) |
| 71 | .then(r => r.json()) |
| 72 | .then(res => { |
| 73 | textarea.value = ''; |
| 74 | window.localStorage.setItem( |
| 75 | `schnack-draft-${slug}`, |
| 76 | textarea.value |
| 77 | ); |
| 78 | if (res.id) { |
| 79 | this.firstLoad = true; |
| 80 | window.location.hash = '#comment-' + res.id; |
| 81 | } |
| 82 | this.refresh(); |
| 83 | }); |
| 84 | }); |
no test coverage detected