Skip to content

Commit

Permalink
Merge pull request #727 from bcgov/fix/587-milestone-reporting-read-only
Browse files Browse the repository at this point in the history
feat: #587 milestone reporting read only
  • Loading branch information
gurjmatharu authored Jun 17, 2022
2 parents f10561e + adb9336 commit bd40529
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 4 deletions.
167 changes: 167 additions & 0 deletions app/components/Form/ProjectMilestoneReportFormSummary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import { useMemo } from "react";
import { graphql, useFragment } from "react-relay";
import FormBase from "./FormBase";
import readOnlyTheme from "lib/theme/ReadOnlyTheme";
import type { JSONSchema7 } from "json-schema";

import CUSTOM_DIFF_FIELDS from "lib/theme/CustomDiffFields";
import { utils } from "@rjsf/core";
import { ProjectMilestoneReportFormSummary_projectRevision$key } from "__generated__/ProjectMilestoneReportFormSummary_projectRevision.graphql";
import { getFilteredSchema } from "lib/theme/getFilteredSchema";
import { projectMilestoneSchema } from "data/jsonSchemaForm/projectMilestoneSchema";

const { fields } = utils.getDefaultRegistry();

const customFields = { ...fields, ...CUSTOM_DIFF_FIELDS };

interface Props {
projectRevision: ProjectMilestoneReportFormSummary_projectRevision$key;
viewOnly?: boolean;
}
const milestoneReportUiSchema = {
reportDueDate: {
"ui:widget": "DueDateWidget",
},
submittedDate: {
"ui:widget": "ReceivedDateWidget",
},
comments: {
"ui:widget": "TextAreaWidget",
},
};

const ProjectMilestoneReportFormSummary: React.FC<Props> = (props) => {
const { summaryProjectMilestoneReportFormChanges, isFirstRevision } =
useFragment(
graphql`
fragment ProjectMilestoneReportFormSummary_projectRevision on ProjectRevision {
isFirstRevision
summaryProjectMilestoneReportFormChanges: formChangesFor(
formDataTableName: "reporting_requirement"
reportType: "Milestone"
) {
edges {
node {
id
isPristine
newFormData
operation
formChangeByPreviousFormChangeId {
newFormData
}
}
}
}
}
`,
props.projectRevision
);

const renderDiff = !isFirstRevision && !props.viewOnly;

// If we are showing the diff then we want to see archived records, otherwise filter out the archived milestone reports
let milestoneReportFormChanges =
summaryProjectMilestoneReportFormChanges.edges;
if (!renderDiff)
milestoneReportFormChanges =
summaryProjectMilestoneReportFormChanges.edges.filter(
({ node }) => node.operation !== "ARCHIVE"
);

// Sorting the milestone reports form changes by the reporting requirement index
const [sortedMilestoneReports] = useMemo(() => {
const filteredReports = milestoneReportFormChanges.map(({ node }) => node);

filteredReports.sort(
(a, b) =>
a.newFormData.reportingRequirementIndex -
b.newFormData.reportingRequirementIndex
);

return [filteredReports];
}, [milestoneReportFormChanges]);

const allFormChangesPristine = useMemo(
() =>
!milestoneReportFormChanges.some(
({ node }) => node?.isPristine === false || node?.isPristine === null
),
[milestoneReportFormChanges]
);

const milestoneReportsJSX = useMemo(() => {
return sortedMilestoneReports.map((milestoneReport, index) => {
if (!milestoneReport) return;

// Set the formSchema and formData based on showing the diff or not
const { formSchema, formData } = !renderDiff
? {
formSchema: projectMilestoneSchema,
formData: milestoneReport.newFormData,
}
: getFilteredSchema(
projectMilestoneSchema as JSONSchema7,
milestoneReport
);

return (
<div key={index} className="reportContainer">
<header>
<h4>Milestone Report {index + 1}</h4>
</header>
{/* Show this part if none of milestone report form properties have been updated */}
{Object.keys(formSchema.properties).length === 0 &&
milestoneReport.operation !== "ARCHIVE" && (
<em>Milestone report not updated</em>
)}

{/* Show this part if the whole milestone report has been removed */}
{renderDiff && milestoneReport.operation === "ARCHIVE" && (
<em className="diffOld">Milestone Report Removed</em>
)}
<FormBase
liveValidate
key={`form-${milestoneReport.id}`}
tagName={"dl"}
theme={readOnlyTheme}
fields={renderDiff ? customFields : fields}
schema={formSchema as JSONSchema7}
uiSchema={milestoneReportUiSchema}
formData={formData}
formContext={{
operation: milestoneReport.operation,
oldData:
milestoneReport.formChangeByPreviousFormChangeId?.newFormData,
}}
/>
<style jsx>{`
div.reportContainer {
padding-top: 1em;
}
`}</style>
</div>
);
});
}, [sortedMilestoneReports, renderDiff]);

return (
<>
<h3>Project Milestone Reports</h3>
{milestoneReportFormChanges.length < 1 && props.viewOnly && (
<dd>
<em>No Milestone Reports</em>
</dd>
)}
{(allFormChangesPristine || milestoneReportFormChanges.length < 1) &&
!props.viewOnly ? (
<dd>
<em>Milestone reports not {isFirstRevision ? "added" : "updated"}</em>
</dd>
) : (
milestoneReportsJSX
)}
</>
);
};

export default ProjectMilestoneReportFormSummary;
1 change: 0 additions & 1 deletion app/components/Form/ProjectQuarterlyReportFormSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ const ProjectQuarterlyReportFormSummary: React.FC<Props> = (props) => {
projectReportingRequirementSchema as JSONSchema7,
quarterlyReport
);

