Skip to content

Commit

Permalink
Merge pull request #913 from bcgov/feat/calculated-intensity-performance
Browse files Browse the repository at this point in the history
Feat/calculated intensity performance
  • Loading branch information
pbastia authored Aug 8, 2022
2 parents 4b0bd3d + 9d177b2 commit e49710f
Showing 28 changed files with 498 additions and 24 deletions.
9 changes: 9 additions & 0 deletions app/components/Form/ProjectEmissionIntensityReportForm.tsx
Original file line number Diff line number Diff line change
@@ -58,6 +58,9 @@ const ProjectEmissionsIntensityReport: React.FC<Props> = (props) => {
edges {
node {
id
asEmissionIntensityReport {
calculatedEiPerformance
}
rowId
newFormData
changeStatus
@@ -76,6 +79,10 @@ const ProjectEmissionsIntensityReport: React.FC<Props> = (props) => {
projectRevision.emissionIntensityReportingRequirementFormChange.edges[0]
?.node;

const calculatedEiPerformance =
projectRevision.emissionIntensityReportFormChange.edges[0]?.node
.asEmissionIntensityReport.calculatedEiPerformance;

const [
addProjectEmissionsIntensityReport,
isAddingEmissionsIntensityReportFormChange,
@@ -196,6 +203,8 @@ const ProjectEmissionsIntensityReport: React.FC<Props> = (props) => {
formData={emissionIntensityReportFormChange?.newFormData}
formContext={{
form: emissionIntensityReportFormChange?.newFormData,
calculatedValue: calculatedEiPerformance,
isPercent: true,
}}
uiSchema={emissionIntensityReportUiSchema}
onChange={(change) =>
Original file line number Diff line number Diff line change
@@ -101,9 +101,16 @@ describe("when creating a project, the project page", () => {
// add teimp reports
cy.findByRole("link", { name: /Emissions Intensity Report/i }).click();
cy.findByRole("button", { name: /Add TEIMP Agreement/i }).click();
cy.addEmissionIntensityReport("2022-01-01", "2022-02-02", "1", "2", "3");
cy.addEmissionIntensityReport(
"2022-01-01",
"2022-02-02",
"1",
"2",
"3",
"4"
);
cy.contains("Changes saved").should("be.visible");
cy.happoAndAxe("Emission Intensity Form", "filled", "main");
cy.happoAndAxe("Emission Intensity Form", "filled", "main", true);
cy.findByText(/Submit TEIMP Report/).click();

// No annual reports
Original file line number Diff line number Diff line change
@@ -132,9 +132,16 @@ describe("when editing a project, the project page", () => {
cy.findByRole("button", { name: /Emissions Intensity Report/i }).click();
cy.findByText(/Edit emissions intensity report/i).click();
cy.findByRole("button", { name: /Add TEIMP Agreement/i }).click();
cy.addEmissionIntensityReport("2022-01-01", "2022-02-02", "1", "2", "3");
cy.addEmissionIntensityReport(
"2022-01-01",
"2022-02-02",
"1",
"2",
"3",
"4"
);
cy.contains("Changes saved.");
cy.happoAndAxe("Project teimp agreement form", "editing", "main");
cy.happoAndAxe("Project teimp agreement form", "editing", "main", true);
cy.findByText(/Submit TEIMP report/i).click();

// edit annual reports -- change comments
5 changes: 3 additions & 2 deletions app/cypress/integration/cif/project-revision/index.spec.js
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ describe("the new project page", () => {
cy.findByText(/Add emissions intensity report/i).click();
cy.findByText(/Add TEIMP Agreement/i).click();
cy.contains("Changes saved.");
cy.happoAndAxe("Emissions Intensity Report", "empty", "main");
cy.happoAndAxe("Emissions Intensity Report", "empty", "main", true);

// Annual reports
cy.findByText(/Annual reports/i).click();
@@ -231,7 +231,8 @@ describe("the new project page", () => {
cy.happoAndAxe(
"Emissions intensity report Form",
"with errors",
".error-detail"
".error-detail",
true
);

// Annual reports
6 changes: 5 additions & 1 deletion app/cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -374,7 +374,8 @@ Cypress.Commands.add(
measurmentPeriodEndDate,
functionalUnit,
baselineEmissionIntensity,
targetEmissionIntensity
targetEmissionIntensity,
postProjectEmissionIntensity
) => {
cy.addMeasurementPeriodStartDate(measurementPeriodStartDate);
cy.addMeasurementPeriodEndDate(measurmentPeriodEndDate);
@@ -385,6 +386,9 @@ Cypress.Commands.add(
cy.get('[aria-label="Target Emission Intensity (TEI)"]')
.clear()
.type(targetEmissionIntensity);
cy.get('[aria-label*="Post Project Emission Intensity"]')
.clear()
.type(postProjectEmissionIntensity);

return cy.url().should("include", "/form/6");
}
17 changes: 17 additions & 0 deletions app/data/jsonSchemaForm/projectEmissionIntensitySchema.ts
Original file line number Diff line number Diff line change
@@ -39,6 +39,14 @@ export const emissionIntensityReportSchema = {
title: "Total lifetime emissions reductions",
type: "number",
},
calculatedGHGEmissionIntensityPerformance: {
title: "GHG Emission Intensity Performance",
type: "number",
},
adjustedGHGEmissionIntensityPerformance: {
title: "GHG Emission Intensity Performance (Adjusted)",
type: "number",
},
},
};

@@ -84,6 +92,15 @@ export const emissionIntensityReportUiSchema = {
totalLifetimeEmissionReduction: {
"ui:widget": "TextWidget",
},
calculatedGHGEmissionIntensityPerformance: {
"ui:widget": "ReadOnlyCalculatedValueWidget",
isPercent: true,
hideOptional: true,
},
adjustedGHGEmissionIntensityPerformance: {
"ui:widget": "AdjustableCalculatedValueWidget",
isPercent: true,
},
};

export const emissionIntensityReportingRequirementUiSchema = {
8 changes: 7 additions & 1 deletion app/lib/theme/FieldTemplate.tsx
Original file line number Diff line number Diff line change
@@ -12,11 +12,17 @@ const FieldTemplate: React.FC<FieldTemplateProps> = ({
displayLabel,
required,
id,
uiSchema,
}) => {
return (
<div>
{displayLabel && (
<FieldLabel label={label} required={required} htmlFor={id} />
<FieldLabel
label={label}
required={required}
htmlFor={id}
uiSchema={uiSchema}
/>
)}
{help}
{children}
2 changes: 2 additions & 0 deletions app/lib/theme/FormWithTheme.tsx
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ import DueDateWidget from "lib/theme/widgets/DueDateWidget";
import ConditionalAmountWidget from "./widgets/ConditionalAmountWidget";
import DateWidget from "./widgets/DateWidget";
import { AdjustableCalculatedValueWidget } from "./widgets/AdjustableCalculatedValueWidget";
import ReadOnlyCalculatedValueWidget from "./widgets/ReadOnlyCalculatedValueWidget";

const { fields, widgets } = utils.getDefaultRegistry();

@@ -34,6 +35,7 @@ const formTheme: ThemeProps = {
DueDateWidget: DueDateWidget,
DateWidget,
AdjustableCalculatedValueWidget,
ReadOnlyCalculatedValueWidget,
},
ObjectFieldTemplate: ObjectFieldTemplate,
FieldTemplate: FieldTemplate,
2 changes: 2 additions & 0 deletions app/lib/theme/ReadOnlyTheme.tsx
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import ReadOnlyWidget from "./widgets/ReadOnlyWidget";
import ReadOnlyMoneyWidget from "./widgets/ReadOnlyMoneyWidget";
import ReadOnlyDateWidget from "./widgets/ReadOnlyDateWidget";
import ReadOnlyAdjustableCalculatedValueWidget from "./widgets/ReadOnlyAdjustableCalculatedValueWidget";
import ReadOnlyCalculatedValueWidget from "./widgets/ReadOnlyCalculatedValueWidget";
const { fields, widgets } = utils.getDefaultRegistry();

const readOnlyTheme: ThemeProps = {
@@ -27,6 +28,7 @@ const readOnlyTheme: ThemeProps = {
DueDateWidget: ReadOnlyDateWidget,
DateWidget: ReadOnlyDateWidget,
AdjustableCalculatedValueWidget: ReadOnlyAdjustableCalculatedValueWidget,
ReadOnlyCalculatedValueWidget,
},
ObjectFieldTemplate: ReadOnlyObjectFieldTemplate,
FieldTemplate: ReadOnlyFieldTemplate,
14 changes: 8 additions & 6 deletions app/lib/theme/widgets/AdjustableCalculatedValueWidget.tsx
Original file line number Diff line number Diff line change
@@ -15,8 +15,9 @@ export const AdjustableCalculatedValueWidget: React.FC<WidgetProps> = (
formContext,
} = props;

// If we are using this widget to show numbers as money, we can set `isMoney` to true in the uiSchema.
// If we are using this widget to show numbers as money or percent, we can set `isMoney` or `isPercent` to true in the uiSchema.
const isMoney = uiSchema?.isMoney;
const isPercent = uiSchema?.isPercent;

const calculatedValue =
formContext[uiSchema.calculatedValueFormContextProperty];
@@ -29,12 +30,13 @@ export const AdjustableCalculatedValueWidget: React.FC<WidgetProps> = (
<>
<NumberFormat
thousandSeparator
fixedDecimalScale={isMoney}
fixedDecimalScale={isMoney || isPercent}
id={id}
prefix={isMoney ? "$" : ""}
suffix={isPercent ? "%" : ""}
disabled={disabled}
className="adjustable"
decimalScale={isMoney ? 2 : 10} //Hardcoded for now, we can change it if we need to
decimalScale={isMoney || isPercent ? 2 : 10} //Hardcoded for now, we can change it if we need to
defaultValue={(schema as any).defaultValue}
value={calculatedValue}
displayType="text"
@@ -44,13 +46,13 @@ export const AdjustableCalculatedValueWidget: React.FC<WidgetProps> = (
)}
<NumberFormat
thousandSeparator
fixedDecimalScale={isMoney}
fixedDecimalScale={isMoney || isPercent}
id={adjustedInputId}
prefix={isMoney ? "$" : ""}
suffix={isPercent ? "%" : ""}
disabled={disabled}
className="adjustable"
decimalScale={isMoney ? 2 : 10} //Hardcoded for now, we can change it if we need to
defaultValue={(schema as any).defaultValue}
decimalScale={isMoney || isPercent ? 2 : 10} //Hardcoded for now, we can change it if we need toma as any).defaultValue}
value={value}
onValueChange={({ floatValue }) => {
if (
6 changes: 5 additions & 1 deletion app/lib/theme/widgets/FieldLabel.tsx
Original file line number Diff line number Diff line change
@@ -3,19 +3,23 @@ interface Props {
required: boolean;
htmlFor: string;
tagName?: "label" | "dt";
uiSchema?: any;
}

const FieldLabel: React.FC<Props> = ({
label,
required,
htmlFor,
tagName = "label",
uiSchema,
}) => {
if (!label) {
return null;
}

const displayedLabel = label + (required ? "" : " (optional)") + " ";
const displayedLabel = uiSchema?.hideOptional
? label
: label + (required ? "" : " (optional)") + " ";

if (tagName === "label")
return <label htmlFor={htmlFor}>{displayedLabel}</label>;
Original file line number Diff line number Diff line change
@@ -6,8 +6,9 @@ const ReadOnlyAdjustableCalculatedValueWidget: React.FC<WidgetProps> = (
) => {
const { id, value, uiSchema, label, formContext } = props;

// If we are using this widget to show numbers as money, we can set `isMoney` to true in the uiSchema.
// If we are using this widget to show numbers as money or percent, we can set `isMoney` or `isPercent` to true in the uiSchema.
const isMoney = uiSchema?.isMoney;
const isPercent = uiSchema?.isPercent;

const calculatedValue =
formContext[uiSchema.calculatedValueFormContextProperty];
@@ -21,10 +22,11 @@ const ReadOnlyAdjustableCalculatedValueWidget: React.FC<WidgetProps> = (
{
<NumberFormat
thousandSeparator
fixedDecimalScale={isMoney}
fixedDecimalScale={isMoney || isPercent}
id={id}
prefix={isMoney ? "$" : ""}
decimalScale={isMoney ? 2 : 10}
suffix={isPercent ? "%" : ""}
decimalScale={isMoney || isPercent ? 2 : 10}
value={value}
displayType="text"
/>
@@ -40,13 +42,15 @@ const ReadOnlyAdjustableCalculatedValueWidget: React.FC<WidgetProps> = (
fixedDecimalScale={isMoney}
id={adjustedInputId}
prefix={isMoney ? "$" : ""}
decimalScale={isMoney ? 2 : 10}
suffix={isPercent ? "%" : ""}
decimalScale={isMoney || isPercent ? 2 : 10}
value={value}
displayType="text"
/>
</dd>
</div>
)}
{!value && <em>Not added</em>}
<style jsx>{`
div.adjustedValue {
position: relative;
43 changes: 43 additions & 0 deletions app/lib/theme/widgets/ReadOnlyCalculatedValueWidget.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { WidgetProps } from "@rjsf/core";
import NumberFormat from "react-number-format";

const ReadOnlyCalculatedValueWidget: React.FC<WidgetProps> = ({
id,
formContext,
label,
uiSchema,
}) => {
// If we are using this widget to show numbers as money or percent, we can set `isMoney` or `isPercent` to true in the uiSchema.
const isMoney = uiSchema?.isMoney;
const isPercent = uiSchema?.isPercent;

return (
<dd>
{formContext.calculatedValue ? (
<NumberFormat
fixedDecimalScale={isMoney || isPercent}
prefix={isMoney ? "$" : ""}
suffix={isPercent ? "%" : ""}
id={id}
decimalScale={isMoney || isPercent ? 2 : 10} //Hardcoded for now, we can change it if we need to
value={formContext.calculatedValue}
displayType="text"
aria-label={label}
/>
) : (
<NumberFormat
fixedDecimalScale={isMoney || isPercent}
prefix={isMoney ? "$" : ""}
suffix={isPercent ? "%" : ""}
id={id}
decimalScale={isMoney || isPercent ? 2 : 10}
value={0}
displayType="text"
aria-label={label}
/>
)}
</dd>
);
};

export default ReadOnlyCalculatedValueWidget;
Original file line number Diff line number Diff line change
@@ -11,6 +11,9 @@ const mutation = graphql`
formChanges {
id
rowId
asEmissionIntensityReport {
calculatedEiPerformance
}
newFormData
projectRevisionByProjectRevisionId {
...TaskList_projectRevision
Original file line number Diff line number Diff line change
@@ -9,6 +9,9 @@ const mutation = graphql`
updateFormChange(input: $input) {
formChange {
id
asEmissionIntensityReport {
calculatedEiPerformance
}
newFormData
operation
changeStatus
Loading

0 comments on commit e49710f

Please sign in to comment.