diff --git a/src/component/comments/src/gitment.js b/src/component/comments/src/gitment.js index 62ce272..6bce9f1 100644 --- a/src/component/comments/src/gitment.js +++ b/src/component/comments/src/gitment.js @@ -1,10 +1,6 @@ import { autorun, observable } from 'mobx' -import { - LS_ACCESS_TOKEN_KEY, - LS_USER_KEY, - NOT_INITIALIZED_ERROR, -} from './constants' +import { LS_ACCESS_TOKEN_KEY, LS_USER_KEY } from './constants' import { getTargetContainer, http, Query } from './utils' import defaultTheme from './theme/default' @@ -139,15 +135,6 @@ export default class Gitment { } } - init() { - return this.createIssue() - .then(() => this.loadComments()) - .then(comments => { - this.state.error = null - return comments - }) - } - useTheme(theme = {}) { this.theme = theme @@ -173,20 +160,6 @@ export default class Gitment { }) } - createIssue() { - const { id, owner, repo, title, link, desc, labels } = this - - return http - .post(`/repos/${owner}/${repo}/issues`, { - title, - body: `${link}\n\n${desc}`, - }) - .then(meta => { - this.state.meta = meta - return meta - }) - } - getIssue() { if (this.state.meta.id) return Promise.resolve(this.state.meta) @@ -207,16 +180,26 @@ export default class Gitment { } loadMeta() { - const { id, owner, repo } = this - return http - .get(`/repos/${owner}/${repo}/issues`, { - creator: owner, - }) - .then(issues => { - if (!issues.length) return Promise.reject(NOT_INITIALIZED_ERROR) - this.state.meta = issues[0] - return issues[0] - }) + const { id, owner, repo, gistId, type = 'issues' } = this + if (type === 'gist') { + return http + .get(`/repos/${owner}/${repo}/${type}/${gistId}`, { + creator: owner, + }) + .then(issue => { + this.state.meta = issue + return issue + }) + } else { + return http + .get(`/repos/${owner}/${repo}/${type}/${id}`, { + creator: owner, + }) + .then(issue => { + this.state.meta = issue + return issue + }) + } } loadComments(page = this.state.currentPage) { diff --git a/src/component/comments/src/theme/default.js b/src/component/comments/src/theme/default.js index b4921db..c8c3d27 100644 --- a/src/component/comments/src/theme/default.js +++ b/src/component/comments/src/theme/default.js @@ -1,25 +1,28 @@ -import { github as githubIcon, heart as heartIcon, spinner as spinnerIcon } from '../icons' +import { + github as githubIcon, + heart as heartIcon, + spinner as spinnerIcon, +} from '../icons' import { NOT_INITIALIZED_ERROR } from '../constants' function renderHeader({ meta, user, reactions }, instance) { const container = document.createElement('div') - container.lang = "en-US" + container.lang = 'en-US' container.className = 'gitment-container gitment-header-container' const likeButton = document.createElement('span') - const likedReaction = reactions.find(reaction => ( - reaction.content === 'heart' && reaction.user.login === user.login - )) + const likedReaction = reactions.find( + reaction => + reaction.content === 'heart' && reaction.user.login === user.login + ) likeButton.className = 'gitment-header-like-btn' likeButton.innerHTML = ` ${heartIcon} - ${ likedReaction - ? 'Unlike' - : 'Like' - } - ${ meta.reactions && meta.reactions.heart - ? ` • ${meta.reactions.heart} Liked` - : '' + ${likedReaction ? 'Unlike' : 'Like'} + ${ + meta.reactions && meta.reactions.heart + ? ` • ${meta.reactions.heart} Liked` + : '' } ` @@ -34,10 +37,7 @@ function renderHeader({ meta, user, reactions }, instance) { const commentsCount = document.createElement('span') commentsCount.innerHTML = ` - ${ meta.comments - ? ` • ${meta.comments} Comments` - : '' - } + ${meta.comments ? ` • ${meta.comments} Comments` : ''} ` container.appendChild(commentsCount) @@ -51,35 +51,19 @@ function renderHeader({ meta, user, reactions }, instance) { return container } -function renderComments({ meta, comments, commentReactions, currentPage, user, error }, instance) { +function renderComments( + { meta, comments, commentReactions, currentPage, user, error }, + instance +) { const container = document.createElement('div') - container.lang = "en-US" + container.lang = 'en-US' container.className = 'gitment-container gitment-comments-container' if (error) { const errorBlock = document.createElement('div') errorBlock.className = 'gitment-comments-error' - if (error === NOT_INITIALIZED_ERROR - && user.login - && user.login.toLowerCase() === instance.owner.toLowerCase()) { - const initHint = document.createElement('div') - const initButton = document.createElement('button') - initButton.className = 'gitment-comments-init-btn' - initButton.onclick = () => { - initButton.setAttribute('disabled', true) - instance.init() - .catch(e => { - initButton.removeAttribute('disabled') - alert(e) - }) - } - initButton.innerText = 'Initialize Comments' - initHint.appendChild(initButton) - errorBlock.appendChild(initHint) - } else { - errorBlock.innerText = error - } + errorBlock.innerText = error container.appendChild(errorBlock) return container } else if (comments === undefined) { @@ -105,30 +89,42 @@ function renderComments({ meta, comments, commentReactions, currentPage, user, e const commentItem = document.createElement('li') commentItem.className = 'gitment-comment' commentItem.innerHTML = ` - - + +