Skip to content

Commit

Permalink
move js to public, move _add_notes to _notes
Browse files Browse the repository at this point in the history
  • Loading branch information
moozzi committed Nov 4, 2023
1 parent 6d50182 commit 2d29fa8
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ class App < Sinatra::Base
if @record.notes.create!(body: params[:body])
flash[:success] = "Note added successfully."
else
flash[:danger] = "Something went wrong!"
flash[:danger] = "Failed to create Note."
end

redirect "/db/#{name}/#{params[:id]}"
Expand Down
28 changes: 28 additions & 0 deletions public/javascript/notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const Notes = {
init() {
(document.querySelectorAll('.delete-note') || []).forEach(button => {
button.addEventListener('click', () => {
const noteId = button.getAttribute('data-note-id');
Notes.delete(noteId, button)
});
});
},

delete(noteId, button) {
fetch(`${document.location}/notes/${noteId}`, {
method: 'DELETE'
})
.then(response => {
if (response.ok) {
button.parentElement.parentElement.remove();
} else {
console.error('Failed to delete note');
}
})
.catch(error => {
console.error('Error:', error);
});
}
};

ready(Notes.init);
31 changes: 4 additions & 27 deletions views/_notes.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<% if notes.present? %>
<script type="text/javascript" src="/javascript/notes.js"></script>

<% unless notes.empty? %>
<h3>Notes</h3>
<% notes.each do |note| %>
<div class="box">
Expand All @@ -15,29 +17,4 @@
<% end %>
<% end %>

<script>
const deleteButtons = document.querySelectorAll('.delete-note');
const path = '<%= request.path_info %>'
deleteButtons.forEach(button => {
button.addEventListener('click', () => {
const noteId = button.getAttribute('data-note-id');
deleteNote(noteId, button)
});
});

function deleteNote(noteId, button) {
fetch(`${path}/notes/${noteId}`, {
method: 'DELETE'
})
.then(response => {
if (response.ok) {
button.parentElement.parentElement.remove();
} else {
console.error('Failed to delete note');
}
})
.catch(error => {
console.error('Error:', error);
});
}
</script>
<%= partial(:add_note) %>
2 changes: 0 additions & 2 deletions views/db/advisories/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,3 @@
</table>

<%= partial(:notes, notes: @advisory.notes) %>

<%= partial(:add_note, parent: @advisory) %>
2 changes: 0 additions & 2 deletions views/db/credentials/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,3 @@
</table>

<%= partial(:notes, notes: @credential.notes) %>

<%= partial(:add_note, parent: @credential) %>
2 changes: 0 additions & 2 deletions views/db/email_addresses/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,3 @@


<%= partial(:notes, notes: @email_address.notes) %>

<%= partial(:add_note, parent: @email_address) %>
2 changes: 0 additions & 2 deletions views/db/host_names/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,3 @@
</table>

<%= partial(:notes, notes: @host_name.notes) %>

<%= partial(:add_note, parent: @host_name) %>
2 changes: 0 additions & 2 deletions views/db/ip_addresses/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,3 @@
</table>

<%= partial(:notes, notes: @ip_address.notes) %>

<%= partial(:add_note, parent: @ip_address) %>
1 change: 0 additions & 1 deletion views/db/mac_addresses/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,3 @@

<%= partial(:notes, notes: @mac_address.notes) %>

<%= partial(:add_note, parent: @mac_address) %>
2 changes: 0 additions & 2 deletions views/db/open_ports/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,3 @@


<%= partial(:notes, notes: @open_port.notes) %>

<%= partial(:add_note, parent: @open_port) %>
2 changes: 0 additions & 2 deletions views/db/passwords/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,3 @@


<%= partial(:notes, notes: @password.notes) %>

<%= partial(:add_note, parent: @password) %>
1 change: 0 additions & 1 deletion views/db/ports/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,3 @@

<%= partial(:notes, notes: @port.notes) %>

<%= partial(:add_note, parent: @port) %>
1 change: 0 additions & 1 deletion views/db/services/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,3 @@

<%= partial(:notes, notes: @service.notes) %>

<%= partial(:add_note, parent: @service) %>
2 changes: 0 additions & 2 deletions views/db/urls/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,3 @@
</table>

<%= partial(:notes, notes: @url.notes) %>

<%= partial(:add_note, parent: @url) %>
2 changes: 0 additions & 2 deletions views/db/user_names/show.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,3 @@
</table>

<%= partial(:notes, notes: @user_name.notes) %>

<%= partial(:add_note, parent: @user_name) %>

0 comments on commit 2d29fa8

Please sign in to comment.