Skip to content

Instantly share code, notes, and snippets.

@Alavi1412
Created April 24, 2017 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alavi1412/aa305cc75e2e992f9dc004e9a20f3ff0 to your computer and use it in GitHub Desktop.
Save Alavi1412/aa305cc75e2e992f9dc004e9a20f3ff0 to your computer and use it in GitHub Desktop.
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