Skip to content

Commit

Permalink
Merge pull request #1993 from alphagov/render-simple-smart-answer-dia…
Browse files Browse the repository at this point in the history
…gram

Open diagram for smart answer in new tab
  • Loading branch information
andy-collon authored Jan 8, 2024
2 parents 4e018e2 + 4a99766 commit f81c02c
Show file tree
Hide file tree
Showing 10 changed files with 2,232 additions and 1,092 deletions.
25 changes: 25 additions & 0 deletions app/assets/javascripts/modules/smart_answer_flowchart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//= require mermaid/dist/mermaid

(function (Modules) {
'use strict'

Modules.SmartAnswerFlowchart = function () {
this.start = function (element) {
var diagram = element.find('.flowchart')
var flowchartLoading = element.find('.flowchart__loading')

// eslint-disable-next-line
mermaid.initialize(
{
startOnLoad: true,
flowchart: {
useMaxWidth: false
}
}
)

diagram.removeClass('flowchart--hidden')
flowchartLoading.addClass('flowchart__loading--hidden')
}
}
})(window.GOVUKAdmin.Modules)
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// Pages
@import "downtime";
@import "smart_answer_builder";
@import "smart_answer_flowchart";

// GOVUK Design System
@import "govuk_publishing_components/all_components";
14 changes: 14 additions & 0 deletions app/assets/stylesheets/smart_answer_flowchart.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.smart-answer-flowchart {
.mermaid {
background-color: #ffffff;
}

.flowchart--hidden,
.flowchart__loading--hidden {
display: none;
}

pre {
word-break: normal;
}
}
7 changes: 7 additions & 0 deletions app/controllers/editions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ def process_unpublish
end
end

def diagram
# [MT] TODO: What's the best way to handle requests for a diagram for a non-simple smart answer?
if @resource.format != "SimpleSmartAnswer"
render plain: "404 Not Found", status: :not_found
end
end

protected

def permitted_params(subtype: nil)
Expand Down
11 changes: 11 additions & 0 deletions app/views/editions/diagram.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= render 'shared/edition_header' %>

<div class="smart-answer-flowchart" data-module="smart-answer-flowchart">
<pre class="mermaid flowchart flowchart--hidden">
<%= @resource.generate_mermaid %>
</pre>

<p class="flowchart__loading">Generating diagram ...</p>
</div>

<% content_for :page_title, "Diagram for #{@resource.title}" %>
6 changes: 6 additions & 0 deletions app/views/editions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
<div class="tab-content add-top-margin">
<div role="tabpanel" class="tab-pane <% if active_tab.name == 'edit'%>active<% end %>" id="edit">
<div class="link-check-report col-md-4">
<% if @edition.class.to_s == "SimpleSmartAnswerEdition" %>
<p>
View the <%= link_to "flow diagram (opens in a new tab)", diagram_edition_path(@edition), target: '_blank' %>
</p>
<% end %>
<%= render 'link_check_reports/link_check_report', edition: @edition, report: @edition.latest_link_check_report %>
<% if @edition.class.to_s.in?(Edition::HAS_GOVSPEAK_FIELDS) %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
get "tagging", to: "editions#linking"
get "related_external_links", to: "editions#linking"
get "unpublish"
get "diagram"
post "duplicate"
post "update_tagging"
post "process_unpublish"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"stylelint-config-gds": "^1.1.0"
},
"dependencies": {
"mermaid": "10.5.1",
"paste-html-to-govspeak": "^0.4.0"
},
"resolutions": {
Expand Down
54 changes: 54 additions & 0 deletions test/functional/editions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,32 @@ class EditionsControllerTest < ActionController::TestCase
assert_response :success
assert_not_nil assigns(:resource)
end

should "render a link to the diagram when edition is a simple smart answer" do
simple_smart_answer_artefact = FactoryBot.create(
:artefact,
slug: "my-simple-smart-answer",
kind: "guide",
name: "test",
owning_app: "publisher",
)
simple_smart_answer = SimpleSmartAnswerEdition.create!(
title: "test ssa",
panopticon_id: simple_smart_answer_artefact.id,
)

get :show, params: { id: simple_smart_answer.id }

assert_select ".link-check-report p", { text: "View the flow diagram (opens in a new tab)" } do
assert_select "a[href=?]", diagram_edition_path(simple_smart_answer).to_s,
{ count: 1, text: "flow diagram (opens in a new tab)" }
end
end

should "not render a link to the diagram when edition is not a simple smart answer" do
get :show, params: { id: @guide.id }
assert_select "p", { count: 0, text: "View the flow diagram (opens in a new tab)" }
end
end

context "#admin" do
Expand Down Expand Up @@ -1204,4 +1230,32 @@ class EditionsControllerTest < ActionController::TestCase
assert_equal "Option One", question.options.first.label
end
end

context "#diagram" do
context "given a simple smart answer exists" do
setup do
@artefact = FactoryBot.create(:artefact, slug: "foo", name: "Foo", kind: "simple_smart_answer", owning_app: "publisher")
@edition = FactoryBot.create(:simple_smart_answer_edition, body: "blah", state: "draft", slug: "foo", panopticon_id: @artefact.id)
@edition.save!
end

should "render a diagram page for it" do
get :diagram, params: { id: @edition.id }

assert_response :success
assert_select "title", "Diagram for #{@edition.title} | GOV.UK Publisher"
end
end

context "given a non-simple smart answer exists" do
setup do
@welsh_guide = FactoryBot.create(:guide_edition, :welsh, :in_review)
end

should "return a 404" do
get :diagram, params: { id: @welsh_guide.id }
assert_response :not_found
end
end
end
end
Loading

0 comments on commit f81c02c

Please sign in to comment.