Skip to content

Commit

Permalink
Make add and delete labels for authors and artists correctly reflect …
Browse files Browse the repository at this point in the history
…the titles of either author or artist
  • Loading branch information
Janell-Huyck committed Feb 1, 2024
1 parent f0ec2f6 commit 14849a5
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
24 changes: 21 additions & 3 deletions app/assets/javascripts/add_remove_authors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ document.addEventListener('DOMContentLoaded', function() {
authorGroupElement.addEventListener('click', function(event) {
if (event.target &&
event.target.matches("button[type='button']") &&
event.target.textContent === 'Remove Author') {
['Remove Author', 'Remove Artist'].includes(event.target.textContent)
) {
// Navigate up two levels to get the grandparent element, which is the author element.
const authorElement = event.target.parentNode.parentNode;
const authorElements = Array.from(authorGroupElement.children);
Expand All @@ -23,7 +24,15 @@ document.addEventListener('DOMContentLoaded', function() {
}
});

function getRoleFromButton() {
const button = document.getElementById('add_author_btn');
if (!button) return null; // Return null if the button doesn't exist

return button.textContent.includes('Artist') ? 'Artist' : 'Author';
}

function removeAuthor(authorIndex) {
console.log("Remove author at : " + authorIndex);
const authorGroupElement = document.getElementById('author_group');
if (authorGroupElement && authorGroupElement.children[authorIndex]) {
authorGroupElement.children[authorIndex].remove();
Expand Down Expand Up @@ -59,6 +68,9 @@ function createElementWithAttributes(tag, attributes) {
function createInput(type, name) {
// The "for" attribute of the label will be updated with the updateAuthorIds function.

const role = getRoleFromButton();
if (!role) return;

const wrapper = createElementWithAttributes("div", {
class: "col-md-5",
"data-type": type
Expand All @@ -69,7 +81,7 @@ function createInput(type, name) {
"data-type": type
});

label.textContent = name.includes('first_name') ? 'Author First Name' : 'Author Last Name';
label.textContent = name.includes('first_name') ? role + ' First Name' : role + ' Last Name';

const input = createElementWithAttributes("input", {
type: "text",
Expand All @@ -86,12 +98,15 @@ function createInput(type, name) {
}

function createDeleteButton() {
const role = getRoleFromButton();
if (!role) return;

const wrapper = createElementWithAttributes("div", { class: "col-md-2" });
const button = createElementWithAttributes("button", {
type: "button",
class: "form-control form-group bg-danger text-white"
});
button.textContent = "Remove Author";
button.textContent = "Remove " + role;
wrapper.appendChild(button);
return wrapper;
}
Expand All @@ -117,4 +132,7 @@ function updateFieldAndLabel(group, fieldName, index) {
if (label && label.tagName === 'LABEL') {
label.setAttribute('for', input.id);
}



}
8 changes: 4 additions & 4 deletions app/views/partials/_author.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
<% publication.author_first_name.zip(publication.author_last_name).each_with_index do |(first_name, last_name), index| %>
<div class="form-row" data-type="<%= name %>" data-index=<%= index %> id="author_<%= index %>">
<div class="col-md-5">
<%= label_tag("author_first_name_#{index}", 'Author First Name', class: "required") %>
<%= label_tag("author_first_name_#{index}", author_or_artist_label + ' First Name', class: "required") %>
<%= text_field_tag "#{name}[author_first_name][]", first_name, 'data-publication-type': name, id: "author_first_name_#{index}", required: true, class: "form-control form-group" %>
</div>
<div class="col-md-5">
<%= label_tag("author_last_name_#{index}", 'Author Last Name', class: "required") %>
<%= label_tag("author_last_name_#{index}", author_or_artist_label + ' Last Name', class: "required") %>
<%= text_field_tag "#{name}[author_last_name][]", last_name, 'data-publication-type': name, id: "author_last_name_#{index}", required: true, class: "form-control form-group" %>
</div>
<% if index > 0 %>
<div class="col-md-2">
<%= button_tag "Remove Author", type: "button", class: "form-control form-group bg-danger text-white" %>
<%= button_tag "Remove " + author_or_artist_label, type: "button", class: "form-control form-group bg-danger text-white" %>
</div>
<% end %>
</div>
<% end %>
</div>

<%= button_tag "Add Author", type: "button", id: "add_author_btn", onclick: "addAuthor('#{name}')", class: "btn btn-primary" %>
<%= button_tag "Add " + author_or_artist_label, type: "button", id: "add_author_btn", onclick: "addAuthor('#{name}')", class: "btn btn-primary" %>
50 changes: 50 additions & 0 deletions spec/features/author_management/author_vs_artist_labels_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

require 'rails_helper'

# When adding an author or artist for a publication, the page should
# correctly display the title of the author or artist as "Author" or "Artist".
# This is a feature test that checks the behavior of the page when adding
# and deleting authors and artists.
describe 'Author and Artist labels', :feature, js: true do
let(:submitter) { FactoryBot.create(:submitter) }

before do
create_submitter(submitter)
end

it 'uses the title of Author for new books' do
visit new_book_path
expect(page).to have_content('Add Author')
expect(page).not_to have_content('Add Artist')
first_name_fields.last.set('First0')
last_name_fields.last.set('Last0')
click_on 'Add Author'
first_name_fields.last.set('First1')
last_name_fields.last.set('Last1')
click_on 'Add Author'
expect(page).to have_selector('button', text: 'Remove Author', count: 2)
expect(page).not_to have_selector('button', text: 'Remove Artist')
first('button', text: 'Remove Author').click
expect(page).to have_selector('button', text: 'Remove Author', count: 1)
expect(page).not_to have_content('Artist')
end


it 'has the title of Artist for new artworks' do
visit new_artwork_path
expect(page).to have_content('Add Artist')
expect(page).not_to have_content('Add Author')
first_name_fields.last.set('First0')
last_name_fields.last.set('Last0')
click_on 'Add Artist'
first_name_fields.last.set('First1')
last_name_fields.last.set('Last1')
click_on 'Add Artist'
expect(page).to have_selector('button', text: 'Remove Artist', count: 2)
expect(page).not_to have_selector('button', text: 'Remove Author')
first('button', text: 'Remove Artist').click
expect(page).to have_selector('button', text: 'Remove Artist', count: 1)
expect(page).not_to have_content('Author')
end
end
2 changes: 1 addition & 1 deletion spec/features/create_artwork_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# New artwork Page
expect(page).to have_current_path(Rails.application.routes.url_helpers.new_artwork_path)
(0..artwork.author_first_name.count - 1).each do |i|
click_on('Add Author') if i != 0
click_on('Add Artist') if i != 0
all(:xpath, "//input[@name='artwork[author_first_name][]']").last.set(artwork.author_first_name[i])
all(:xpath, "//input[@name='artwork[author_last_name][]']").last.set(artwork.author_last_name[i])
end
Expand Down

0 comments on commit 14849a5

Please sign in to comment.