Skip to content

Commit e85ecef

Browse files
committed
reworked tag rename response handling
1 parent 9c5d3ea commit e85ecef

File tree

5 files changed

+55
-29
lines changed

5 files changed

+55
-29
lines changed

app/assets/javascripts/qpixel_api.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,16 @@ window.QPixel = {
493493

494494
const data = await resp.json();
495495

496+
return data;
497+
},
498+
499+
renameTag: async (categoryId, tagId, name) => {
500+
const resp = await QPixel.fetchJSON(`/categories/${categoryId}/tags/${tagId}/rename`, { name }, {
501+
headers: { 'Accept': 'application/json' }
502+
});
503+
504+
const data = await resp.json();
505+
496506
return data;
497507
}
498508
};

app/assets/javascripts/tags.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,15 @@ $(() => {
193193
const tagName = $tgt.attr('data-name');
194194

195195
const renameTo = prompt(`Rename tag ${tagName} to:`);
196-
if (!!renameTo) {
197-
const resp = await QPixel.fetchJSON(`/categories/${categoryId}/tags/${tagId}/rename`, { name: renameTo });
198-
199-
const data = await resp.json();
200196

201-
if (data.success) {
202-
location.reload();
203-
}
204-
else {
205-
QPixel.createNotification('danger', `Failed to rename the tag. (${resp.status})`);
206-
}
197+
if (!renameTo) {
198+
return;
207199
}
200+
201+
const data = await QPixel.renameTag(categoryId, tagId, renameTo);
202+
203+
QPixel.handleJSONResponse(data, () => {
204+
location.reload();
205+
});
208206
});
209207
});

app/controllers/tags_controller.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,14 @@ def rename
141141
end
142142
end
143143

144-
render json: { success: status, tag: @tag }, status: status ? :ok : :bad_request
144+
if status
145+
render json: { status: 'success', tag: @tag }
146+
else
147+
render json: { status: 'failed',
148+
message: 'Failed to rename the tag.',
149+
tag: @tag },
150+
status: :bad_request
151+
end
145152
end
146153

147154
def select_merge; end

global.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,15 @@ interface QPixel {
364364
*/
365365
lockThread?: (id: string) => Promise<QPixelResponseJSON>
366366

367+
/**
368+
* Attempts to rename a tag
369+
* @param categoryId id of the category to rename the tag in
370+
* @param tagId id of the tag to rename
371+
* @param name new tag name
372+
* @returns result of the operation
373+
*/
374+
renameTag?: (categoryId: string, tagId: string, name: string) => Promise<QPixelResponseJSON>
375+
367376
/**
368377
* Attempts to raise a flag
369378
* @param flag new flag data

test/controllers/tags_controller_test.rb

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,23 +176,16 @@ class TagsControllerTest < ActionController::TestCase
176176
sign_in users(:moderator)
177177

178178
tag = tags(:base)
179-
180179
new_tag_name = 'renamed'
181180

182-
post :rename, params: {
183-
format: :json,
184-
id: categories(:main).id,
185-
name: new_tag_name,
186-
tag_id: tag.id,
187-
tag: tag
188-
}
181+
try_rename_tag(categories(:main), tag, new_tag_name)
189182

190183
assert_response(:success)
191184
assert_valid_json_response
192185

193186
res_body = JSON.parse(response.body)
194187

195-
assert_equal true, res_body['success']
188+
assert_equal 'success', res_body['status']
196189
assert_equal new_tag_name, res_body['tag']['name']
197190

198191
log_entry = AuditLog.last
@@ -204,24 +197,33 @@ class TagsControllerTest < ActionController::TestCase
204197
sign_in users(:moderator)
205198

206199
tag = tags(:base)
207-
208200
old_tag_name = tag.name
209201

210-
post :rename, params: {
211-
format: :json,
212-
id: categories(:main).id,
213-
name: '',
214-
tag_id: tag.id,
215-
tag: tag
216-
}
202+
try_rename_tag(categories(:main), tag, '')
217203

218204
assert_response(:bad_request)
219205
assert_valid_json_response
220206

221207
res_body = JSON.parse(response.body)
222208

223-
assert_equal false, res_body['success']
209+
assert_equal 'failed', res_body['status']
210+
assert_no_nil res_body['message']
224211
tag.reload
225212
assert_equal tag.name, old_tag_name
226213
end
214+
215+
private
216+
217+
# @param category [Category] category to rename the tag in
218+
# @param tag [Tag] tag to rename
219+
# @param name [String] new tag name
220+
def try_rename_tag(category, tag, name)
221+
post :rename, params: {
222+
format: :json,
223+
id: category.id,
224+
name: name,
225+
tag_id: tag.id,
226+
tag: tag
227+
}
228+
end
227229
end

0 commit comments

Comments
 (0)