fix: display of likes
This commit is contained in:
@@ -1,43 +1,46 @@
|
||||
$('#req-evals').on('click', () => {
|
||||
frappe.msgprint("The evaluations have been moved to <a href='https://t.me/fossunited'>Telegram</a>")
|
||||
})
|
||||
var set_likes = function(liked, likes) {
|
||||
var set_likes = function (liked, likes) {
|
||||
let $btn = $('.btn-like');
|
||||
$btn.text(`${likes} 👍`);
|
||||
likes ? $btn.text(`${likes} 👍`): $btn.text(`👍`);
|
||||
if (liked) {
|
||||
$btn.addClass('btn-secondary').removeClass('btn-default');
|
||||
$btn.addClass('btn-dark').removeClass('btn-default');
|
||||
} else {
|
||||
$btn.addClass('btn-default').removeClass('btn-secondary');
|
||||
$btn.addClass('btn-default').removeClass('btn-dark');
|
||||
}
|
||||
};
|
||||
|
||||
// set initial
|
||||
/* frappe.call('community.www.hackathons.project.like', {project: frappe.form_dict.project}, (data) => {
|
||||
set_likes(data.message.action =="Liked", data.message.likes)
|
||||
}) */
|
||||
// set initial likes
|
||||
frappe.ready(() => {
|
||||
var url_params = new URLSearchParams(window.location.search);
|
||||
frappe.call('community.www.hackathons.project.like', { project: url_params.get("project"), initial: true }, (data) => {
|
||||
set_likes(data.message.action == "Liked", data.message.likes)
|
||||
})
|
||||
})
|
||||
|
||||
// like - unlike
|
||||
$('.btn-like').on('click', (e) => {
|
||||
frappe.call('community.www.hackathons.project.like', {project: $(e.target).attr("data-project")}, (data) => {
|
||||
set_likes(data.message.action =="Liked", data.message.likes);
|
||||
frappe.call('community.www.hackathons.project.like', { project: $(e.target).attr("data-project") }, (data) => {
|
||||
set_likes(data.message.action == "Liked", data.message.likes);
|
||||
});
|
||||
});
|
||||
|
||||
// accept / reject
|
||||
$('.btn-accept').on('click', (e) => {
|
||||
frappe.call('community.www.hackathons.project.join_request', {id: $(e.target).attr('data-request-id'), action: 'Accept'}, (data) => {
|
||||
frappe.call('community.www.hackathons.project.join_request', { id: $(e.target).attr('data-request-id'), action: 'Accept' }, (data) => {
|
||||
window.location.reload();
|
||||
});
|
||||
});
|
||||
|
||||
$('.btn-reject').on('click', (ev) => {
|
||||
frappe.call('community.www.hackathons.project.join_request', {id: $(ev.target).attr('data-request-id'), action: 'Reject'}, (data) => {
|
||||
frappe.call('community.www.hackathons.project.join_request', { id: $(ev.target).attr('data-request-id'), action: 'Reject' }, (data) => {
|
||||
window.location.reload();
|
||||
});
|
||||
});
|
||||
|
||||
$('.btn-leave').on('click', (ev) => {
|
||||
frappe.call('community.www.hackathons.project.join_request', {id: $(ev.target).attr('data-request-id'), action: 'Reject'}, (data) => {
|
||||
frappe.call('community.www.hackathons.project.join_request', { id: $(ev.target).attr('data-request-id'), action: 'Reject' }, (data) => {
|
||||
window.location.reload();
|
||||
});
|
||||
});
|
||||
@@ -14,7 +14,6 @@ def get_context(context):
|
||||
context.hackathon = hackathon
|
||||
context.members = get_members(project)
|
||||
context.confirmed_members = get_comfirmed_members(project)
|
||||
context.likes = get_project_likes(project)
|
||||
context.updates = get_updates(project)
|
||||
if frappe.session.user != "Guest":
|
||||
context.my_project = get_my_projects()
|
||||
@@ -35,9 +34,6 @@ def get_members(project_name):
|
||||
def get_comfirmed_members(project_name):
|
||||
return frappe.get_all("Community Project Member", {"project": project_name, "status": ("=", "Accepted") }, ['name'])
|
||||
|
||||
def get_project_likes(project_name):
|
||||
return frappe.get_all("Community Project Like", {"project": project_name})
|
||||
|
||||
def get_updates(project_name):
|
||||
return frappe.get_all('Community Project Update', {"project": project_name}, ['owner', 'creation', '`update` as project_update'])
|
||||
|
||||
@@ -68,25 +64,27 @@ def join_request(id, action):
|
||||
frappe.db.set_value('Community Project Member', id, 'status', 'Rejected')
|
||||
|
||||
def has_already_liked(project):
|
||||
try:
|
||||
likes = frappe.get_doc("Community Project Like", {"project": project, "owner": frappe.session.user})
|
||||
return likes
|
||||
except frappe.DoesNotExistError:
|
||||
return None
|
||||
likes = frappe.db.get_value('Community Project Like', {"owner": frappe.session.user, "project": project})
|
||||
return likes
|
||||
|
||||
@frappe.whitelist()
|
||||
def like(project):
|
||||
liked_project = has_already_liked(project)
|
||||
if liked_project:
|
||||
action= "Unliked"
|
||||
liked_project.delete()
|
||||
else:
|
||||
action= "Liked"
|
||||
frappe.get_doc({"doctype": "Community Project Like","project": project}).save()
|
||||
def get_project_likes(project):
|
||||
return len(frappe.get_all("Community Project Like", {"project": project}))
|
||||
|
||||
likes = frappe.db.get_all("Community Project Like", {"project": project})
|
||||
frappe.db.set_value("Community Project", project, "likes", len(likes))
|
||||
@frappe.whitelist()
|
||||
def like(project, initial=False):
|
||||
liked_project = has_already_liked(project)
|
||||
action = "Liked" if (liked_project and initial) else "Unliked"
|
||||
if not initial:
|
||||
if liked_project:
|
||||
action = "Unliked"
|
||||
frappe.get_doc("Community Project Like", liked_project).delete()
|
||||
else:
|
||||
action = "Liked"
|
||||
frappe.get_doc({"doctype": "Community Project Like","project": project}).save()
|
||||
|
||||
frappe.db.set_value("Community Project", project, "likes", get_project_likes(project))
|
||||
return {
|
||||
"action": action,
|
||||
"likes": len(likes)
|
||||
"likes": get_project_likes(project)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user