liquid_feedback_frontend
changeset 1618:2f9e1e882de6
Add/remove interest via fetch
author | bsw |
---|---|
date | Mon Feb 01 23:43:54 2021 +0100 (2021-02-01) |
parents | 9e26d81775a1 |
children | 62856200414d |
files | app/main/_layout/default.html app/main/delegation/_info.lua app/main/interest/_action/update.lua app/main/interest/_action/xhr_update.lua app/main/issue/_sidebar.lua app/main/issue/_sidebar_whatcanido.lua model/interest.lua static/js/xhr.js |
line diff
1.1 --- a/app/main/_layout/default.html Mon Feb 01 21:57:34 2021 +0100 1.2 +++ b/app/main/_layout/default.html Mon Feb 01 23:43:54 2021 +0100 1.3 @@ -13,7 +13,7 @@ 1.4 <![endif]--> 1.5 <script type="text/javascript" src="__BASEURL__/static/mdl/material.js"></script> 1.6 <script type="text/javascript" src="__BASEURL__/static/js/jsprotect.js"></script> 1.7 - <script type="text/javascript" src="__BASEURL__/static/js/partialload.js"></script> 1.8 + <script type="text/javascript" src="__BASEURL__/static/js/xhr.js"></script> 1.9 <style> 1.10 </style> 1.11 <!-- WEBMCP SLOTNODIV meta_navigation_style -->
2.1 --- a/app/main/delegation/_info.lua Mon Feb 01 21:57:34 2021 +0100 2.2 +++ b/app/main/delegation/_info.lua Mon Feb 01 23:43:54 2021 +0100 2.3 @@ -185,8 +185,13 @@ 2.4 } 2.5 elseif not issue.half_frozen and not info.own_participation then 2.6 ui.link{ 2.7 - attr = { class = "float-right mdl-button mdl-js-button mdl-button--icon mdl-button--accent" }, 2.8 - module = "interest", action = "update", params = { issue_id = issue.id }, 2.9 + attr = { 2.10 + class = "float-right mdl-button mdl-js-button mdl-button--icon mdl-button--accent", 2.11 + }, 2.12 + form_attr = { 2.13 + onsubmit = "toggleInterest(" .. issue.id .. ", 'issue_" .. issue.id .. "_interest_icon'); return false;" 2.14 + }, 2.15 + module = "interest", action = "update", params = { issue_id = issue.id, interested = true }, 2.16 routing = { default = { 2.17 mode = "redirect", module = "index", view = "index", params = { 2.18 unit = redirect_unit, area = redirect_area 2.19 @@ -194,13 +199,18 @@ 2.20 anchor = "issue_" .. issue.id 2.21 } }, 2.22 content = function() 2.23 - ui.tag{ tag = "i", attr = { class = "material-icons" }, content = "star_outline" } 2.24 + ui.tag{ tag = "i", attr = { id = "issue_" .. issue.id .. "_interest_icon", class = "material-icons" }, content = "star_outline" } 2.25 end 2.26 } 2.27 elseif not issue.half_frozen and info.own_participation then 2.28 ui.link{ 2.29 - attr = { class = "float-right mdl-button mdl-js-button mdl-button--icon mdl-button--accent" }, 2.30 - module = "interest", action = "update", params = { issue_id = issue.id, delete = true }, 2.31 + attr = { 2.32 + class = "float-right mdl-button mdl-js-button mdl-button--icon mdl-button--accent" 2.33 + }, 2.34 + form_attr = { 2.35 + onsubmit = "toggleInterest(" .. issue.id .. ", 'issue_" .. issue.id .. "_interest_icon'); return false;" 2.36 + }, 2.37 + module = "interest", action = "update", params = { issue_id = issue.id, interested = false }, 2.38 routing = { default = { 2.39 mode = "redirect", module = "index", view = "index", params = { 2.40 unit = redirect_unit, area = redirect_area 2.41 @@ -208,7 +218,7 @@ 2.42 anchor = "issue_" .. issue.id 2.43 } }, 2.44 content = function() 2.45 - ui.tag{ tag = "i", attr = { class = "material-icons" }, content = "star" } 2.46 + ui.tag{ tag = "i", attr = { id = "issue_" .. issue.id .. "_interest_icon", class = "material-icons" }, content = "star" } 2.47 end 2.48 } 2.49 if not issue.closed and info.own_participation and info.weight and info.weight > 1 then
3.1 --- a/app/main/interest/_action/update.lua Mon Feb 01 21:57:34 2021 +0100 3.2 +++ b/app/main/interest/_action/update.lua Mon Feb 01 23:43:54 2021 +0100 3.3 @@ -1,41 +1,5 @@ 3.4 -local issue_id = assert(param.get("issue_id", atom.integer), "no issue id given") 3.5 - 3.6 -local interest = Interest:by_pk(issue_id, app.session.member.id) 3.7 - 3.8 -local issue = Issue:new_selector():add_where{ "id = ?", issue_id }:for_share():single_object_mode():exec() 3.9 - 3.10 -if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then 3.11 - return execute.view { module = "index", view = "403" } 3.12 -end 3.13 +local issue_id = param.get("issue_id", atom.integer) 3.14 +local interested = param.get("interested", atom.boolean) 3.15 3.16 -if issue.closed then 3.17 - slot.put_into("error", _"This issue is already closed.") 3.18 - return false 3.19 -elseif issue.fully_frozen then 3.20 - slot.put_into("error", _"Voting for this issue has already begun.") 3.21 - return false 3.22 -elseif 3.23 - (issue.half_frozen and issue.phase_finished) or 3.24 - (not issue.accepted and issue.phase_finished) 3.25 -then 3.26 - slot.put_into("error", _"Current phase is already closed.") 3.27 - return false 3.28 -end 3.29 +return Interest:update(issue_id, app.session.member, interested) 3.30 3.31 -if param.get("delete", atom.boolean) then 3.32 - if interest then 3.33 - interest:destroy() 3.34 --- slot.put_into("notice", _"Interest removed") 3.35 - else 3.36 --- slot.put_into("notice", _"Interest already removed") 3.37 - end 3.38 - return 3.39 -end 3.40 - 3.41 -if not interest then 3.42 - interest = Interest:new() 3.43 - interest.issue_id = issue_id 3.44 - interest.member_id = app.session.member_id 3.45 - interest:save() 3.46 --- slot.put_into("notice", _"Interest updated") 3.47 -end
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/app/main/interest/_action/xhr_update.lua Mon Feb 01 23:43:54 2021 +0100 4.3 @@ -0,0 +1,9 @@ 4.4 +local issue_id = param.get("issue_id", atom.integer) 4.5 +local interested = param.get("interested", atom.boolean) 4.6 + 4.7 +slot.set_layout() 4.8 + 4.9 +if not Interest:update(issue_id, app.session.member, interested) then 4.10 + request.set_status("500 Internal Server Error") 4.11 +end 4.12 +
5.1 --- a/app/main/issue/_sidebar.lua Mon Feb 01 21:57:34 2021 +0100 5.2 +++ b/app/main/issue/_sidebar.lua Mon Feb 01 23:43:54 2021 +0100 5.3 @@ -86,7 +86,7 @@ 5.4 ui.link { 5.5 attr = { class = "mdl-button mdl-js-button" }, 5.6 module = "interest", action = "update", 5.7 - params = { issue_id = issue.id, delete = true }, 5.8 + params = { issue_id = issue.id, interested = false }, 5.9 routing = { default = { 5.10 mode = "redirect", module = initiative and "initiative" or "issue", view = "show", id = initiative and initiative.id or issue.id 5.11 } }, 5.12 @@ -96,7 +96,7 @@ 5.13 ui.link { 5.14 attr = { class = "mdl-button mdl-js-button" }, 5.15 module = "interest", action = "update", 5.16 - params = { issue_id = issue.id }, 5.17 + params = { issue_id = issue.id, interested = true }, 5.18 routing = { default = { 5.19 mode = "redirect", module = initiative and "initiative" or "issue", view = "show", id = initiative and initiative.id or issue.id 5.20 } },
6.1 --- a/app/main/issue/_sidebar_whatcanido.lua Mon Feb 01 21:57:34 2021 +0100 6.2 +++ b/app/main/issue/_sidebar_whatcanido.lua Mon Feb 01 23:43:54 2021 +0100 6.3 @@ -278,7 +278,7 @@ 6.4 routing = { default = { 6.5 mode = "redirect", module = view_module, view = "show", id = view_id 6.6 } }, 6.7 - params = { issue_id = issue.id, delete = true }, 6.8 + params = { issue_id = issue.id, interested = false }, 6.9 text = _"remove my interest" 6.10 } 6.11 end } 6.12 @@ -291,7 +291,7 @@ 6.13 ui.tag { tag = "li", content = function () 6.14 ui.link { 6.15 module = "interest", action = "update", 6.16 - params = { issue_id = issue.id }, 6.17 + params = { issue_id = issue.id, interested = true }, 6.18 routing = { default = { 6.19 mode = "redirect", module = view_module, view = "show", id = view_id 6.20 } },
7.1 --- a/model/interest.lua Mon Feb 01 21:57:34 2021 +0100 7.2 +++ b/model/interest.lua Mon Feb 01 23:43:54 2021 +0100 7.3 @@ -22,4 +22,45 @@ 7.4 :add_where{ "issue_id = ? AND member_id = ?", issue_id, member_id } 7.5 :optional_object_mode() 7.6 :exec() 7.7 -end 7.8 \ No newline at end of file 7.9 +end 7.10 + 7.11 +function Interest:update(issue_id, member, interested) 7.12 + local interest = Interest:by_pk(issue_id, member.id) 7.13 + 7.14 + local issue = Issue:new_selector():add_where{ "id = ?", issue_id }:for_share():single_object_mode():exec() 7.15 + 7.16 + if not member:has_voting_right_for_unit_id(issue.area.unit_id) then 7.17 + return execute.view { module = "index", view = "403" } 7.18 + end 7.19 + 7.20 + if issue.closed then 7.21 + slot.put_into("error", _"This issue is already closed.") 7.22 + return false 7.23 + elseif issue.fully_frozen then 7.24 + slot.put_into("error", _"Voting for this issue has already begun.") 7.25 + return false 7.26 + elseif 7.27 + (issue.half_frozen and issue.phase_finished) or 7.28 + (not issue.accepted and issue.phase_finished) 7.29 + then 7.30 + slot.put_into("error", _"Current phase is already closed.") 7.31 + return false 7.32 + end 7.33 + 7.34 + if interested == false then 7.35 + if interest then 7.36 + interest:destroy() 7.37 + end 7.38 + return true 7.39 + end 7.40 + 7.41 + if not interest then 7.42 + interest = Interest:new() 7.43 + interest.issue_id = issue_id 7.44 + interest.member_id = app.session.member_id 7.45 + interest:save() 7.46 + end 7.47 + 7.48 + return true 7.49 + 7.50 +end
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/static/js/xhr.js Mon Feb 01 23:43:54 2021 +0100 8.3 @@ -0,0 +1,21 @@ 8.4 +function toggleInterest(issue_id, iconElId) { 8.5 + var iconEl = document.getElementById(iconElId); 8.6 + var interested = iconEl.innerHTML == "star_outline"; 8.7 + 8.8 + var data = new FormData(); 8.9 + data.append("issue_id", issue_id); 8.10 + data.append("interested", interested); 8.11 + 8.12 + fetch("/lf/interest/xhr_update", { 8.13 + method : "POST", 8.14 + body: data 8.15 + }).then( 8.16 + response => { 8.17 + if (response.status == 200) { 8.18 + iconEl.innerHTML = interested ? "star" : "star_outline"; 8.19 + } 8.20 + } 8.21 + ); 8.22 + 8.23 +} 8.24 +