From fce2e9cb320645aa2b78d103c0bf78dd9daa9dae Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Thu, 14 Nov 2024 09:46:09 -0800 Subject: [PATCH] Add a similar holdings component --- .../similar_holdings_component.rb | 14 ++++++++++++++ app/javascript/controllers/index.js | 3 +++ .../controllers/lookup_title_controller.js | 18 ++++++++++++++++++ .../catalog/record/_metadata_panels.html.erb | 1 + .../similar_holdings_component_spec.rb | 15 +++++++++++++++ 5 files changed, 51 insertions(+) create mode 100644 app/components/access_panels/similar_holdings_component.rb create mode 100644 app/javascript/controllers/lookup_title_controller.js create mode 100644 spec/components/access_panels/similar_holdings_component_spec.rb diff --git a/app/components/access_panels/similar_holdings_component.rb b/app/components/access_panels/similar_holdings_component.rb new file mode 100644 index 000000000..d34cd442b --- /dev/null +++ b/app/components/access_panels/similar_holdings_component.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module AccessPanels + class SimilarHoldingsComponent < ViewComponent::Base + def initialize(document:) + @id = document.id + super() + end + + def call + helpers.turbo_frame_tag 'similar_holdings', src: "https://semantic-search-demo.stanford.edu/similar/#{@id}" + end + end +end diff --git a/app/javascript/controllers/index.js b/app/javascript/controllers/index.js index aead314ba..1da6cce70 100644 --- a/app/javascript/controllers/index.js +++ b/app/javascript/controllers/index.js @@ -21,3 +21,6 @@ application.register("preview-brief", PreviewBriefController) import PreviewFilmstripController from "./preview_filmstrip_controller" application.register("preview-filmstrip", PreviewFilmstripController) + +import LookupTitleController from "./lookup_title_controller" +application.register("lookup-title", LookupTitleController) diff --git a/app/javascript/controllers/lookup_title_controller.js b/app/javascript/controllers/lookup_title_controller.js new file mode 100644 index 000000000..4e34a3199 --- /dev/null +++ b/app/javascript/controllers/lookup_title_controller.js @@ -0,0 +1,18 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + static values = { + druid: String + } + + connect() { + fetch(`https://purl.stanford.edu/${this.druidValue}.json`) + .then(response => response.json()) + .then(json => this.setJson(json)) + } + + setJson(json) { + const title = json.label + this.element.innerHTML = title + } +} diff --git a/app/views/catalog/record/_metadata_panels.html.erb b/app/views/catalog/record/_metadata_panels.html.erb index 72d01f26e..67c7509d7 100644 --- a/app/views/catalog/record/_metadata_panels.html.erb +++ b/app/views/catalog/record/_metadata_panels.html.erb @@ -3,6 +3,7 @@ <%= render AccessPanels::CourseReservesComponent.new(document: document) %> <%= render AccessPanels::AtTheLibraryComponent.new(document: document) %> + <%= render AccessPanels::SimilarHoldingsComponent.new(document: document) %> <% context = capture do %> <%= render AccessPanels::AppearsInComponent.new(document: document) %> diff --git a/spec/components/access_panels/similar_holdings_component_spec.rb b/spec/components/access_panels/similar_holdings_component_spec.rb new file mode 100644 index 000000000..c79ef395e --- /dev/null +++ b/spec/components/access_panels/similar_holdings_component_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe AccessPanels::SimilarHoldingsComponent, type: :component do + pending "add some examples to (or delete) #{__FILE__}" + + # it "renders something useful" do + # expect( + # render_inline(described_class.new(attr: "value")) { "Hello, components!" }.css("p").to_html + # ).to include( + # "Hello, components!" + # ) + # end +end