Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 110 additions & 38 deletions browser/main/NoteList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import context from 'browser/lib/context'
const { remote } = require('electron')
const { dialog } = remote
const WP_POST_PATH = '/wp/v2/posts'
const WP_TAGS_PATH = '/wp/v2/tags'

function sortByCreatedAt (a, b) {
return new Date(b.createdAt) - new Date(a.createdAt)
Expand Down Expand Up @@ -721,47 +722,54 @@ class NoteList extends React.Component {
} else {
authToken = `Bearer ${token}`
}
const contentToRender = firstNote.content.replace(`# ${firstNote.title}`, '')
const markdown = new Markdown()
const data = {
title: firstNote.title,
content: markdown.render(contentToRender),
status: 'publish'
}

let url = ''
let method = ''
if (firstNote.blog && firstNote.blog.blogId) {
url = `${address}${WP_POST_PATH}/${firstNote.blog.blogId}`
method = 'PUT'
} else {
url = `${address}${WP_POST_PATH}`
method = 'POST'
}
// eslint-disable-next-line no-undef
fetch(url, {
method: method,
body: JSON.stringify(data),
headers: {
'Authorization': authToken,
'Content-Type': 'application/json'
this.getTagIdList(address, authToken, firstNote.tags).then(tagIdList => {
const contentToRender = firstNote.content.replace(`# ${firstNote.title}`, '')
const markdown = new Markdown()
const data = {
title: firstNote.title,
content: markdown.render(contentToRender),
status: 'publish',
tags: tagIdList
}
}).then(res => res.json())
.then(response => {
if (_.isNil(response.link) || _.isNil(response.id)) {
return Promise.reject()
}
firstNote.blog = {
blogLink: response.link,
blogId: response.id

let url = ''
let method = ''
if (firstNote.blog && firstNote.blog.blogId) {
url = `${address}${WP_POST_PATH}/${firstNote.blog.blogId}`
method = 'PUT'
} else {
url = `${address}${WP_POST_PATH}`
method = 'POST'
}
// eslint-disable-next-line no-undef
fetch(url, {
method: method,
body: JSON.stringify(data),
headers: {
'Authorization': authToken,
'Content-Type': 'application/json'
}
this.save(firstNote)
this.confirmPublish(firstNote)
})
.catch((error) => {
console.error(error)
this.confirmPublishError()
})
}).then(res => res.json())
.then(response => {
if (_.isNil(response.link) || _.isNil(response.id)) {
return Promise.reject()
}
firstNote.blog = {
blogLink: response.link,
blogId: response.id
}
this.save(firstNote)
this.confirmPublish(firstNote)
})
.catch((error) => {
console.error(error)
this.confirmPublishError()
})
}).catch(error => {
console.error(error)
this.confirmPublishError()
})
}

confirmPublishError () {
Expand Down Expand Up @@ -789,6 +797,70 @@ class NoteList extends React.Component {
}
}

getTagIdList (serverUrl, authToken, tagList = []) { // get tag id
if (tagList.length === 0) {
return Promise.resolve([])
} else {
return Promise.all(tagList.map(tagName => this.getTagId(serverUrl, authToken, tagName)))
}
}

getTagId (serverUrl, authToken, tagName) { // if tag not exist at server, create this tag
tagName = tagName.toLowerCase()
return new Promise((resolve, reject) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.searchTag() seems to returns a Promise. Why do we need to create another Promise?

this.searchTag(serverUrl, tagName).then(id => {
if (id === -1) {
this.createTag(serverUrl, authToken, tagName).then(newId => {
resolve(newId)
}).catch(error => {
reject(error)
})
} else {
resolve(id)
}
}).catch(error => {
reject(error)
})
})
}

createTag (serverUrl, authToken, tagName) {
return new Promise((resolve, reject) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem

fetch(`${serverUrl}${WP_TAGS_PATH}`, {
method: 'POST',
body: JSON.stringify({
name: tagName
}),
headers: {
'Authorization': authToken,
'Content-Type': 'application/json'
}

}).then(res => res.json()).then(response => {
resolve(response.id)
}).catch(error => {
reject(error)
})
})
}

searchTag (serverUrl, tagName) { // search tag name
return new Promise((resolve, reject) => {
fetch(`${serverUrl}${WP_TAGS_PATH}?search=${tagName}`, {
method: 'GET'
}).then(res => res.json()).then(response => {
const tag = response.find(item => (item.name === tagName))
if (tag) {
resolve(tag.id)
} else {
resolve(-1)
}
}).catch(error => {
reject(error)
})
})
}

openBlog (note) {
const { shell } = electron
shell.openExternal(note.blog.blogLink)
Expand Down