Skip to content

Commit

Permalink
Merge pull request #3344 from bcgov/NDT-313-accordion-folder-edit-cbc…
Browse files Browse the repository at this point in the history
…-project-data

feat: accordion folder edit
  • Loading branch information
AntBush authored Jun 6, 2024
2 parents 0a37621 + 64e2370 commit 95411f1
Show file tree
Hide file tree
Showing 14 changed files with 3,011 additions and 490 deletions.
8 changes: 7 additions & 1 deletion app/components/Review/Accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const Accordion = ({
allowAnalystEdit,
children,
defaultToggled,
isCBC,
cbcId,
error,
name,
onToggle,
Expand Down Expand Up @@ -96,7 +98,11 @@ const Accordion = ({
<StyledToggleRight>
{allowAnalystEdit && (
<StyledLink
href={`/analyst/application/${applicationId}/edit/${name}`}
href={
isCBC
? `/analyst/cbc/${cbcId}/edit/${name}`
: `/analyst/application/${applicationId}/edit/${name}`
}
passHref
>
<StyledButton>
Expand Down
2 changes: 2 additions & 0 deletions app/components/Review/ReviewSectionField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const ReviewSectionField: React.FC<FieldProps> = (props) => {
name={name}
defaultToggled={uiOptions.defaultExpanded || hasErrors}
toggled={formContext.toggleOverride}
cbcId={formContext.cbcId}
isCBC={formContext.isCBC}
error={hasErrors}
title={schema.title}
allowAnalystEdit={allowAnalystEdit}
Expand Down
7 changes: 7 additions & 0 deletions app/formSchema/uiSchema/cbc/reviewUiSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ const reviewUiSchema = {
projectTitle: {
'ui:widget': 'TextWidget',
},
'ui:options': { allowAnalystEdit: true },
},
projectType: {
...projectTypeUiSchema,
'ui:options': { allowAnalystEdit: true },
},
locationsAndCounts: {
...locationsAndCountsUiSchema,
'ui:options': { allowAnalystEdit: true },
},
funding: {
...fundingUiSchema,
'ui:options': { allowAnalystEdit: true },
},
eventsAndDates: {
...eventsAndDatesUiSchema,
Expand Down Expand Up @@ -65,16 +69,19 @@ const reviewUiSchema = {
...eventsAndDatesUiSchema.dateAnnounced,
'ui:widget': 'DateWidget',
},
'ui:options': { allowAnalystEdit: true },
},
miscellaneous: {
...miscellaneousUiSchema,
'ui:options': { allowAnalystEdit: true },
},
projectDataReviews: {
...projectDataReviewsUiSchema,
lastReviewed: {
...projectDataReviewsUiSchema.lastReviewed,
'ui:widget': 'DateWidget',
},
'ui:options': { allowAnalystEdit: true },
},
};

Expand Down
95 changes: 20 additions & 75 deletions app/pages/analyst/cbc/[cbcId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import reviewUiSchema from 'formSchema/uiSchema/cbc/reviewUiSchema';
import editUiSchema from 'formSchema/uiSchema/cbc/editUiSchema';
import { useFeature } from '@growthbook/growthbook-react';
import CbcTheme from 'components/Analyst/CBC/CbcTheme';
import { createCbcSchemaData } from 'utils/schemaUtils';

const getCbcQuery = graphql`
query CbcIdQuery($rowId: Int!) {
Expand Down Expand Up @@ -69,7 +70,7 @@ const Cbc = ({
const [editMode, setEditMode] = useState(false);
const hiddenSubmitRef = useRef<HTMLButtonElement>(null);
const { session } = query;

const { rowId } = query.cbcByRowId;
const [formData, setFormData] = useState({} as any);
const [baseFormData, setBaseFormData] = useState({} as any);
useEffect(() => {
Expand All @@ -78,76 +79,16 @@ const Cbc = ({
const { edges } = cbcDataByCbcId;
const cbcData = edges[0].node;
const { jsonData } = cbcData;
const tombstone = {
projectNumber: jsonData.projectNumber,
originalProjectNumber: jsonData.originalProjectNumber,
phase: jsonData.phase,
intake: jsonData.intake,
projectStatus: jsonData.projectStatus,
projectTitle: jsonData.projectTitle,
projectDescription: jsonData.projectDescription,
applicantContractualName: jsonData.applicantContractualName,
currentOperatingName: jsonData.currentOperatingName,
eightThirtyMillionFunding: jsonData.eightThirtyMillionFunding,
federalFundingSource: jsonData.federalFundingSource,
federalProjectNumber: jsonData.federalProjectNumber,
};

const projectType = {
projectType: jsonData.projectType,
transportProjectType: jsonData.transportProjectType,
highwayProjectType: jsonData.highwayProjectType,
lastMileProjectType: jsonData.lastMileProjectType,
lastMileMinimumSpeed: jsonData.lastMileMinimumSpeed,
connectedCoastNetworkDependant: jsonData.connectedCoastNetworkDependant,
};
const locationsAndCounts = {
projectLocations: jsonData.projectLocations,
communitiesAndLocalesCount: jsonData.communitiesAndLocalesCount,
indigenousCommunities: jsonData.indigenousCommunities,
householdCount: jsonData.householdCount,
transportKm: jsonData.transportKm,
highwayKm: jsonData.highwayKm,
restAreas: jsonData.restAreas,
};

const funding = {
bcFundingRequest: jsonData.bcFundingRequest,
federalFunding: jsonData.federalFunding,
applicantAmount: jsonData.applicantAmount,
otherFunding: jsonData.otherFunding,
totalProjectBudget: jsonData.totalProjectBudget,
};

const eventsAndDates = {
nditConditionalApprovalLetterSent:
jsonData.nditConditionalApprovalLetterSent,
bindingAgreementSignedNditRecipient:
jsonData.bindingAgreementSignedNditRecipient,
announcedByProvince: jsonData.announcedByProvince,
dateApplicationReceived: jsonData.dateApplicationReceived,
dateConditionallyApproved: jsonData.dateConditionallyApproved,
dateAgreementSigned: jsonData.dateAgreementSigned,
proposedStartDate: jsonData.proposedStartDate,
proposedCompletionDate: jsonData.proposedCompletionDate,
reportingCompletionDate: jsonData.reportingCompletionDate,
dateAnnounced: jsonData.dateAnnounced,
};

const miscellaneous = {
projectMilestoneCompleted: jsonData.projectMilestoneCompleted,
constructionCompletedOn: jsonData.constructionCompletedOn,
milestoneComments: jsonData.milestoneComments,
primaryNewsRelease: jsonData.primaryNewsRelease,
secondaryNewsRelease: jsonData.secondaryNewsRelease,
notes: jsonData.notes,
};

const projectDataReviews = {
locked: jsonData.locked,
lastReviewed: jsonData.lastReviewed,
reviewNotes: jsonData.reviewNotes,
};
const {
tombstone,
projectType,
locationsAndCounts,
funding,
eventsAndDates,
miscellaneous,
projectDataReviews,
} = createCbcSchemaData(jsonData);

setFormData({
tombstone,
Expand Down Expand Up @@ -272,11 +213,15 @@ const Cbc = ({
)}
</RightAlignText>
<StyledCbcForm
additionalContext={
editMode
? { toggleOverride: toggleOverrideEdit, errors: formErrors }
: { toggleOverride: toggleOverrideReadOnly, errors: formErrors }
}
additionalContext={{
toggleOverride: editMode
? toggleOverrideEdit
: toggleOverrideReadOnly,
isEditable: true,
isCBC: true,
cbcId: rowId,
errors: formErrors,
}}
formData={formData}
handleChange={(e) => {
setFormData({ ...e.formData });
Expand Down
153 changes: 153 additions & 0 deletions app/pages/analyst/cbc/[cbcId]/edit/[section].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { SectionCbcDataQuery } from '__generated__/SectionCbcDataQuery.graphql';
import CbcAnalystLayout from 'components/Analyst/CBC/CbcAnalystLayout';
import defaultRelayOptions from 'lib/relay/withRelayOptions';
import Layout from 'components/Layout';
import { usePreloadedQuery } from 'react-relay';
import { RelayProps, withRelay } from 'relay-nextjs';
import { graphql } from 'relay-runtime';
import review from 'formSchema/analyst/cbc/review';
import { ProjectTheme } from 'components/Analyst/Project';
import { useState } from 'react';
import { useRouter } from 'next/router';
import editUiSchema from 'formSchema/uiSchema/cbc/editUiSchema';
import { FormBase } from 'components/Form';
import { Button } from '@button-inc/bcgov-theme';
import { RJSFSchema } from '@rjsf/utils';
import useModal from 'lib/helpers/useModal';
import { ChangeModal } from 'components/Analyst';
import { useUpdateCbcDataAndInsertChangeRequest } from 'schema/mutations/cbc/updateCbcDataAndInsertChangeReason';
import { createCbcSchemaData } from 'utils/schemaUtils';

const getCbcSectionQuery = graphql`
query SectionCbcDataQuery($rowId: Int!) {
cbcByRowId(rowId: $rowId) {
projectNumber
rowId
sharepointTimestamp
cbcDataByCbcId(first: 500) {
edges {
node {
jsonData
sharepointTimestamp
rowId
projectNumber
updatedAt
updatedBy
}
}
}
}
session {
sub
}
...CbcAnalystLayout_query
}
`;

const EditCbcSection = ({
preloadedQuery,
}: RelayProps<Record<string, unknown>, SectionCbcDataQuery>) => {
const query = usePreloadedQuery(getCbcSectionQuery, preloadedQuery);
const { session, cbcByRowId } = query;
const router = useRouter();
const section = router.query.section as string;
const [updateFormData] = useUpdateCbcDataAndInsertChangeRequest();
const [changeReason, setChangeReason] = useState<null | string>(null);
const [formData, setFormData] = useState<any>(null);

const { cbcDataByCbcId, rowId } = cbcByRowId;
const { jsonData, rowId: cbcDataRowId } = cbcDataByCbcId.edges[0].node;

const dataBySection = createCbcSchemaData(jsonData);

const changeModal = useModal();

const handleChangeRequestModal = (e) => {
changeModal.open();
setFormData({ ...dataBySection, [section]: e.formData });
};

const handleSubmit = () => {
updateFormData({
variables: {
inputCbcData: {
rowId: cbcDataRowId,
cbcDataPatch: {
jsonData: {
...formData.tombstone,
...formData.projectType,
...formData.locationsAndCounts,
...formData.funding,
...formData.eventsAndDates,
...formData.miscellaneous,
...formData.projectDataReviews,
},
},
},
inputCbcChangeReason: {
cbcDataChangeReason: {
description: changeReason,
cbcDataId: cbcDataRowId,
},
},
},
debounceKey: 'cbc_update_section_data',
onCompleted: () => {
router.push(`/analyst/cbc/${rowId}`);
},
});
};

return (
<Layout title="Edit CBC Section" session={session}>
<CbcAnalystLayout query={query}>
<FormBase
formData={dataBySection[section]}
schema={review.properties[section] as RJSFSchema}
theme={ProjectTheme}
uiSchema={editUiSchema[section]}
onSubmit={handleChangeRequestModal}
noValidate
noHtml5Validate
omitExtraData={false}
>
<Button>Save</Button>
<Button
variant="secondary"
style={{ marginLeft: '24px' }}
onClick={(e) => {
e.preventDefault();
router.push(`/analyst/cbc/${rowId}`);
}}
>
Cancel
</Button>
</FormBase>
</CbcAnalystLayout>
<ChangeModal
id="change-modal-cbc"
onCancel={() => {
setChangeReason(null);
changeModal.close();
}}
value={changeReason}
onChange={(e) => setChangeReason(e.target.value)}
onSave={handleSubmit}
{...changeModal}
/>
</Layout>
);
};

export const withRelayOptions = {
...defaultRelayOptions,

variablesFromContext: (ctx) => {
return {
rowId: parseInt(ctx.query.cbcId.toString(), 10),
section: ctx.query.section,
};
},
};

export default withRelay(EditCbcSection, getCbcSectionQuery, withRelayOptions);
36 changes: 36 additions & 0 deletions app/schema/mutations/cbc/updateCbcDataAndInsertChangeReason.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { graphql } from 'react-relay';
import { updateCbcDataAndInsertChangeReasonMutation } from '__generated__/updateCbcDataAndInsertChangeReasonMutation.graphql';
import useDebouncedMutation from '../useDebouncedMutation';

const mutation = graphql`
mutation updateCbcDataAndInsertChangeReasonMutation(
$inputCbcData: UpdateCbcDataByRowIdInput!
$inputCbcChangeReason: CreateCbcDataChangeReasonInput!
) {
updateCbcDataByRowId(input: $inputCbcData) {
cbcData {
id
rowId
jsonData
updatedAt
}
}
createCbcDataChangeReason(input: $inputCbcChangeReason) {
cbcDataChangeReason {
id
cbcDataId
description
createdBy
createdAt
}
}
}
`;

const useUpdateCbcDataAndInsertChangeRequest = () =>
useDebouncedMutation<updateCbcDataAndInsertChangeReasonMutation>(
mutation,
() => 'An error occurred while attempting to update the cbc data.'
);

export { mutation, useUpdateCbcDataAndInsertChangeRequest };
Loading

0 comments on commit 95411f1

Please sign in to comment.