-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3344 from bcgov/NDT-313-accordion-folder-edit-cbc…
…-project-data feat: accordion folder edit
- Loading branch information
Showing
14 changed files
with
3,011 additions
and
490 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
app/schema/mutations/cbc/updateCbcDataAndInsertChangeReason.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Oops, something went wrong.