-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#2191 edit updates #2198
#2191 edit updates #2198
Changes from all commits
07247c6
4b82ce5
1778fdc
347d906
55581fc
cd0b69e
66350dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// Akvo RSR is covered by the GNU Affero General Public License. | ||
// See more details in the license.txt file located at the root folder of the | ||
// Akvo RSR module. For additional details on the GNU license please see | ||
// < http://www.gnu.org/licenses/agpl.html >. | ||
|
||
// DEFAULT VALUES | ||
var defaultValues = JSON.parse(document.getElementById("default-values").innerHTML); | ||
|
||
// CSRF TOKEN | ||
function getCookie(name) { | ||
var cookieValue = null; | ||
if (document.cookie && document.cookie !== '') { | ||
var cookies = document.cookie.split(';'); | ||
for (var i = 0; i < cookies.length; i++) { | ||
var cookie = cookies[i].trim(); | ||
// Does this cookie string begin with the name we want? | ||
if (cookie.substring(0, name.length + 1) == (name + '=')) { | ||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); | ||
break; | ||
} | ||
} | ||
} | ||
return cookieValue; | ||
} | ||
|
||
var csrftoken = getCookie('csrftoken'); | ||
|
||
// display error message in empty div below title | ||
function setError(message) { | ||
var errorNode = document.getElementById('projectUpdateError'); | ||
errorNode.innerHTML = message; | ||
} | ||
|
||
// display confirmation message before deleting update | ||
function confirmDeleteUpdate(node) { | ||
return function(e) { | ||
e.preventDefault(); | ||
|
||
// check if delete button is enabled | ||
if ((' ' + node.className + ' ').indexOf(' disabled ') == -1) { | ||
|
||
var updateId = node.id.split('-')[1]; | ||
var confirmId = 'confirm-delete-' + updateId; | ||
|
||
var confirmNode = document.getElementById(confirmId); | ||
node.setAttribute('class', 'delete-update disabled'); | ||
|
||
var sureNode = document.createElement('span'); | ||
sureNode.innerHTML = defaultValues.sure_message; | ||
|
||
var yesNode = document.createElement('a'); | ||
yesNode.setAttribute('style', 'color: green; margin-left: 5px;'); | ||
yesNode.onclick = confirmDelete(yesNode, updateId); | ||
yesNode.innerHTML = defaultValues.yes; | ||
|
||
var noNode = document.createElement('a'); | ||
noNode.setAttribute('style', 'color: red; margin-left: 5px;'); | ||
noNode.onclick = dismissConfirmationNo(sureNode, updateId); | ||
noNode.innerHTML = defaultValues.no; | ||
|
||
sureNode.appendChild(yesNode); | ||
sureNode.appendChild(noNode); | ||
confirmNode.appendChild(sureNode); | ||
|
||
} | ||
|
||
}; | ||
} | ||
|
||
function dismissConfirmationNo(sureNode, updateId) { | ||
return function(e) { | ||
e.preventDefault(); | ||
|
||
dismissConfirmation(sureNode, updateId); | ||
}; | ||
} | ||
|
||
function dismissConfirmation(sureNode, updateId) { | ||
var parentNode = sureNode.parentNode; | ||
parentNode.removeChild(sureNode); | ||
|
||
var updateNodeId = 'update-' + updateId; | ||
deleteButtonNode = document.getElementById(updateNodeId); | ||
deleteButtonNode.setAttribute('class', 'delete-update'); | ||
} | ||
|
||
function confirmDelete(yesNode, updateId) { | ||
return function(e) { | ||
e.preventDefault(); | ||
var sureNode = yesNode.parentNode; | ||
sureNode.innerHTML = defaultValues.delete_progress; | ||
// var parentNode = sureNode.parentNode; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two lines can be removed 😄 |
||
// parentNode.removeChild(sureNode); | ||
|
||
deleteUpdate(sureNode, updateId); | ||
}; | ||
} | ||
|
||
// make api call to delete specified update | ||
function deleteUpdate(sureNode, updateId) { | ||
|
||
var api_url = '/rest/v1/project_update/' + updateId + '/?format=json', | ||
request = new XMLHttpRequest(); | ||
|
||
request.open('DELETE', api_url, true); | ||
request.setRequestHeader("X-CSRFToken", csrftoken); | ||
request.setRequestHeader("Content-type", "application/json"); | ||
|
||
request.onload = function() { | ||
if (request.status >= 204 && request.status < 300) { | ||
// Successfully deleted update, remove container. | ||
removeUpdateContainer(updateId); | ||
} else if (request.status == 404) { | ||
// Update not found, most likely already deleted | ||
dismissConfirmation(sureNode, updateId); | ||
setError(defaultValues.error_delete); | ||
} else { | ||
// We reached our target server, but it returned an error | ||
dismissConfirmation(sureNode, updateId); | ||
setError(request.status + defaultValues.error_misc); | ||
} | ||
}; | ||
|
||
request.onerror = function() { | ||
// There was a connection error of some sort | ||
setError(defaultValues.error_connection); | ||
return false; | ||
}; | ||
|
||
request.send(); | ||
} | ||
|
||
// remove update from screen once it has been delete | ||
function removeUpdateContainer(updateId) { | ||
var nodeId = 'update-' + updateId + '-container'; | ||
var removeNode = document.getElementById(nodeId); | ||
var parentNode = removeNode.parentNode; | ||
parentNode.removeChild(removeNode); | ||
} | ||
|
||
// add onlick to all delete buttons | ||
function setDeleteUpdateOnClick() { | ||
var deleteUpdateNodes = document.querySelectorAll('.delete-update'); | ||
|
||
if (deleteUpdateNodes !== null) { | ||
for (var i = 0; i < deleteUpdateNodes.length; i++) { | ||
deleteUpdateNodes[i].onclick = confirmDeleteUpdate(deleteUpdateNodes[i]); | ||
} | ||
} | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', function() { | ||
setDeleteUpdateOnClick(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,8 @@ <h2>{% if edit_mode %}{% trans 'Edit an update' %}{% else %}{% trans 'Add an upd | |
{% else %} | ||
{% if edit_mode %} | ||
<p class="small"> | ||
{% blocktrans with update_time=update.time_gmt|date:"H:i T" expires_time=update.expires_at|date:"H:i T" %} | ||
You posted this update at {{update_time}}. You have until {{expires_time}} to save your edits. | ||
{% blocktrans with update_time=update.time_gmt|date:"H:i T" %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add the date here as well. Previously you would not be able to see this on a different date (since we only allowed editing within 20 minutes), but now it's a bit weird to see "You posted this update at 14:29 GMT." when that was two weeks ago. |
||
You posted this update at {{update_time}}. | ||
{% endblocktrans %} | ||
</p> | ||
{% endif %} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe change this line to:
if (node.className.indexOf('disabled') < 0) {
?