return (
<div key={index} className="reportContainer">
<header>
Expand Down
35 changes: 35 additions & 0 deletions app/cypress/integration/cif/project-revision/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ describe("the new project page", () => {
cy.findByText(/project overview not added/i).should("be.visible");
cy.findByText(/project managers not added/i).should("be.visible");
cy.findByText(/Annual Report 3/i).should("be.visible");
cy.findByText(/Milestone Report 1/i).should("be.visible");
cy.checkA11y("main", tempRules, logAxeResults);
cy.get("body").happoScreenshot({
component: "Project Summary Form",
Expand Down Expand Up @@ -509,6 +510,31 @@ describe("the new project page", () => {
cy.contains("Changes saved.");
cy.findByRole("button", { name: /^submit/i }).click();

// edit milestone reports
cy.contains("Review and Submit Project");
cy.findByRole("button", { name: /Milestone reports/i }).click();
cy.findByText(/Edit milestone 1/i).click();
cy.get('[aria-label*="Milestone Description"]')
.eq(0)
.clear()
.type("new description");
cy.get('[aria-label*="Due Date"]').eq(0).click();
cy.get(".react-datepicker__month-select").select(0);
cy.get(".react-datepicker__year-select").select("1999");
cy.get(`.react-datepicker__day--001`)
.not(`.react-datepicker__day--outside-month`)
.click();
cy.contains("Changes saved.");
cy.get("body").happoScreenshot({
component: "Project Milestone Reports Form",
variant: "editing",
});
cy.findByRole("button", { name: /^submit/i }).click();

cy.findByText(/Submit changes/i).click();
cy.contains("Changes saved.");
cy.findByText(/Review and submit information/i).click();

// edit quarterly reports
cy.contains("Review and Submit Project");
cy.findByRole("button", { name: /Quarterly reports/i }).click();
Expand Down Expand Up @@ -595,6 +621,7 @@ describe("the new project page", () => {
rules: {
dlitem: { enabled: false },
"definition-list": { enabled: false },
"duplicate-id": { enabled: false },
},
};
//Checking for diffing color accessibility
Expand Down Expand Up @@ -623,6 +650,14 @@ describe("the new project page", () => {
cy.findByText(/^Secondary contacts/i)
.next()
.should("have.text", "No Secondary contacts");
cy.findByText(/Milestone reports/i).click();
cy.get("a")
.contains(/Milestone 1/i)
.click();
cy.findAllByText(/Report Due Date/i)
.eq(0)
.next()
.contains(/Jan[.]? 1, 1999/i);
cy.findByText(/Quarterly reports/i).click();
cy.get("a")
.contains(/Quarterly reports/i)
Expand Down
4 changes: 2 additions & 2 deletions app/data/formPages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ProjectQuarterlyReportForm from "components/Form/ProjectQuarterlyReportFo
import ProjectAnnualReportForm from "components/Form/ProjectAnnualReportForm";
import ProjectQuarterlyReportFormSummary from "components/Form/ProjectQuarterlyReportFormSummary";
import ProjectAnnualReportFormSummary from "components/Form/ProjectAnnualReportFormSummary";
import ProjectMilestoneReportFormSummary from "components/Form/ProjectMilestoneReportFormSummary";

interface FormPageDefinition {
title: string;
Expand All @@ -35,8 +36,7 @@ const formPages: FormPageDefinition[] = [
{
title: "milestone reports",
editComponent: ProjectMilestoneReportForm,
// TODO: switch to ProjectMilestoneReportFormSummary when it's been merged
viewComponent: null,
viewComponent: ProjectMilestoneReportFormSummary,
},
{
title: "quarterly reports",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const mutation = graphql`
const useUpdateReportingRequirementFormChange = () => {
return useDebouncedMutation<updateReportingRequirementFormChangeMutation>(
mutation,
() => "An error occurred when updating the project quarterly reports form."
() => "An error occurred when updating the project reports form."
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const pageQuery = graphql`
...ProjectQuarterlyReportFormSummary_projectRevision
...ProjectAnnualReportForm_projectRevision
...ProjectMilestoneReportForm_projectRevision
...ProjectMilestoneReportFormSummary_projectRevision
...ProjectAnnualReportFormSummary_projectRevision
}
...ProjectForm_query
Expand Down
5 changes: 5 additions & 0 deletions app/pages/cif/project-revision/[projectRevision]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { useMemo } from "react";
import { TaskListMode } from "components/TaskList/types";
import ProjectQuarterlyReportFormSummary from "components/Form/ProjectQuarterlyReportFormSummary";
import ProjectAnnualReportFormSummary from "components/Form/ProjectAnnualReportFormSummary";
import ProjectMilestoneReportFormSummary from "components/Form/ProjectMilestoneReportFormSummary";

const pageQuery = graphql`
query ProjectRevisionQuery($projectRevision: ID!) {
Expand All @@ -47,6 +48,7 @@ const pageQuery = graphql`
...ProjectQuarterlyReportFormSummary_projectRevision
...ProjectAnnualReportFormSummary_projectRevision
...TaskList_projectRevision
...ProjectMilestoneReportFormSummary_projectRevision
projectByProjectId {
latestCommittedProjectRevision {
id
Expand Down Expand Up @@ -210,6 +212,9 @@ export function ProjectRevision({
<ProjectFormSummary projectRevision={query.projectRevision} />
<ProjectManagerFormSummary projectRevision={query.projectRevision} />
<ProjectContactFormSummary projectRevision={query.projectRevision} />
<ProjectMilestoneReportFormSummary
projectRevision={query.projectRevision}
/>
<ProjectQuarterlyReportFormSummary
projectRevision={query.projectRevision}
/>
Expand Down
Loading

0 comments on commit bd40529

Please sign in to comment.