Created
April 24, 2017 15:33
-
-
Save Alavi1412/aa305cc75e2e992f9dc004e9a20f3ff0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createWidget } from 'discourse/widgets/widget'; | |
import { h } from 'virtual-dom'; | |
import { avatarImg } from 'discourse/widgets/post'; | |
import { ajax } from 'discourse/lib/ajax'; | |
import { popupAjaxError } from 'discourse/lib/ajax-error'; | |
export default createWidget('widget-bookmark', { | |
tagName: 'div.widget-bookmark.widget-container', | |
buildKey: (attrs) => 'widget-bookmark', | |
defaultState(attrs) { | |
return { | |
data: [], | |
loaded: false, | |
contents: [] | |
} | |
}, | |
sendBookmark() { | |
let self = this; | |
return ajax("/posts/" + self.state.data.post_id + "/bookmark", { | |
type: 'PUT', | |
data: { bookmarked: false } | |
}).then(function (res, err){ | |
if (err) { | |
popupAjaxError(err); | |
} | |
else | |
{ | |
self.state.loaded = false; | |
self.state.contents = []; | |
self.scheduleRerender(); | |
}; | |
}); | |
}, | |
getData(){ | |
var user = Discourse.User.currentProp('username'); | |
var bookmarked; | |
let self = this; | |
if (user) | |
{ | |
self.state.loaded = true; | |
ajax(`user_actions.json?username=${user}&filter=3&no_results_help_key=user_activity.no_bookmarks`) | |
.then(function(res,err){ | |
bookmarked = res.user_actions; | |
for(var i = 0 ; i < bookmarked.length ; i++) | |
{ | |
self.state.data = bookmarked[i]; | |
self.state.contents.push( | |
self.attach('button', { | |
action: 'sendBookmark', | |
title: bookmarked[i].title, | |
icon: 'remove', | |
className: 'btn bookmark bookmarked' | |
})); | |
self.state.contents.push(h("div.bookmarkBlock",[h("a.bookmarded-link",{attributes: | |
{href: "/t/" + bookmarked[i].slug + "/" + bookmarked[i].topic_id }}, | |
h("span.bookmarked-title", bookmarked[i].title))])); | |
self.state.contents.push(h("br")); | |
} | |
self.scheduleRerender(); | |
}); | |
} | |
}, | |
html(attrs, state) { | |
if (state.loaded == false) | |
this.getData(); | |
return h('div.widget-inner', state.contents); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment