Skip to content

Commit f9a2c0a

Browse files
committed
Update status during term listing changes.
Don't redraw page, which can cause pagination thrash. Term status can change as a side effect when the parent is assigned, but that's the only field that behaves that way, so only update that.
1 parent ad9cd98 commit f9a2c0a

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

lute/templates/term/index.html

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,24 @@
127127
setTimeout(() => { tooltip.fadeOut(200, function() { $(this).remove(); }); }, 800);
128128
}
129129

130+
/**
131+
* Post the update, and update the term status.
132+
* We only need to update the status as that's the only
133+
* thing that might change as a side-effect of setting
134+
* the parent.
135+
*/
130136
function post_update(td, term_id, update_type, update) {
137+
138+
// Update via datatables dom manipulation, keeps the dt
139+
// model consistent.
140+
const _update_status_in_row = function(updated_status) {
141+
const table = $('#termtable').DataTable();
142+
const row = table.row(td.closest('tr'));
143+
const select = row.node().querySelector('.term-status-select');
144+
if (select)
145+
select.value = updated_status;
146+
};
147+
131148
const payload = {
132149
term_id: term_id,
133150
update_type: update_type,
@@ -140,11 +157,8 @@
140157
contentType: 'application/json',
141158
data: JSON.stringify(payload),
142159
success: function(response) {
143-
// console.log('Success:', response.status);
144160
_show_saved_checkmark(td);
145-
// Stay on page 2 if editing an element on page 2. :-P
146-
const change_current_page_on_redraw = false;
147-
$('#termtable').DataTable().draw(change_current_page_on_redraw);
161+
_update_status_in_row(response.status);
148162
},
149163
error: function(xhr) {
150164
if (xhr.responseJSON && xhr.responseJSON.error) {
@@ -262,6 +276,7 @@
262276
const editableStatusCell = function (td, cellData, rowData, row, col) {
263277
function makeDropdown(td) {
264278
const select = document.createElement('select');
279+
select.classList.add('term-status-select');
265280
select.innerHTML = '';
266281
{% for s in update_statuses %}
267282
select.innerHTML += '<option value="{{s.id}}">{{ s.text }}</option>';

lute/term/routes.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,32 @@ def bulk_edit_from_reading_pane():
152152

153153
@bp.route("/ajax_edit_from_index", methods=["POST"])
154154
def ajax_edit_from_index():
155-
"Ajax edit from the term index listing."
155+
"""
156+
Ajax edit from the term index listing.
157+
158+
If successful, returns the term's new status. Only the status is
159+
returned, as that is the only thing that might change as a result
160+
of an ajax update (e.g., if a parent is assigned.
161+
"""
156162
svc = TermService(db.session)
163+
updated_term = None
157164
try:
158165
data = request.get_json()
159166
term_id = int(data.get("term_id", 0))
160167
update_type = data.get("update_type", "")
161168
values = data.get("values")
162169
svc.apply_ajax_update(term_id, update_type, values)
170+
repo = TermRepository(db.session)
171+
updated_term = repo.find(term_id)
163172
except TermServiceException as ex:
164173
return jsonify({"error": str(ex)}), 400
165174
except ValueError as ex:
166175
print(ex, flush=True)
167176
return jsonify({"error": f"Invalid input ({ex})"}), 400
168177
except Exception as ex: # pylint: disable=broad-exception-caught
169178
return jsonify({"error": f"An unexpected error occurred ({ex})"}), 500
170-
return jsonify({"status": "ok"})
179+
180+
return jsonify({"status": updated_term.status})
171181

172182

173183
@bp.route("/export_terms", methods=["POST"])

0 commit comments

Comments
 (0)