diff --git a/app/admin/ascor/assessments.rb b/app/admin/ascor/assessments.rb index 6a58f9170..0a6ed560f 100644 --- a/app/admin/ascor/assessments.rb +++ b/app/admin/ascor/assessments.rb @@ -35,7 +35,7 @@ end panel 'Assessment Results' do - table_for resource.results.includes(:indicator).order(:id) do + table_for resource.results.includes(:indicator).order(:indicator_id) do column(:indicator) column(:answer) column(:source) diff --git a/app/assets/stylesheets/tpi/pages/ascor.scss b/app/assets/stylesheets/tpi/pages/ascor.scss index 18f69dbd1..fb904bc17 100644 --- a/app/assets/stylesheets/tpi/pages/ascor.scss +++ b/app/assets/stylesheets/tpi/pages/ascor.scss @@ -321,17 +321,34 @@ $see-more-width-tablet: 130px; &__indicator { margin-bottom: 1px; + outline: solid 1px $grey-lighter-medium; + margin-left: 40px; + padding: 15px 45px 15px 20px; + display: flex; &__title { - outline: solid 1px $grey-lighter-medium; font-family: $family-sans-serif; font-weight: 700; font-size: 16px; - margin-left: 40px; - padding: 15px 20px; display: flex; align-items: center; gap: 25px; + flex-grow: 2; + } + + &__source { + font-family: $family-sans-serif; + font-size: 14px !important; + line-height: 40px; + + a { + color: $grey-dark; + text-decoration: underline; + } + + a:hover { + color: $dark; + } } } diff --git a/app/controllers/tpi/ascor_controller.rb b/app/controllers/tpi/ascor_controller.rb index 004b710ac..992f2b864 100644 --- a/app/controllers/tpi/ascor_controller.rb +++ b/app/controllers/tpi/ascor_controller.rb @@ -19,6 +19,7 @@ def index def show @assessment = ASCOR::Assessment.find_by country: @country, assessment_date: @assessment_date + @recent_emissions = Api::ASCOR::RecentEmissions.new(@assessment_date, @country).call fixed_navbar("ASCOR Country #{@country.name}", admin_ascor_country_path(@country.id)) end diff --git a/app/javascript/components/tpi/AscorRecentEmissions.js b/app/javascript/components/tpi/AscorRecentEmissions.js new file mode 100644 index 000000000..61cca5c8a --- /dev/null +++ b/app/javascript/components/tpi/AscorRecentEmissions.js @@ -0,0 +1,35 @@ +import PropTypes from 'prop-types'; + +// eslint-disable-next-line no-empty-pattern +const AscorRecentEmissions = ({ }) => { + +}; + +AscorRecentEmissions.propTypes = { + emissions_metric_filter: PropTypes.arrayOf(PropTypes.string).isRequired, + default_emissions_metric_filter: PropTypes.string.isRequired, + emissions_boundary_filter: PropTypes.arrayOf(PropTypes.string).isRequired, + default_emissions_boundary_filter: PropTypes.string.isRequired, + trend_filters: PropTypes.arrayOf(PropTypes.string).isRequired, + default_trend_filter: PropTypes.string.isRequired, + data: PropTypes.arrayOf( + PropTypes.shape({ + value: PropTypes.number, + source: PropTypes.string, + year: PropTypes.number, + unit: PropTypes.string.isRequired, + trends: PropTypes.shape({ + source: PropTypes.string, + year: PropTypes.number, + values: PropTypes.arrayOf( + PropTypes.shape({ + filter: PropTypes.string.isRequired, + value: PropTypes.string.isRequired + }) + ).isRequired + }).isRequired + }) + ).isRequired +}; + +export default AscorRecentEmissions; diff --git a/app/services/api/ascor/recent_emissions.rb b/app/services/api/ascor/recent_emissions.rb new file mode 100644 index 000000000..fc6a60ba0 --- /dev/null +++ b/app/services/api/ascor/recent_emissions.rb @@ -0,0 +1,40 @@ +module Api + module ASCOR + class RecentEmissions + attr_accessor :assessment_date, :country + + def initialize(assessment_date, country) + @assessment_date = assessment_date + @country = country + end + + def call + pathways.map do |pathway| + { + value: pathway.recent_emission_level, + source: pathway.recent_emission_source, + year: pathway.recent_emission_year, + emissions_metric: pathway.emissions_metric, + emissions_boundary: pathway.emissions_boundary, + unit: pathway.units, + trend: { + source: pathway.trend_source, + year: pathway.trend_year, + values: [ + {filter: '1 year trend', value: pathway.trend_1_year}, + {filter: '3 years trend', value: pathway.trend_3_year}, + {filter: '5 years trend', value: pathway.trend_5_year} + ] + } + } + end + end + + private + + def pathways + @pathways ||= ::ASCOR::Pathway.where(assessment_date: assessment_date, country: country) + end + end + end +end diff --git a/app/views/tpi/ascor/_assessment.html.erb b/app/views/tpi/ascor/_assessment.html.erb index fa65af0c3..8e3402dff 100644 --- a/app/views/tpi/ascor/_assessment.html.erb +++ b/app/views/tpi/ascor/_assessment.html.erb @@ -22,31 +22,38 @@
<% ascor_sub_indicators_for(area, indicators).each do |indicator| %> -
-
- <%= "#{indicator.code.split('.').last}. #{indicator.text}" %> +
+
+
+ <%= "#{indicator.code.split('.').last}. #{indicator.text}" %> +
+ <% if ascor_assessment_result_for(indicator, @assessment).source.present? %> +
+ <%= link_to 'Source', ascor_assessment_result_for(indicator, @assessment).source %> +
+ <% end %>
<% ascor_sub_indicators_for(indicator, metrics).each do |metric| %>
-
- <% if metric.code == 'EP.1.a.i' %> - - <% elsif metric.code == 'EP.1.a.ii' %> - - <% elsif ascor_assessment_result_for(metric, @assessment).answer.present? %> + <% if metric.code == 'EP.1.a.i' %> + <%= render 'tpi/ascor/metrics_ep1a', recent_emissions: @recent_emissions %> + <% elsif metric.code == 'EP.1.a.ii' %> + <%# skipped because EP.1.a.i and EP.1.a.ii are rendered via same React component %> + <% else %> + <% if ascor_assessment_result_for(metric, @assessment).answer.present? %>
<%= ascor_assessment_result_for(metric, @assessment).answer %>
<% end %> -
-
- <%= "#{metric.code.split('.').last}. #{metric.text}" %> -
- <% if ascor_assessment_result_for(metric, @assessment).source.present? %> -
- <%= link_to 'Source', ascor_assessment_result_for(metric, @assessment).source %> +
+ <%= "#{metric.code.split('.').last}. #{metric.text}" %>
+ <% if ascor_assessment_result_for(metric, @assessment).source.present? %> +
+ <%= link_to 'Source', ascor_assessment_result_for(metric, @assessment).source %> +
+ <% end %> <% end %>
<% end %> diff --git a/app/views/tpi/ascor/_metrics_ep1a.html.erb b/app/views/tpi/ascor/_metrics_ep1a.html.erb new file mode 100644 index 000000000..e240fc3b6 --- /dev/null +++ b/app/views/tpi/ascor/_metrics_ep1a.html.erb @@ -0,0 +1,9 @@ +<%= react_component('AscorRecentEmissions', { + emissions_metric_filter: ASCOR::EmissionsMetric::VALUES, + default_emissions_metric_filter: 'Absolute', + emissions_boundary_filter: ASCOR::EmissionsBoundary::VALUES, + default_emissions_boundary_filter: 'Production - excluding LULUCF', + trend_filters: ['1 year trend', '3 years trend', '5 years trend'], + default_trend_filter: '1 year trend', + data: recent_emissions +}) %> \ No newline at end of file diff --git a/spec/services/api/ascor/recent_emissions_spec.rb b/spec/services/api/ascor/recent_emissions_spec.rb new file mode 100644 index 000000000..26ded6837 --- /dev/null +++ b/spec/services/api/ascor/recent_emissions_spec.rb @@ -0,0 +1,82 @@ +require 'rails_helper' + +RSpec.describe Api::ASCOR::RecentEmissions do + subject { described_class.new(@assessment_date, @country).call } + + before_all do + @country = create(:ascor_country, id: 1, name: 'USA', iso: 'USA') + @assessment_date = Date.new(2019, 1, 1) + + create :ascor_pathway, + country: @country, + assessment_date: Date.new(2019, 1, 1), + emissions_metric: 'Intensity per capita', + emissions_boundary: 'Consumption - excluding LULUCF', + units: 'MtCO2e', + trend_1_year: '+ 6%', + trend_3_year: '+ 8%', + trend_5_year: '+ 9%', + trend_source: 'source', + trend_year: 2019, + recent_emission_level: 100, + recent_emission_source: 'source', + recent_emission_year: 2019 + create :ascor_pathway, + country: @country, + assessment_date: Date.new(2019, 1, 1), + emissions_metric: 'Absolute', + emissions_boundary: 'Production - only LULUCF', + units: 'MtCO2e', + trend_1_year: '- 6%', + trend_3_year: '+ 6%', + trend_5_year: '+ 8%', + trend_source: 'source 2', + trend_year: 2020, + recent_emission_level: 200, + recent_emission_source: 'source 2', + recent_emission_year: 2020 + create :ascor_pathway, country: create(:ascor_country, id: 2, name: 'Czechia', iso: 'CZE') + create :ascor_pathway, country: @country, assessment_date: Date.new(2019, 2, 1) + end + + it 'returns expected result' do + expect(subject).to match_array( + [ + { + value: 100, + source: 'source', + year: 2019, + unit: 'MtCO2e', + emissions_metric: 'Intensity per capita', + emissions_boundary: 'Consumption - excluding LULUCF', + trend: { + source: 'source', + year: 2019, + values: [ + {filter: '1 year trend', value: '+ 6%'}, + {filter: '3 years trend', value: '+ 8%'}, + {filter: '5 years trend', value: '+ 9%'} + ] + } + }, + { + value: 200, + source: 'source 2', + year: 2020, + unit: 'MtCO2e', + emissions_metric: 'Absolute', + emissions_boundary: 'Production - only LULUCF', + trend: { + source: 'source 2', + year: 2020, + values: [ + {filter: '1 year trend', value: '- 6%'}, + {filter: '3 years trend', value: '+ 6%'}, + {filter: '5 years trend', value: '+ 8%'} + ] + } + } + ] + ) + end +end