Skip to content

Commit 85d39a7

Browse files
committed
fix edit / delete note
1 parent 962e5d5 commit 85d39a7

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

lua/gitlab/actions/discussions/init.lua

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ local M = {
3636

3737
---Load the discussion data, storage them in M.discussions and M.unlinked_discussions and call
3838
---callback with data
39-
---@param callback fun(data: DiscussionData): nil
39+
---@param callback (fun(data: DiscussionData): nil)?
4040
M.load_discussions = function(callback)
4141
job.run_job("/discussions/list", "POST", { blacklist = state.settings.discussion_tree.blacklist }, function(data)
4242
M.discussions = data.discussions
4343
M.unlinked_discussions = data.unlinked_discussions
44-
callback(data)
44+
if type(callback) == "function" then
45+
callback(data)
46+
end
4547
end)
4648
end
4749

@@ -482,6 +484,7 @@ M.send_reply = function(tree, discussion_id)
482484
job.run_job("/reply", "POST", body, function(data)
483485
u.notify("Sent reply!", vim.log.levels.INFO)
484486
M.add_reply_to_tree(tree, data.note, discussion_id)
487+
M.load_discussions()
485488
end)
486489
end
487490
end
@@ -506,7 +509,7 @@ M.send_deletion = function(tree, unlinked)
506509
local root_node = M.get_root_node(tree, current_node)
507510
local note_id = note_node.is_root and root_node.root_note_id or note_node.id
508511

509-
local body = { discussion_id = root_node.id, note_id = note_id }
512+
local body = { discussion_id = root_node.id, note_id = tonumber(note_id) }
510513

511514
job.run_job("/comment", "DELETE", body, function(data)
512515
u.notify(data.message, vim.log.levels.INFO)
@@ -553,11 +556,14 @@ M.edit_comment = function(tree, unlinked)
553556
vim.api.nvim_buf_set_lines(currentBuffer, 0, -1, false, lines)
554557
state.set_popup_keymaps(
555558
edit_popup,
556-
M.send_edits(tostring(root_node.id), note_node.root_note_id or note_node.id, unlinked)
559+
M.send_edits(tostring(root_node.id), tonumber(note_node.root_note_id or note_node.id), unlinked)
557560
)
558561
end
559562

560-
-- This function sends the edited comment to the Go server
563+
---This function sends the edited comment to the Go server
564+
---@param discussion_id string
565+
---@param note_id integer
566+
---@param unlinked boolean
561567
M.send_edits = function(discussion_id, note_id, unlinked)
562568
return function(text)
563569
local body = {
@@ -568,10 +574,10 @@ M.send_edits = function(discussion_id, note_id, unlinked)
568574
job.run_job("/comment", "PATCH", body, function(data)
569575
u.notify(data.message, vim.log.levels.INFO)
570576
if unlinked then
571-
M.unlinked_discussions = M.replace_text(M.unlinked_discussions, discussion_id, note_id, text)
577+
M.replace_text(M.unlinked_discussions, discussion_id, note_id, text)
572578
M.rebuild_unlinked_discussion_tree()
573579
else
574-
M.discussions = M.replace_text(M.discussions, discussion_id, note_id, text)
580+
M.replace_text(M.discussions, discussion_id, note_id, text)
575581
M.rebuild_discussion_tree()
576582
end
577583
end)
@@ -869,13 +875,17 @@ M.redraw_resolved_status = function(tree, note, mark_resolved)
869875
tree:render()
870876
end
871877

878+
---Replace text in discussion after note update.
879+
---@param data Discussion[]|UnlinkedDiscussion[]
880+
---@param discussion_id string
881+
---@param note_id integer
882+
---@param text string
872883
M.replace_text = function(data, discussion_id, note_id, text)
873884
for i, discussion in ipairs(data) do
874885
if discussion.id == discussion_id then
875886
for j, note in ipairs(discussion.notes) do
876887
if note.id == note_id then
877888
data[i].notes[j].body = text
878-
return data
879889
end
880890
end
881891
end
@@ -919,7 +929,7 @@ M.get_note_node = function(tree, node)
919929
end
920930

921931
M.add_reply_to_tree = function(tree, note, discussion_id)
922-
local note_node = M.build_note(note)
932+
local note_node = discussions_tree.build_note(note)
923933
note_node:expand()
924934
tree:add_node(note_node, discussion_id and ("-" .. discussion_id) or nil)
925935
tree:render()

lua/gitlab/actions/discussions/tree.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ end
123123
---@return NuiTree.Node
124124
---@return string
125125
---@return NuiTree.Node[]
126-
local function build_note(note, resolve_info)
126+
M.build_note = function(note, resolve_info)
127127
local text, text_nodes = build_note_body(note, resolve_info)
128128
local note_node = NuiTree.Node({
129129
text = text,
@@ -163,7 +163,7 @@ M.add_discussions_to_table = function(items, unlinked)
163163

164164
for j, note in ipairs(discussion.notes) do
165165
if j == 1 then
166-
_, root_text, root_text_nodes = build_note(note, { resolved = note.resolved, resolvable = note.resolvable })
166+
_, root_text, root_text_nodes = M.build_note(note, { resolved = note.resolved, resolvable = note.resolvable })
167167

168168
root_file_name = (type(note.position) == "table" and note.position.new_path or nil)
169169
root_new_line = (type(note.position) == "table" and note.position.new_line or nil)
@@ -173,7 +173,7 @@ M.add_discussions_to_table = function(items, unlinked)
173173
resolvable = note.resolvable
174174
resolved = note.resolved
175175
else -- Otherwise insert it as a child node...
176-
local note_node = build_note(note)
176+
local note_node = M.build_note(note)
177177
table.insert(discussion_children, note_node)
178178
end
179179
end

0 commit comments

Comments
 (0)